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?

1 Like

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 GitHub - nix-community/nixos-generators: Collection of image builders [maintainer=@Lassulus] 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.