FYI: building Docker image with Nix

Assuming you have default.nix in the project directory and can build your project with nix-build. Also you have a Dockerfile:

FROM scratch # !!!
ADD out.tar.gz /
WORKDIR /data
RUN ["/out/bin/foo", "whatever", "to", "prepare", "the", "image"]
EXPOSE 8000
CMD ["/out/bin/bar", "--socket", ":8000"] # Just an example

Then build your Docker image from scratch with only Nix closure inside:

nix-build default.nix -o out \
&& tar czf out.tar.gz out $(nix-store -qR out) \
&& docker build . -t foo
3 Likes