Add local DNS records, DHCP reservations and an installer ISO
DNS and DHCP (needed to replace a dnsmasq-based router): - router.dns.domain/hosts/overrides/forwardZones: authoritative local domain, split-horizon host records with reverse entries, wildcard overrides and per-zone forwarders (private zones skip DNSSEC). - lans.<n>.dhcp.reservations: Kea host reservations, published in DNS. - Tunnels with dns = true admit what their peers route, so a site-to-site peer without a transfer address can use the resolver. Installer: - router-install wipes a disk, creates an ESP and an ext4 root and installs a prebuilt system with its secrets; nothing is built on the box. installed-layout is the shared disk layout contract. - lib.mkInstaller builds an ISO for a given nixosSystem; packages.installer-example is the example's ISO. Covered by the dns and installer VM tests (the latter installs onto an empty disk and boots it under UEFI), wireguard DNS and assertion cases. PLAN.md records the remaining gaps for replacing the hand-written router. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,12 @@ let
|
||||
lans = lib.attrValues lansByName;
|
||||
|
||||
validIp = s: ipv4.parse s != null;
|
||||
hostnameOk = s: builtins.match "[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?" s != null;
|
||||
# A DNS name: labels of letters, digits, dashes and underscores.
|
||||
dnsNameOk =
|
||||
s:
|
||||
builtins.match "[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?(\\.[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?)*" s
|
||||
!= null;
|
||||
|
||||
# Assertions about a single LAN; skipped (reported once) when its addresses
|
||||
# do not even parse, so we never feed null into the integer helpers.
|
||||
@@ -58,6 +64,60 @@ let
|
||||
message = "router: ${where}.dhcp.pool contains the router's own address ${lan.ipv4.address}.";
|
||||
}
|
||||
]
|
||||
)
|
||||
++ lib.concatLists (
|
||||
lib.mapAttrsToList (
|
||||
host: r:
|
||||
let
|
||||
rwhere = "${where}.dhcp.reservations.${host}";
|
||||
rok = validIp r.address;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = hostnameOk host;
|
||||
message = "router: ${rwhere}: `${host}` is not a valid hostname (letters, digits and dashes).";
|
||||
}
|
||||
{
|
||||
assertion = builtins.match "([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}" r.mac != null;
|
||||
message = "router: ${rwhere}.mac `${r.mac}` is not a MAC address like aa:bb:cc:dd:ee:ff.";
|
||||
}
|
||||
{
|
||||
assertion = rok;
|
||||
message = "router: ${rwhere}.address `${r.address}` is not a valid IPv4 address.";
|
||||
}
|
||||
{
|
||||
assertion = lan.dhcp.enable;
|
||||
message = "router: ${rwhere}: reservations need dhcp.enable on the LAN.";
|
||||
}
|
||||
]
|
||||
++ lib.optionals (rok && addrOk) [
|
||||
{
|
||||
assertion = ipv4.inSubnet net (ipv4.parse r.address);
|
||||
message = "router: ${rwhere}.address ${r.address} is not inside ${lan.ipv4.subnet}.";
|
||||
}
|
||||
{
|
||||
assertion = r.address != lan.ipv4.address;
|
||||
message = "router: ${rwhere}.address is the router's own address.";
|
||||
}
|
||||
]
|
||||
) lan.dhcp.reservations
|
||||
)
|
||||
++ (
|
||||
let
|
||||
rs = lib.attrValues lan.dhcp.reservations;
|
||||
macs = map (r: lib.toLower r.mac) rs;
|
||||
addrs = map (r: r.address) rs;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.length macs == lib.length (lib.unique macs);
|
||||
message = "router: ${where}.dhcp.reservations: two reservations share a MAC address.";
|
||||
}
|
||||
{
|
||||
assertion = lib.length addrs == lib.length (lib.unique addrs);
|
||||
message = "router: ${where}.dhcp.reservations: two reservations share an address.";
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
wgs = lib.attrValues cfg.wireguard;
|
||||
@@ -234,6 +294,50 @@ let
|
||||
}
|
||||
];
|
||||
|
||||
dnsCfg = cfg.dns;
|
||||
anyIp = s: validIp s || v6Cidr "${s}/128";
|
||||
dnsAssertions =
|
||||
let
|
||||
names =
|
||||
lib.attrNames dnsCfg.hosts ++ lib.attrNames dnsCfg.overrides ++ lib.attrNames dnsCfg.forwardZones;
|
||||
recordSets = dnsCfg.hosts // dnsCfg.overrides;
|
||||
shortNames = lib.filter (n: !(lib.hasInfix "." n)) (lib.attrNames dnsCfg.hosts);
|
||||
reservationNames = lib.concatMap (l: lib.attrNames l.dhcp.reservations) lans;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.all dnsNameOk names && (dnsCfg.domain == null || dnsNameOk dnsCfg.domain);
|
||||
message = "router.dns: a name in domain/hosts/overrides/forwardZones is not a valid DNS name.";
|
||||
}
|
||||
{
|
||||
assertion = lib.all (as: as != [ ] && lib.all anyIp as) (lib.attrValues recordSets);
|
||||
message = "router.dns: every host/override needs at least one valid IPv4 or IPv6 address.";
|
||||
}
|
||||
{
|
||||
assertion = lib.all (as: as != [ ] && lib.all anyIp as) (lib.attrValues dnsCfg.forwardZones);
|
||||
message = "router.dns.forwardZones: every zone needs at least one valid resolver address.";
|
||||
}
|
||||
{
|
||||
assertion = dnsCfg.domain != null || shortNames == [ ];
|
||||
message = "router.dns.hosts: `${lib.concatStringsSep ", " shortNames}` has no dot, so router.dns.domain must be set.";
|
||||
}
|
||||
{
|
||||
assertion =
|
||||
lib.length (lib.attrNames dnsCfg.hosts) == lib.length (
|
||||
lib.unique (
|
||||
map (n: if lib.hasInfix "." n || dnsCfg.domain == null then n else "${n}.${dnsCfg.domain}") (
|
||||
lib.attrNames dnsCfg.hosts
|
||||
)
|
||||
)
|
||||
);
|
||||
message = "router.dns.hosts: two names are the same once placed in the domain.";
|
||||
}
|
||||
{
|
||||
assertion = lib.all (n: !(lib.elem n (lib.attrNames dnsCfg.hosts))) reservationNames;
|
||||
message = "router.dns.hosts: a name is also a DHCP reservation, which already publishes it.";
|
||||
}
|
||||
];
|
||||
|
||||
overlapsLan = n: lib.any (l: ipv4.overlaps n l) lanNets;
|
||||
|
||||
wgAssertions =
|
||||
@@ -347,6 +451,7 @@ in
|
||||
}
|
||||
]
|
||||
++ lib.concatLists (lib.mapAttrsToList perLan lansByName)
|
||||
++ dnsAssertions
|
||||
++ lib.concatMap ruleAssertions cfg.firewall.rules
|
||||
++ [
|
||||
{
|
||||
|
||||
@@ -27,6 +27,11 @@ in
|
||||
inherit (l.ipv4) subnet;
|
||||
interface = l.device;
|
||||
pools = [ { pool = "${l.dhcp.pool.start} - ${l.dhcp.pool.end}"; } ];
|
||||
reservations = lib.mapAttrsToList (hostname: r: {
|
||||
hw-address = lib.toLower r.mac;
|
||||
ip-address = r.address;
|
||||
inherit hostname;
|
||||
}) l.dhcp.reservations;
|
||||
option-data = [
|
||||
{
|
||||
name = "routers";
|
||||
|
||||
+47
-6
@@ -17,6 +17,37 @@ let
|
||||
a
|
||||
else
|
||||
ipv4.cidr (builtins.head parts) (lib.toInt (builtins.elemAt parts 1));
|
||||
|
||||
dns = cfg.dns;
|
||||
inherit (dns) domain;
|
||||
fqdn = name: if lib.hasInfix "." name || domain == null then name else "${name}.${domain}";
|
||||
isV6 = lib.hasInfix ":";
|
||||
rrOf = a: if isV6 a then "AAAA" else "A";
|
||||
|
||||
# name -> addresses: explicit hosts plus DHCP reservations (A records only).
|
||||
reservations = lib.concatMap (
|
||||
l: lib.mapAttrsToList (n: r: lib.nameValuePair n [ r.address ]) l.dhcp.reservations
|
||||
) lans;
|
||||
records =
|
||||
lib.mapAttrsToList (n: a: lib.nameValuePair (fqdn n) a) dns.hosts
|
||||
++ map (r: lib.nameValuePair (fqdn r.name) r.value) reservations
|
||||
++ lib.mapAttrsToList (n: a: lib.nameValuePair n a) dns.overrides;
|
||||
|
||||
# Unbound reads these as one quoted argument.
|
||||
localData = lib.concatMap (r: map (a: ''"${r.name}. ${rrOf a} ${a}"'') r.value) records;
|
||||
# Reverse records for hosts and reservations (not for wildcard overrides).
|
||||
ptrs = lib.concatMap (r: map (a: ''"${a} ${r.name}."'') r.value) (
|
||||
lib.filter (r: !(lib.hasAttr r.name dns.overrides)) records
|
||||
);
|
||||
privateZones = lib.attrNames dns.forwardZones ++ lib.optional (domain != null) domain;
|
||||
|
||||
# Peers of tunnels that use the resolver may be remote networks with no
|
||||
# address of their own on the tunnel (site-to-site); allow what they route.
|
||||
dnsPeerNets = lib.filter (n: !lib.hasSuffix "/0" n) (
|
||||
lib.concatMap (w: lib.concatMap (p: p.allowedIPs) (lib.attrValues w.peers)) (
|
||||
lib.filter (w: w.dns) (lib.attrValues cfg.wireguard)
|
||||
)
|
||||
);
|
||||
in
|
||||
{
|
||||
config = lib.mkIf cfg.enable {
|
||||
@@ -31,16 +62,26 @@ in
|
||||
"127.0.0.0/8 allow"
|
||||
]
|
||||
++ map (l: "${l.ipv4.subnet} allow") lans
|
||||
++ map (a: "${netOf a} allow") dnsAddrs;
|
||||
++ map (a: "${netOf a} allow") dnsAddrs
|
||||
++ map (n: "${n} allow") dnsPeerNets;
|
||||
local-zone =
|
||||
lib.optional (domain != null) "${domain}. static"
|
||||
++ map (n: "${n}. redirect") (lib.attrNames dns.overrides);
|
||||
local-data = localData;
|
||||
local-data-ptr = ptrs;
|
||||
domain-insecure = privateZones ++ lib.attrNames dns.overrides;
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (cfg.dns.forwarders != [ ]) {
|
||||
forward-zone = [
|
||||
{
|
||||
// lib.optionalAttrs (cfg.dns.forwarders != [ ] || dns.forwardZones != { }) {
|
||||
forward-zone =
|
||||
lib.mapAttrsToList (name: addrs: {
|
||||
inherit name;
|
||||
forward-addr = addrs;
|
||||
}) dns.forwardZones
|
||||
++ lib.optional (cfg.dns.forwarders != [ ]) {
|
||||
name = ".";
|
||||
forward-addr = cfg.dns.forwarders;
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@@ -156,6 +156,29 @@ in
|
||||
example = "192.168.10.199";
|
||||
};
|
||||
};
|
||||
reservations = mkOption {
|
||||
type = types.attrsOf (
|
||||
types.submodule {
|
||||
options = {
|
||||
mac = mkOption {
|
||||
type = types.str;
|
||||
example = "a8:a1:59:0b:4b:97";
|
||||
description = "Hardware address of the client.";
|
||||
};
|
||||
address = mkOption {
|
||||
type = types.str;
|
||||
example = "192.168.10.20";
|
||||
description = "Address handed to the client; must lie inside the LAN.";
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
default = { };
|
||||
description = ''
|
||||
Fixed leases, keyed by hostname. The name is also sent to the
|
||||
client and published in DNS (under `router.dns.domain` if set).
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
ipv6.pd = mkOption {
|
||||
@@ -415,6 +438,55 @@ in
|
||||
);
|
||||
};
|
||||
|
||||
dns.domain = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
example = "home.example.org";
|
||||
description = ''
|
||||
Local domain. It is authoritative on the router: names below it that
|
||||
have no record answer NXDOMAIN and are never forwarded. Short names in
|
||||
`dns.hosts` and DHCP reservation names live in it.
|
||||
'';
|
||||
};
|
||||
|
||||
dns.hosts = mkOption {
|
||||
type = types.attrsOf (types.listOf types.str);
|
||||
default = { };
|
||||
example = {
|
||||
nas = [ "192.168.10.20" ];
|
||||
"git.example.org" = [
|
||||
"10.43.0.50"
|
||||
"fd00::50"
|
||||
];
|
||||
};
|
||||
description = ''
|
||||
Local records: name to IPv4/IPv6 addresses. A name without a dot is
|
||||
placed in `dns.domain`; a full name overrides public DNS for that
|
||||
name only (split horizon). Reverse records are created as well.
|
||||
'';
|
||||
};
|
||||
|
||||
dns.overrides = mkOption {
|
||||
type = types.attrsOf (types.listOf types.str);
|
||||
default = { };
|
||||
example = {
|
||||
"k8s.example.org" = [ "10.43.0.50" ];
|
||||
};
|
||||
description = "Like `dns.hosts`, but the name and everything below it answer with these addresses (wildcard).";
|
||||
};
|
||||
|
||||
dns.forwardZones = mkOption {
|
||||
type = types.attrsOf (types.listOf types.str);
|
||||
default = { };
|
||||
example = {
|
||||
"branch.example.org" = [ "10.1.0.254" ];
|
||||
};
|
||||
description = ''
|
||||
Zones answered by other resolvers (e.g. across a tunnel). DNSSEC
|
||||
validation is skipped for them: they are private zones.
|
||||
'';
|
||||
};
|
||||
|
||||
dns.forwarders = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
|
||||
Reference in New Issue
Block a user