hive-network

The bridge every container on a host attaches to, and the private-netns isolation of agent and CI containers behind it. It comes up on any host that runs agents or swarm services: services.hyperhive.network.enable defaults to off, and the modules that need the bridge set it with mkDefault true — the hive controller, CI, the hive collector, and the gateway's resolver, which every swarm service turns on. Configured via services.hyperhive.network.*.

Agent containers always run in a private netns.

Network map

One picture of the whole hive — two planes: the few infra containers that bind host ports (the secret store and the queue) share the host netns; every other container (agents, CI, and the service containers such as the forge and matrix) gets a private netns behind the bridge. The unix-socket control plane rides the VFS, and none of that touches it.

                        internet
                           │  uplink NIC — NAT MASQUERADE for the
                           │  bridge subnet (10.42.0.0/24 default)
┌──────────────────────────┴─────────────────────────────────────────┐
│ host netns — the host itself plus the gateway                      │
│                                                                    │
│   nginx :80/:443                                  [hive-gateway]   │
│   dnsmasq 10.42.0.1:53 (DNS) + :67 (DHCP)         [hive-gateway]   │
│   squid 10.42.0.1:3128 (egress proxy)             [hive-squid]     │
│   hive-c0re dashboard 127.0.0.1:7000              (host service)   │
│                                                                    │
│                  hive-br0   10.42.0.1/24                           │
│              ┌──────────┼──────────────┐                           │
└──────────────┼──────────┼──────────────┼───────────────────────────┘
          vb-h-<a>    vb-h-<b>      vb-<runner>     veth pairs
               │          │              │
          ┌────┴────┐ ┌───┴─────┐ ┌──────┴──┐   one private netns
          │ agent a │ │ agent b │ │ runner  │   each; eth0 leases
          │  eth0   │ │  eth0   │ │  eth0   │   from the DHCP pool
          └─────────┘ └─────────┘ └─────────┘
container netns IPv4 listens / reached via
gateway (host) host host addresses nginx :80/:443 (every vhost); dnsmasq bridgeIp:53 + DHCP :67 on the bridge
egress proxy (host) host bridgeIp squid bridgeIp:3128; agents with a credential send HTTP and HTTPS through it
hive-forge private, veth on bridge DHCP pool forgejo on UDS /run/hive-forge/forgejo.sock → the forge.<swarm-domain> vhost; no SSH
hive-matrix private, veth on bridge DHCP pool tuwunel on UDS /run/hive-matrix/tuwunel.sock → the matrix vhost; out to store, SSO, peers
swarm-ci-runner private, veth on bridge DHCP pool outbound only (runner → forge); no inbound surface
swarm-review-classifier private, veth on bridge DHCP pool outbound only (classifier → queue, secret store, swarm collector); no inbound surface
h-<agent> private, veth on bridge DHCP pool web UI via UDS /run/hive-agent/<name> → nginx sub-path; in-container UI port hashed 8100–8999

The flows, end to end:

Container shape (where dnsmasq lives)

dnsmasq runs on the host itself, next to nginx — both come from the gateway module, one front door for DNS and HTTP. Its bind-interfaces listener sits on bridgeIp, on the host's bridge interface.

Configuration

{
  services.hyperhive = {
    swarm = {
      domain = "myswarm.example";
      hives.firsthive = { }; # -> domain = firsthive.myswarm.example
    };
    deploy.hive-controller = {
      enable = true;
      hiveName = "firsthive";
    };
    # network.bridgeIp = "10.42.0.1"; # default
  };
}

The hive domain (services.hyperhive.domain, which the resolver answers for) comes from this hive's entry in the swarm directory: swarm.hives.<hiveName>.domain, default <hiveName>.<swarm.domain>. Eval fails until swarm.domain and that entry exist (2-handbook/swarm/hive-directory.md).

Bridge addressing

Default subnet is 10.42.0.0/24, host-side gateway at 10.42.0.1. RFC 1918 space, unlikely to clash with operator's existing setup; override bridgeIp + bridgePrefixLength if a different range is already in use. /24 gives 254 usable per-agent addresses — enough for any single-host hive; bigger swarms or tighter addressing schemes pick their own.

Resolver behaviour

dnsmasq is authoritative for the hive's own zone (<hive-domain>) plus whatever swarm-service names this host contributes via gateway.localNames — forge.<swarm-domain> and chat.<swarm-domain> (matrix) when this host runs those services, auth.<swarm-domain> when it runs authelia, and hive-collector.hive on every hive — answering each with the bridge IP (where nginx is reachable, and where the hive's own collector listens). Note forge and matrix are swarm-domain names, not sub-domains of <hive-domain>: a swarm runs one forge and one homeserver, so their names belong to the swarm rather than to whichever hive happens to host them.

Every domain answered this way is also local= for dnsmasq, so the other record types (AAAA, TXT, MX, HTTPS) for those names get an empty answer from dnsmasq itself instead of waiting on the host's upstream resolver. Without it, dnsmasq 2.86 and later send them upstream, and a host whose upstream doesn't answer leaves each lookup to its client's own timeout.

dnsmasq forwards everything else to the host's own resolvers: it runs on the host and reads the host's /etc/resolv.conf directly. Containers don't need to know the upstream — they query the bridge IP and dnsmasq does the right thing per-name.

No fallback server= exists, by design: dnsmasq queries all known upstreams in parallel, so a hardcoded public resolver would take a share of normal traffic, not just cover the gap.

dnsmasq runs on the host and reads the host's /etc/resolv.conf directly, so a network change (new router, new lease, laptop moving networks) reaches it the moment openresolv rewrites the file. Nothing needs synchronising, and no unit watches for it.

bind-interfaces + interface = [ bridgeName "lo" ] means the listener only accepts queries from the bridge interface (plus lo for container health-checks). External hosts can't reach it — no DNS-amplification surface even when the operator opens port 80 for gateway HTTP.

resolveLocalQueries = false keeps dnsmasq out of the host's own resolution stack — the host's resolver (systemd-resolved, plain glibc nss, dnscrypt-proxy, etc.) keeps doing whatever the operator configured. The hive resolver is purely for inbound queries from agent containers.

Firewall posture

networking.firewall.interfaces.<bridge>.allowedUDPPorts = [ 53 67 ] networking.firewall.interfaces.<bridge>.allowedTCPPorts = [ 53 80 443 ]

The host firewall is the only firewall. The swarm service containers that share the host netns (bao, the queue) run with networking.firewall.enable = false (nix/container-modules/swarm-container.nix): a NixOS firewall inside a shared-netns container runs against the host ruleset — at container boot its firewall-start flushes the nixos-fw chains, rebuilds them from the container's (empty) port list, and deletes the host's nixos-nat-* chains without recreating them, silently wiping the bridge holes above plus the agents' NAT. Private-netns containers (every container with privateNetwork = true: agents and the service containers on the bridge) keep their own firewall — it's scoped to their namespace.

Reaching host services (exposeHostPorts)

By default agents can only reach the host on 80/443 (+53 DNS), so a host-side service on another port — for example a dev OTLP collector you want agents to reach directly — is unreachable. (hyperhive's own services need none of this: a module that means to be reachable from agents opens its own port here, which the hive collector and nats.enable both do. Note that deploy.swarm-otel.upstream.endpoint is the upstream, which no agent ever dials. See docs/4-implementation/scheduler/observability.md.)

services.hyperhive.network.exposeHostPorts = [ 4318 ]; opens each listed TCP port P on the bridge-interface allowedTCPPorts, so an agent can connect to <bridgeIp>:P (for a dev OTLP collector, point its endpoint at http://<bridgeIp>:4318, default http://10.42.0.1:4318). The hive's own collector needs none of this: agents dial it by otel.collector.host.

This is firewall-only: the host service must bind an address reachable from the bridge — 0.0.0.0 or the bridge IP — not loopback only. The bridge→127.0.0.0/8 DROP rule (below) still applies, so a service bound to 127.0.0.1 only stays unreachable; rebind it to 0.0.0.0.

The port is reachable by every agent on the bridge subnet (like DNS/gateway), so only expose services safe for any agent to reach.

Egress proxy (squid)

Every hive host runs squid in the host netns, listening on <bridgeIp>:3128 (nix/host-modules/hive-squid.nix). The port is open on the bridge interface only. An agent sends its outbound HTTP and HTTPS through it once its hive says the proxy is ready and it has fetched its proxy credential (see Agents and the proxy below). A program that ignores the proxy variables still reaches squid when it speaks HTTP on port 80 or TLS on port 443 and its agent is restricted or in log mode: the host redirects that traffic into squid's intercept ports (see Intercepted web traffic below). Everything else goes straight out through the NAT, as before: the unrestricted agents, the service containers and every other port and protocol. A hive controller host refuses the direct connections of the restricted and log agents (egress.forward = "reject", the default while the intercept is on); egress.forward = "log" only logs them (see Agent bridge egress log below).

Squid filters on the destination hostname: a CONNECT target or a plain-HTTP host. It never decrypts TLS. The one TLS key it loads is a throwaway that the unit generates at each start, that signs nothing and that no agent trusts. It caches nothing.

Intercepted web traffic

A program that ignores the proxy variables still gets squid's policy for its web traffic. In the inet hive_egress table (nix/host-modules/hive-egress.nix) the intercept chain, at prerouting, redirects IPv4 TCP port 80 to <bridgeIp>:3129, an intercept listener of squid, and port 443 to <bridgeIp>:3131, the ClientHello relay (see ECH below), which sends the connection on to squid's other intercept listener, <bridgeIp>:3130. Squid reads the original destination from conntrack. The chain exists where the host runs squid (services.hyperhive.deploy.hive-controller.egress.intercept.enable).

Container isolation

Each agent container runs in a private network namespace with a dedicated veth pair attached to the bridge. The following table summarises what the nix side sets up unconditionally:

effect mechanism
IP forwarding boot.kernel.sysctl."net.ipv4.ip_forward" = 1
Internet NAT networking.nat { enable = true; internalInterfaces = [ bridgeName ]; } — MASQUERADE on packets leaving via any external NIC
Loopback DROP nix/host-modules/hive-egress.nix — own nftables table inet hive_egress (whichever firewall the host runs): drops bridge → 127.0.0.0/8 and ::1 traffic; the kernel default route_localnet=0 is the first layer
Forward log nix/host-modules/hive-egress.nix — the same table's forward chain logs and counts what the host routes from the bridge to the uplink, and refuses it with egress.forward = "reject" (see Agent bridge egress log below)
Source check nix/host-modules/hive-egress.nix — the bridge hive_egress table drops a frame whose source MAC or address differs from the sending container's own (see Agent bridge source check below)
Gateway access networking.firewall.interfaces.<bridge>.allowedTCPPorts = [ 80 443 ] — lets isolated agents (private netns, veth on bridge) reach nginx on the host
c0re signal HIVE_NETWORK_BRIDGE, HIVE_NETWORK_SUBNET in systemd.services.hive-c0re.environment — both required; hive-c0re refuses to start without them

HIVE_NETWORK_SUBNET is the host-side bridge IP + prefix (e.g. 10.42.0.1/24), not the canonical network address. The Rust side must normalise (bitwise-AND with mask) before subnet membership checks or address arithmetic.

What the Rust side does

hive-c0re reads HIVE_NETWORK_BRIDGE + HIVE_NETWORK_SUBNET and passes PRIVATE_NETWORK=1, LOCAL_ADDRESS= (empty), HOST_ADDRESS=<bridge-ip>, and HOST_BRIDGE=<bridgeName> when creating or updating containers: lifecycle::set_nspawn_flags hands them to hive-priv, which rewrites /etc/nixos-containers/<container>.conf. hive-c0re validates both variables once at daemon startup, not per container: they're process-global, so a missing or malformed value is a misconfigured daemon rather than one bad container, and failing at boot gives a single diagnostic instead of one per agent. No non-isolated mode exists to fall back to. hive-c0re leaves LOCAL_ADDRESS empty so the container's dhcpcd acquires an address from the bridge dnsmasq pool (networking.useDHCP = true in nix/agent-modules/network.nix). This applies to every agent container, manager included.

HOST_ADDRESS is the bridge gateway IP (the address part of HIVE_NETWORK_SUBNET, via lifecycle::bridge_gateway_ip — taken verbatim so a non-.1 operator override still resolves to wherever the bridge actually lives). It's load-bearing: nixos-container's container-side network setup only installs a default route (ip route add default via $HOST_ADDRESS) when HOST_ADDRESS is non-empty. In bridge mode nixos-container skips the host-side address/route setup, so writing it only affects the container's default route — without it the container comes up with an IP but no path off the bridge subnet (no internet, no api.anthropic.com).

How the isolated container gets its resolver

nixos-container copies the host's /etc/resolv.conf into the container at every start. The host resolver (for example 127.0.0.53 from systemd-resolved, or a LAN router) is unreachable from a private netns and isn't authoritative for the hive's own zones, so it's replaced with the bridge dnsmasq at boot. Because the copy happens on every start, it would clobber a declarative environment.etc."resolv.conf" — so the wiring is runtime:

Why isolation is safe: hive-c0re's control-plane sockets are unix domain sockets bind-mounted into containers, not network listeners — see the Control plane (no network) bullet under Network map above. PRIVATE_NETWORK=1 has no effect on a path that never touches the network stack.

The nix side also enables IP forwarding + NAT (agents reach the internet through the host) and drops bridge-subnet → loopback traffic from its own nftables table (inet hive_egress, nix/host-modules/hive-egress.nix, which also holds the bridge hive_egress table of Agent bridge source check; defence-in-depth against a compromised agent reaching the c0re dashboard HTTP at 127.0.0.1). Agents have no legitimate reason to reach the dashboard over loopback — the hive-c0re admin socket is a UDS, not TCP.

Agent bridge egress log

services.hyperhive.deploy.hive-controller.egress.forward sets what the forward chain of the inet hive_egress table does with traffic the host routes from the agent bridge to anywhere but the bridge. It takes accept, log or reject; the default is reject where the intercept is on (see The default below) and log elsewhere. In log mode the chain blocks nothing. It logs each new flow and counts every packet. In reject mode it also refuses the flow.

Agent bridge source check

Every container on the agent bridge is root in its own network namespace and shares one L2 segment with the others, so it can send with another container's MAC or address. services.hyperhive.deploy.hive-controller.egress.antiSpoof sets what the bridge hive_egress table (nix/host-modules/hive-egress.nix) does about it. It takes drop or log (the default, until the lease set survives a host suspend/resume). Two tables, not one: the bridge carries traffic between two ports, and the inet hooks never see it as coming from a port. Only a bridge-family hook at prerouting sees which veth a frame came in on, so the source check lives in that table and the host-side filtering stays in inet hive_egress.

Enforcement comes with later changes in the same series: the log only shows what such a change would cut.

Cross-references