aboutsummaryrefslogtreecommitdiffstats
path: root/host.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 16:57:46 +0000
committertv <tv@krebsco.de>2026-11-16 18:08:39 +0100
commitf17f88f1f53247ec86a472105cf94cfc664cd1b8 (patch)
treeeb7b564820fc3de989906865a4f2078bb287bc92 /host.nix
parent2c37475d89b75d382728e7b33c01892770b15a26 (diff)
get guest addresses via DHCP, one tap device per VM
host.nix now makes a bridge fcbr0 with a number of tap devices (default 8) and runs a dnsmasq of its own for DHCP. The run script takes the first tap device it can lock and derives the guest MAC from its number. The guest uses DHCP, with the MAC as client identifier, as the machine-id (and thus the default DUID) is the same in every VM. host.nix now takes { user, taps ? 8 } instead of just the user.
Diffstat (limited to 'host.nix')
-rw-r--r--host.nix48
1 files changed, 41 insertions, 7 deletions
diff --git a/host.nix b/host.nix
index 3368677..378a888 100644
--- a/host.nix
+++ b/host.nix
@@ -1,15 +1,49 @@
-# Host side of the guest's network: a tap device owned by the user running
-# the VM, NATed to the outside world.
-# usage (in the host's configuration.nix): imports = [ (import ./host.nix "tv") ];
-user: {
- networking.interfaces.fctap0 = {
+# Host side of the guests' network: a bridge with tap devices fctap0,
+# fctap1, ... owned by the user running the VMs, a DHCP server, and NAT to
+# the outside world. Each running VM takes one tap device, so taps is the
+# number of VMs that can run at the same time.
+# usage (in the host's configuration.nix):
+# imports = [ (import ./host.nix { user = "tv"; }) ];
+# The host's firewall has to let the guests in on fcbr0: DHCP (udp 67)
+# as input, and anything from fcbr0 (plus the return traffic) as forward.
+{ user, taps ? 8 }: { lib, pkgs, ... }: let
+ tapNames = map (i: "fctap${toString i}") (lib.range 0 (taps - 1));
+in {
+ networking.interfaces = lib.genAttrs tapNames (_: {
virtual = true;
virtualType = "tap";
virtualOwner = user;
- ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }];
+ }) // {
+ fcbr0.ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }];
};
+ networking.bridges.fcbr0.interfaces = tapNames;
+
networking.nat = {
enable = true;
- internalInterfaces = [ "fctap0" ];
+ internalInterfaces = [ "fcbr0" ];
+ };
+
+ # A dnsmasq of its own, so it doesn't get mixed up with one the host might
+ # run otherwise. DHCP only (port=0 disables DNS); bind-dynamic copes with
+ # fcbr0 not being there yet.
+ systemd.services.fcbr0-dhcp = {
+ wantedBy = [ "multi-user.target" ];
+ after = [ "network.target" ];
+ serviceConfig = {
+ RuntimeDirectory = "fcbr0-dhcp";
+ Restart = "on-failure";
+ ExecStart = lib.escapeShellArgs [
+ "${pkgs.dnsmasq}/bin/dnsmasq"
+ "--keep-in-foreground"
+ "--conf-file=/dev/null"
+ "--port=0"
+ "--bind-dynamic"
+ "--interface=fcbr0"
+ "--dhcp-authoritative"
+ "--dhcp-range=172.16.0.2,172.16.0.254,1h"
+ "--dhcp-option=option:dns-server,9.9.9.9"
+ "--dhcp-leasefile=/run/fcbr0-dhcp/leases"
+ ];
+ };
};
}