nixos-config/lib/fudo/dns.nix

144 lines
4.1 KiB
Nix
Raw Normal View History

2020-06-25 20:38:50 -07:00
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.fudo.dns;
join-lines = concatStringsSep "\n";
2021-02-28 14:22:43 -08:00
domainOpts = { domain, ... }: {
options = with types; {
dnssec = mkOption {
type = bool;
description = "Enable DNSSEC security for this zone.";
default = true;
};
2020-06-25 20:38:50 -07:00
2021-02-28 14:22:43 -08:00
dmarc-report-address = mkOption {
type = nullOr str;
description = "The email to use to recieve DMARC reports, if any.";
example = "admin-user@domain.com";
default = null;
};
2020-06-25 20:38:50 -07:00
2021-02-28 14:22:43 -08:00
network-definition = mkOption {
type = submodule (import ../types/network-definition.nix);
description = "Definition of network to be served by local server.";
2020-06-25 20:38:50 -07:00
};
};
2021-02-28 14:22:43 -08:00
};
2020-06-25 20:38:50 -07:00
2020-11-19 14:21:18 -08:00
hostRecords = host: data:
2021-02-28 14:22:43 -08:00
join-lines
((optional (data.ipv4-address != null) "${host} IN A ${data.ipv4-address}")
++ (optional (data.ipv6-address != null)
"${host} IN AAAA ${data.ipv6-address}")
++ (map (sshfp: "${host} IN SSHFP ${sshfp}") data.ssh-fingerprints)
++ (optional (data.rp != null) "${host} IN RP ${data.rp}")
++ (optional (data.description != null)
"${host} IN TXT ${data.description}"));
2020-06-25 20:38:50 -07:00
makeSrvRecords = protocol: type: records:
join-lines (map (record:
"_${type}._${protocol} IN SRV ${toString record.priority} ${
toString record.weight
} ${toString record.port} ${toString record.host}.") records);
2020-06-25 20:38:50 -07:00
makeSrvProtocolRecords = protocol: types:
join-lines (mapAttrsToList (makeSrvRecords protocol) types);
2020-06-25 20:38:50 -07:00
cnameRecord = alias: host: "${alias} IN CNAME ${host}";
mxRecords = mxs: concatStringsSep "\n" (map (mx: "@ IN MX 10 ${mx}.") mxs);
2020-06-25 20:38:50 -07:00
dmarcRecord = dmarc-email:
optionalString (dmarc-email != null) ''
_dmarc IN TXT "v=DMARC1;p=quarantine;sp=quarantine;rua=mailto:${dmarc-email};"'';
2020-06-25 20:38:50 -07:00
2021-02-28 14:22:43 -08:00
nsRecords = domain: ns-hosts:
join-lines
(mapAttrsToList (host: _: "@ IN NS ${host}.${domain}.") ns-hosts);
2020-06-25 20:38:50 -07:00
in {
options.fudo.dns = with types; {
enable = mkEnableOption "Enable master DNS services.";
# FIXME: This should allow for AAAA addresses too...
nameservers = mkOption {
2021-03-02 19:03:35 -08:00
type = attrsOf (submodule hostOpts);
2020-06-25 20:38:50 -07:00
description = "Map of domain nameserver FQDNs to IP.";
example = {
"ns1.domain.com" = {
ip-addresses = [ "1.1.1.1" ];
ipv6-addresses = [ ];
description = "my fancy dns server";
};
};
};
identity = mkOption {
type = str;
description = "The identity (CH TXT ID.SERVER) of this host.";
2020-06-25 20:38:50 -07:00
};
domains = mkOption {
2021-03-02 19:03:35 -08:00
type = attrsOf (submodule domainOpts);
default = { };
2020-06-25 20:38:50 -07:00
description = "A map of domain to domain options.";
};
listen-ips = mkOption {
type = listOf str;
description = "A list of IPs on which to listen for DNS queries.";
example = [ "1.2.3.4" ];
2020-06-25 20:38:50 -07:00
};
};
config = mkIf cfg.enable {
services.nsd = {
enable = true;
identity = cfg.identity;
2020-06-25 20:38:50 -07:00
interfaces = cfg.listen-ips;
zones = mapAttrs' (dom: dom-cfg:
nameValuePair "${dom}." {
dnssec = dom-cfg.dnssec;
data = ''
$ORIGIN ${dom}.
$TTL 12h
@ IN SOA ns1.${dom}. hostmaster.${dom}. (
${toString builtins.currentTime}
5m
2m
6w
5m)
${optionalString (dom-cfg.default-host != null)
"@ IN A ${dom-cfg.default-host}"}
2020-06-25 20:38:50 -07:00
${mxRecords dom-cfg.mx}
$TTL 6h
2021-03-01 14:43:27 -08:00
${optionalString (dom-cfg.gssapi-realm != null)
''_kerberos IN TXT "${dom-cfg.gssapi-realm}"''}
${nsRecords dom cfg.nameservers}
${join-lines (mapAttrsToList hostRecords cfg.nameservers)}
2020-06-25 20:38:50 -07:00
${dmarcRecord dom-cfg.dmarc-report-address}
${join-lines
(mapAttrsToList makeSrvProtocolRecords dom-cfg.srv-records)}
2020-11-19 14:21:18 -08:00
${join-lines (mapAttrsToList hostRecords dom-cfg.hosts)}
2020-06-25 20:38:50 -07:00
${join-lines (mapAttrsToList cnameRecord dom-cfg.aliases)}
${join-lines dom-cfg.extra-dns-records}
'';
}) cfg.domains;
};
};
}