Files
nixos-router/modules/router/dns.nix
T
nilsandClaude Sonnet 5.5 84f6002d05 Add WireGuard, DNS and more to the web UI and router-setup
Web UI: a WireGuard page (tunnels, peers, keys made on the router, client
configs with a QR code, import of wg-quick configs, live status), a DNS page
(records, wildcards, forward zones), interface dropdowns with VLAN devices,
IPv6 options and a local domain. router-setup asks about VLANs, IPv6 and
the domain. The router publishes its own name in DNS and hands the local
domain to DHCP clients; wg and jq are on the router. Includes a script
that takes over antigua's tunnels.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
2026-10-29 23:53:34 +01:00

113 lines
4.4 KiB
Nix

{ config, lib, ... }:
let
cfg = config.router;
lans = lib.attrValues cfg.lans;
ipv4 = (import ./lib.nix { inherit lib; }).ipv4;
# Tunnels that opted into DNS: listen on each of their addresses.
dnsAddrs = lib.concatMap (w: w.addresses) (lib.filter (w: w.dns) (lib.attrValues cfg.wireguard));
addrOf = a: builtins.head (lib.splitString "/" a);
# IPv4 gets the proper network address; IPv6 netblocks are masked by Unbound.
netOf =
a:
let
parts = lib.splitString "/" a;
in
if lib.hasInfix ":" a then
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;
# The router's own name resolves to its addresses on the LANs (and their
# ULAs), so clients can reach it by name; with a domain also bare, for
# clients that have no search domain.
selfName = config.networking.hostName;
selfAddrs =
map (l: l.ipv4.address) lans
++ map (l: addrOf l.ipv6.address) (lib.filter (l: l.ipv6.address != null) lans);
# Unless dns.hosts names the router itself.
selfDefined = lib.hasAttr selfName dns.hosts || lib.hasAttr (fqdn selfName) dns.hosts;
selfRecord = lib.optional (!selfDefined) (lib.nameValuePair (fqdn selfName) selfAddrs);
# (No reverse record for the bare name: the address has one already.)
selfBare = lib.optional (!selfDefined && fqdn selfName != selfName) (
lib.nameValuePair selfName selfAddrs
);
records =
selfRecord
++ 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 ++ selfBare);
# 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 {
services.unbound = {
enable = true;
settings = {
server = {
interface = [
"127.0.0.1"
]
++ map (l: l.ipv4.address) lans
++ map (l: addrOf l.ipv6.address) (lib.filter (l: l.ipv6.address != null) lans)
++ map addrOf dnsAddrs;
# LAN addresses are assigned by networkd, possibly after Unbound starts.
ip-freebind = true;
access-control = [
"127.0.0.0/8 allow"
]
++ map (l: "${l.ipv4.subnet} allow") lans
++ map (l: "${l.ipv6.address} allow") (lib.filter (l: l.ipv6.address != null) lans)
++ map (a: "${netOf a} allow") dnsAddrs
++ map (n: "${n} allow") dnsPeerNets;
local-zone =
lib.optional (domain != null) "${domain}. static"
# The router's bare name is a zone of its own (not under the domain).
++ lib.optional (!lib.hasInfix "." selfName) "${selfName}. transparent"
++ 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 != [ ] || 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;
};
};
};
};
}