How to export container built by nixos?

I see nixos-container link here, how to export the container as image like docker, and run on other os?

These are not oci images, so they cannot be exported into docker or anything of the likes. They are more-or-less raw sysroots brought to life with systemd-nspawn.

If you’re really set on doing this, you can probably pick out the configuration from the nixos module and create a sysroot with nix build, and then figure out how to run it with systemd-nspawn.

I hope it’s somewhat obvious that I don’t recommend this. What is your use case? Maybe there’s a better tool for what you’re trying to do.


There is GitHub - cloudwatt/nix-container-images: Write container images as NixOS machines which hijacks the NixOS module system to produce docker-compatible images. It replaces systemd with s6 for example. It’s not going to work with all services but it might be enough for what you want to do.


Use case:

I am currently use docker to build my development enviroment, including tmux, nvim, git, xxplugins, this is mainy for code editing, link here.
The image is based on alpine linux, I thought normal distribution based container image is not such reproducible, and nix/nixos is much more suitable for this case, so currently I am trying to use nix/nixos to build my development enviroment. I hope to export it as a container image and run on other os.

So in summary:

  • nix/nixos makes the development enviroment reproducible
  • container image makes the development enviroment portable, since my other os may not be nix/nixos based linux

The project @zimbatm suggests is pretty much exactly what you want then (or perhaps BuildStream, but that’s very non-nix) :slight_smile:

BuildStream is another story, I will try @zimbatm solution.