authoritative-dns/authoritative-dns.nix

145 lines
3.9 KiB
Nix
Raw Permalink Normal View History

2023-10-04 09:31:17 -07:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.authoritative-dns;
zoneOpts = import ./zone-definition.nix { inherit lib; };
zoneToZonefile = import ./zone-to-zonefile.nix { inherit lib; };
2023-11-01 10:13:33 -07:00
reverseZonefile = import ./reverse-zone.nix { inherit pkgs; };
2023-10-04 09:31:17 -07:00
domainOpts = { name, ... }: {
options = with types; {
domain = mkOption {
type = str;
description = "Domain name.";
default = name;
};
ksk = {
key-file = mkOption {
type = nullOr str;
description =
"Key-signing key for this zone. DNSSEC disabled when null.";
default = null;
};
};
zone = mkOption {
type = submodule zoneOpts;
description = "Definition of network zone to be served.";
};
2023-11-01 10:13:33 -07:00
reverse-zones = mkOption {
type = listOf str;
description =
"List of subnets for which to generate reverse lookup zones.";
default = [ ];
};
2023-11-09 10:08:32 -08:00
notify = {
ipv4 = mkOption {
type = listOf str;
description = "List of IPv4 addresses to notify of changes.";
default = [ ];
};
ipv6 = mkOption {
type = listOf str;
description = "List of IPv6 addresses to notify of changes.";
default = [ ];
};
};
2023-10-04 09:31:17 -07:00
};
};
in {
options.services.authoritative-dns = with types; {
enable = mkEnableOption "Enable authoritative DNS service.";
2023-10-05 10:53:17 -07:00
identity = mkOption {
2023-10-04 09:31:17 -07:00
type = str;
description = "The identity (CH TXT ID.SERVER) of this host.";
};
domains = mkOption {
type = attrsOf (submodule domainOpts);
default = { };
description = "A map of domain to domain options.";
};
listen-ips = mkOption {
type = listOf str;
description =
"List of IP addresses on which to listen. If empty, listen on all addresses.";
default = [ ];
};
state-directory = mkOption {
type = str;
description =
"Path on which to store nameserver state, including DNSSEC keys.";
};
timestamp = mkOption {
type = str;
description = "Timestamp to attach to zone record.";
};
2023-11-03 11:07:26 -07:00
ip-host-map = mkOption {
type = attrsOf str;
description =
"Map of IP address to authoritative hostname. Unneeded hosts will be ignored.";
default = { };
};
mirrored-domains = mkOption {
type = attrsOf str;
description = "Map of domain name to primary server IP.";
default = { };
};
2023-10-04 09:31:17 -07:00
};
2024-01-28 22:34:07 -08:00
imports = [ ./nsd.nix ];
2023-10-04 09:31:17 -07:00
config = mkIf cfg.enable {
2024-01-28 22:34:07 -08:00
services.fudo-nsd = {
2023-10-05 13:20:54 -07:00
enable = true;
identity = cfg.identity;
interfaces = cfg.listen-ips;
2024-01-28 22:34:07 -08:00
stateDirectory = cfg.state-directory;
2023-11-01 10:13:33 -07:00
zones = let
2023-11-03 13:13:10 -07:00
forwardZones = mapAttrs' (domain:
2023-11-09 10:13:52 -08:00
{ ksk, zone, notify, ... }:
2023-11-01 10:13:33 -07:00
nameValuePair "${domain}." {
2023-11-03 13:13:10 -07:00
dnssec = ksk.key-file != null;
ksk.keyFile = ksk.key-file;
2023-11-09 10:13:52 -08:00
provideXFR = (map (ns: "${ns}/32 NOKEY") notify.ipv4)
++ (map (ns: "${ns}/64 NOKEY") notify.ipv6);
notify = map (ns: "${ns} NOKEY") (notify.ipv4 ++ notify.ipv6);
2023-11-01 10:13:33 -07:00
data = zoneToZonefile {
inherit domain;
inherit (cfg) timestamp;
2023-11-03 13:13:10 -07:00
inherit zone;
2023-11-01 10:13:33 -07:00
};
}) cfg.domains;
2023-11-03 13:10:50 -07:00
reverseZones = concatMapAttrs (domain:
2023-11-09 10:13:52 -08:00
{ ksk, zone, reverse-zones, notify, ... }:
2023-11-03 10:59:11 -07:00
listToAttrs (map (network:
2023-11-01 10:13:33 -07:00
reverseZonefile {
2023-11-09 10:13:52 -08:00
inherit domain network notify;
inherit (zone) nameservers;
2023-11-01 10:13:33 -07:00
ipHostMap = cfg.ip-host-map;
serial = cfg.timestamp;
2023-11-03 13:13:10 -07:00
}) reverse-zones)) cfg.domains;
secondaryZones = mapAttrs (domain: masterIp: {
allowNotify = "${masterIp}/32";
requestXFR = "AXFR ${masterIp} NOKEY";
}) cfg.mirrored-domains;
2023-11-01 10:13:33 -07:00
in forwardZones // reverseZones;
2023-10-05 13:20:54 -07:00
};
2023-10-04 09:31:17 -07:00
};
}