Networking isolated NixOS containers with auto-generated hostnames

I wanted to ask if there were some existing way to (A) deploy NixOS containers with private networking enabled (i.e. privateNetwork = true;) while (B) making them all reachable via hostnames autogenerated from the container names.

In other words, what I’d like is that if I define the following configuration:

{ containers = {
    foo = { autoStart = true; privateNetwork = true; config = …; };
    bar = { autoStart = true; privateNetwork = true; config = …; };
  };
}

… that there would be some other additional option(s) I could set that would allow the host or any of these two guest containers to connect to foo using the hostname foo and to connect to bar using the hostname bar. In particular, I would like to able to do this without having to think about or specify any IP addresses, network interface names, or any other magic strings.

Currently the way I work around this is to disable private networking (privateNetwork = false;) and run the NixOS containers on the host network, but that’s not ideal either (I’d like some form of network isolation in between “everything is completely isolated” and “everything runs on the same network”).

I’m willing to build a feature like this, but I wanted to first check that I’m not missing something really obvious that already exists.

4 Likes