Recently there's a lot of posts about writing minimal Docker images from Nix flakes, such as:
https://tmp.bearblog.dev/minimal-containers-using-nix/ (by Herman, from BearBlog)
and
https://xeiaso.net/talks/2024/nix-docker-build/ (by https://pony.social/@cadey)
These last two weeks I've been learning Nix from zero. Yesterday, I was able to finish a flake that outputs a 4.2MB (gzipped) Docker image for one of my projects, a static SPA which I bundled with Static-Web-Server.
It's awesome that we have the means to achieve this.
To achieve this, I had to walk a error prone, confusing and painfully slow progressing path, where jumping between docs, blog posts, discourse topics and PR/Issues felt like looking for fabrics to stitch together just to get things working.
There's a lot of great Nix resources out there, but there's also a reason why devs prefer using tools like flox, devenv, devbox, etc. from straight up using the underlying technology.
It's amazingly complex to get things up and running. It also happens that, while most resources provide practical examples for readers to learn from, those still end up being snippets and simpler examples covering first principles and fundamentals of Nix.
I've been thinking about releasing a small series of blog posts focused in a personal, pragmatic approach on how to get things done with Nix and flakes from my personal experience.
If anyone interested in such series, please let me know!
At some point in this adventure, I got the image size reduced to a fifth, just by changing two lines inside the flake.nix file. It felt unbelievable.
The gzipped image. I plan to keep reducing its size, since some static media could be optimized and binaries are not stripped.
Static-Web-Server also supports serving brotli compressed content, which could help shrinking the image even more.
@oxcabe what were the two lines?
using `pkgsStatic` to statically link to musl libc instead of having 30MB of glibc in the Docker layer.