Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Declarative container management

Declarative containers have existed in NixOS for quite some time under the containers option. This repository provides RFC108-style containers which use systemd-networkd instead of the classic script based networking.

Defining your container configuration

You should have already added the hypervisor module to your system during installation.

From here, you can simply declare NixOS containers in your host configuration like so:

{
  nixos.containers.mycontainer = {
    # This option houses the actual system configuration.
    config = {
      services.nginx.enable = true;
      networking.firewall.allowedTCPPorts = [ 80 ];
    };

    bindMounts = [
      "/var/lib/host/path:/var/lib/container/path"
    ];
  };
}

Upon nixos-rebuild, the container will be started. You can verify this with nixos-nspawn list or machinectl list.

Further reading