Moving to NixOS - declarative containers with "network_mode: host"

NixOS noob here, I come for the declarative nature!

Trying to migrate my existing docker-compose set of containers to NixOS. One container has network_mode: host. The reason for this is that there are multiple Airplay clients that require a broad range of ports that are not easy to define.

What is the “Nix” way of accomplishing this? I’ve looked at both bridge and macvlans, but could not get either to work. How best to declare a container that either shares the host’s IP or has its own?

I never used declarative docker, so take my answer with care, but:

  • if you use oci-containers, you can do
virtualisation.oci-containers.containers.<name>.extraOptions = [ "--network=host" ]

which is even the given example here https://github.com/NixOS/nixpkgs/blob/cbc4211f0afffe6dfd2478a62615dd5175a13f9a/nixos/modules/virtualisation/oci-containers.nix#L229

  • if you use nspawn instead via containers.<name> = …, the wiki mentions

By default, if privateNetwork is not set, the container shares the network with the host, enabling it to bind any port on any interface.