{ config, lib, ... }: let cfg = config.router; dhcpLans = lib.filter (l: l.dhcp.enable) (lib.attrValues cfg.lans); ipv4 = (import ./lib.nix { inherit lib; }).ipv4; in { config = lib.mkIf (cfg.enable && dhcpLans != [ ]) { services.kea.dhcp4 = { enable = true; settings = { interfaces-config = { interfaces = map (l: l.device) dhcpLans; # Interfaces are configured asynchronously by networkd. re-detect = true; }; lease-database = { type = "memfile"; persist = true; name = "/var/lib/kea/dhcp4.leases"; }; valid-lifetime = 3600; # The ID keys persistent leases, so derive it from the network address: # stable when LANs are added or removed (an index would shift). subnet4 = map (l: { id = ipv4.networkOf (ipv4.parse l.ipv4.address) l.ipv4.prefixLength; inherit (l.ipv4) subnet; interface = l.device; pools = [ { pool = "${l.dhcp.pool.start} - ${l.dhcp.pool.end}"; } ]; option-data = [ { name = "routers"; data = l.ipv4.address; } { name = "domain-name-servers"; data = l.ipv4.address; } ]; }) dhcpLans; }; }; }; }