NixOS in container (docker, lxc, whatever)

I have the following scenario:

I have a host that is not NixOS, and I would like to have a container on that host with NixOS to experiment with, including running NixOS modules based services in the guest.

From previous searches/queries, I was left with the impression that that’s either

  • impossible
  • pointless
  • doesn’t work well
    due to some interactions with things like host vs guest systemd.

Did I misunderstand, and is there some reasonable way to do this?

2 Likes

There are definitely Docker images for NixOS, so someone must be using it that way.

I also run several NixOS hosts as LXC containers on Proxmox (Debian-based) hosts, and it works just fine. I’m basically treating them like VMs but lighter weight.

You can use https://github.com/nix-community/nixos-generators to create containers from NixOS configurations.

If you don’t need systemd, services etc, you can also use the nixpkgs dockerTools (similar for other container formats) to build a container image from Nix without going full-NixOS.

2 Likes

I’m trying to get GitHub - cloudwatt/nix-container-images: Write container images as NixOS machines to work with newer nixos (works on 19.03), but it proves to be a bit… tedious :sweat_smile:

The main issue is that NixOS is heavily reliant on systemd, and the OCI spec isn’t really designed around that kind of workflow. It’s possible someone has got it working, but you can easily spin up a NixOS container using the official nixos-containers cli. It uses systemd-nspawn to manage the container instead of docker, so not sure if that breaks your usecase, but that is the easiest way.

1 Like

Is there good documentation and a tutorial for nixos-containers? Including the imperative CLI tool?