No, from the nix store size perspective both are equivalent. buildLayeredImage
doesn’t actually create the layers independently, it creates a script that will build them all one-by-one, and then runs that script to produce the final image. In either case you will have to store the full image in the nix store, and all its build/input components will also take up space in the store (until garbage collected, assuming they aren’t used by anything else). Nix treats the final image as one archive, completely independent from its inputs.
Once imported into docker, however, the layers can be de-duplicated in the docker storage space, which is where the layered image can help.
I’ve not suggested anything for your situation, just pointed out that the nix image itself also isn’t built with a Dockerfile
, since you were asking where it is. I’m not aware of anyone writing a set of best practices either, otherwise I would have pointed it out. Personally, I wouldn’t use docker to build docker images, it’s an awful build system.
That said, if I had to do anything involving dockerfiles and nix, that’s indeed pretty much the only way to start.
After that it depends on the use case. Using garbage collection and splitting layers intelligently may come into play, but heavily depends on what it is you’re aiming to do. If for example you want to build something with nix inside a docker context, but not actually deploy nix into the final image, you may want to create a split image and export the nix build as a nix bundle into a google distroless container. This’d mean you would want to maximize the intermediate layers to maximize build caching. It’s all very awkward and use-case specific, docker is not a good build system.
If you actually build images with FROM docker.io/nixos/nix
you aren’t persisting the nix store, so you lose most of nix’ features. Using nix to build the images directly is pretty much always best.
There’s a pretty good chance this is an XY problem anyway, if you tell us what you want to achieve we may be able to give better advice 