Using nixos/nix as a builder in Docker

I’m trying to use nix as a builder in a Dockerfile. Building works really well, but my issue is when I get to running. When I try to run the Docker image, I get exec ./blog: no such file or directory. I’m just really confused as to what’s wrong, since I’m fairly sure this is a Nix specific issue and not a Docker one.

The files can be found here:

1 Like

Are you trying to build an application via Nix, then run that binary on a different system? For similar reasons why you can’t run precompiled binaries on NixOS, you will not easily take a dynamically-linked binary compiled with Nix and run it on a different system by itself. Try executing file ./blog or ldd ./blog, and you will likely find that the compiled binary refers to dynamic loader and dynamic libraries in /nix/store/...

There are a few possible solutions:

  1. Take the entire closure for this package (not just the executable) elsewhere via nix copy or similar
  2. Compile a completely static binary
  3. Use something like nix-bundle.
1 Like

Thank you, I decided I’ll just keep using nix for building and running. Is there any way to shrink the build image though?

Are you currently building your Docker image by basing it on NixOS/nix, and then invoking nix-build? If so, you will get a much leaner image using dockerTools. This way, you get an image with only what you asked for (the packages you specify and their dependencies). Not even Bash is included AFAIK.

1 Like