summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sys/ns/rr.nix545
1 files changed, 303 insertions, 242 deletions
diff --git a/sys/ns/rr.nix b/sys/ns/rr.nix
index 11e1aa6..8f9318d 100644
--- a/sys/ns/rr.nix
+++ b/sys/ns/rr.nix
@@ -1,297 +1,358 @@
-{ lib, pkgs, ... }:
+{ config, lib, options, pkgs, ... }:
with lib; let
+ cfg = config.local.ns;
+
segmentRegex = "[a-z0-9_-]+(\\.[a-z0-9_-]+)*";
emailType = lib.types.strMatching "[a-z0-9._-]+(@${segmentRegex})?";
domainRefType = lib.types.strMatching "@|${segmentRegex}\\.?";
domainNameType = lib.types.strMatching "${segmentRegex}\\.";
+
+ zoneHashCheck = name: zone:
+ let
+ zoneHash = algorithm: "${algorithm}-${builtins.hashString algorithm cfg.nullSerialZones.${name}.content}";
+ expected = zoneHash "sha256";
+ in
+ {
+ inherit expected zone;
+ needsUpdate = zone.soa.serial == null || zone.nullSerialHash != expected;
+ };
+
+ rrTypes = [
+ "A"
+ "AAAA"
+ "CNAME"
+ "MX"
+ "NS"
+ "SOA"
+ "SRV"
+ "TXT"
+ ];
in
{
- options.local.ns.zones = mkOption {
- default = { };
-
- type = with lib.types; attrsOf (submodule ({ config, name, ... }:
- let
- nameOption = mkOption {
- type = domainRefType;
-
- apply = value:
- if value == "@"
- then "${name}."
- else if ! hasSuffix "." value
- then "${value}.${name}."
- else value;
- };
-
- rrType = options: mkOption {
- default = [ ];
- type = listOf (submodule {
- options = options // {
- name = nameOption;
-
- ttl = mkOption {
- type = int;
- default = config.defaultTTL;
- };
- };
- });
- };
-
- rrConfig = { rrs, type, format, applyName ? (rr: rr.name) }: (map
- (rr: {
- inherit type;
- inherit (rr) ttl;
-
- data = format rr;
- name = applyName rr;
- })
- rrs);
- in
- {
- options = {
- defaultTTL = mkOption {
- type = int;
- default = 3600;
- };
-
- nsdConfig = mkOption {
- type = attrsOf unspecified;
- default = { };
- };
-
- content = mkOption {
- type = lines;
- readOnly = true;
+ options.local.ns = {
+ nullSerialZones = mkOption {
+ type = options.local.ns.zones.type;
+ readOnly = true;
+ };
+
+ zones = mkOption {
+ default = { };
+
+ type = with lib.types; attrsOf (submodule ({ config, name, ... }:
+ let
+ nameOption = mkOption {
+ type = domainRefType;
+
+ apply = value:
+ if value == "@"
+ then "${name}."
+ else if ! hasSuffix "." value
+ then "${value}.${name}."
+ else value;
};
- rr = mkOption {
+ rrType = options: mkOption {
default = [ ];
- type = listOf
- (submodule {
- options = {
- name = nameOption;
-
- ttl = mkOption {
- type = int;
- };
-
- class = mkOption {
- type = enum [ "IN" ];
- default = "IN";
- };
-
- type = mkOption {
- type = enum [
- "A"
- "AAAA"
- "CNAME"
- "MX"
- "NS"
- "SOA"
- "SRV"
- "TXT"
- ];
- };
+ type = listOf (submodule {
+ options = options // {
+ name = nameOption;
- data = mkOption {
- type = listOf (either int str);
- default = [ ];
- };
+ ttl = mkOption {
+ type = int;
+ default = config.defaultTTL;
};
- });
+ };
+ });
};
- soa = {
- ttl = mkOption {
- type = int;
- default = config.defaultTTL;
- };
+ rrConfig = { rrs, type, format, applyName ? (rr: rr.name) }: (map
+ (rr: {
+ inherit type;
+ inherit (rr) ttl;
- primary = nameOption;
-
- hostmaster = mkOption {
- type = emailType;
- apply = address:
- let
- split = splitString "@" address;
-
- user = head split;
- domain = if length split == 2 then head (tail split) else name;
- in
- "${replaceStrings [ "." ] [ "\\." ] user}.${domain}.";
- };
-
- serial = mkOption {
+ data = format rr;
+ name = applyName rr;
+ })
+ rrs);
+ in
+ {
+ options = {
+ defaultTTL = mkOption {
type = int;
+ default = 3600;
};
- refresh = mkOption {
- type = int;
- default = 3 * 3600;
+ nsdConfig = mkOption {
+ type = attrsOf unspecified;
+ default = { };
};
- retry = mkOption {
- type = int;
- default = 3600;
+ content = mkOption {
+ type = lines;
+ readOnly = true;
};
- expire = mkOption {
- type = int;
- default = 7 * 24 * 3600;
+ nullSerialHash = mkOption {
+ type = nullOr str;
+ default = null;
};
- negativeTTL = mkOption {
- type = int;
- default = 3600;
+ rr = mkOption {
+ default = [ ];
+ type = listOf
+ (submodule {
+ options = {
+ name = nameOption;
+
+ ttl = mkOption {
+ type = int;
+ };
+
+ class = mkOption {
+ type = enum [ "IN" ];
+ default = "IN";
+ };
+
+ type = mkOption {
+ type = enum rrTypes;
+ };
+
+ data = mkOption {
+ type = listOf (either int str);
+ default = [ ];
+ };
+ };
+ });
};
- };
- a = rrType {
- ipv4 = mkOption {
- type = str;
- };
- };
+ soa = {
+ ttl = mkOption {
+ type = int;
+ default = config.defaultTTL;
+ };
- aaaa = rrType {
- ipv6 = mkOption {
- type = str;
- };
- };
+ primary = nameOption;
- cname = rrType {
- target = nameOption;
- };
+ hostmaster = mkOption {
+ type = emailType;
+ apply = address:
+ let
+ split = splitString "@" address;
- mx = rrType {
- host = nameOption;
+ user = head split;
+ domain = if length split == 2 then head (tail split) else name;
+ in
+ if hasSuffix "." address
+ then address
+ else "${replaceStrings [ "." ] [ "\\." ] user}.${domain}.";
+ };
- priority = mkOption {
- type = int;
- };
- };
+ serial = mkOption {
+ type = nullOr int;
+ default = null;
+ };
- ns = rrType {
- host = nameOption;
- };
+ refresh = mkOption {
+ type = int;
+ default = 3 * 3600;
+ };
- srv = rrType {
- host = nameOption;
+ retry = mkOption {
+ type = int;
+ default = 3600;
+ };
- port = mkOption {
- type = port;
+ expire = mkOption {
+ type = int;
+ default = 7 * 24 * 3600;
+ };
+
+ negativeTTL = mkOption {
+ type = int;
+ default = 3600;
+ };
};
- priority = mkOption {
- type = int;
+ a = rrType {
+ ipv4 = mkOption {
+ type = str;
+ };
};
- proto = mkOption {
- type = enum [ "tcp" "udp" ];
+ aaaa = rrType {
+ ipv6 = mkOption {
+ type = str;
+ };
};
- service = mkOption {
- type = str;
+ cname = rrType {
+ target = nameOption;
};
- weight = mkOption {
- type = int;
+ mx = rrType {
+ host = nameOption;
+
+ priority = mkOption {
+ type = int;
+ };
};
- };
- txt = rrType {
- text = mkOption {
- type = strMatching "[^\"\n\\]*\n?";
- apply = removeSuffix "\n";
+ ns = rrType {
+ host = nameOption;
};
- };
- };
- config = {
- nsdConfig.data = config.content;
-
- content =
- let
- rrLine = rr: concatMapStringsSep " " toString ([ rr.name rr.ttl rr.class rr.type ] ++ rr.data);
- in
- ''
- $ORIGIN ${name}.
- $TTL ${toString config.defaultTTL}
- '' + concatLines (map rrLine config.rr);
-
- rr = mkMerge [
- (mkOrder 0 (singleton {
- inherit (config.soa) ttl;
-
- name = "${name}.";
- type = "SOA";
-
- data = with config.soa; [
- primary
- hostmaster
- serial
- refresh
- retry
- expire
- negativeTTL
- ];
- }))
-
- (mkOrder 1 (rrConfig {
- rrs = config.ns;
- type = "NS";
- format = rr: [ rr.host ];
- }))
-
- (rrConfig {
- rrs = config.a;
- type = "A";
- format = rr: [ rr.ipv4 ];
- })
+ srv = rrType {
+ host = nameOption;
- (rrConfig {
- rrs = config.aaaa;
- type = "AAAA";
- format = rr: [ rr.ipv6 ];
- })
+ port = mkOption {
+ type = port;
+ };
- (rrConfig {
- rrs = config.cname;
- type = "CNAME";
- format = rr: [ rr.target ];
- })
+ priority = mkOption {
+ type = int;
+ };
- (rrConfig {
- rrs = config.mx;
- type = "MX";
- format = rr: [ rr.priority rr.host ];
- })
+ proto = mkOption {
+ type = enum [ "tcp" "udp" ];
+ };
- (rrConfig {
- rrs = config.srv;
- type = "SRV";
+ service = mkOption {
+ type = str;
+ };
- format = rr: [ rr.priority rr.weight rr.port rr.host ];
- applyName = rr: "_${rr.service}._${rr.proto}.${rr.name}";
- })
+ weight = mkOption {
+ type = int;
+ };
+ };
- (rrConfig {
- rrs = config.txt;
- type = "TXT";
-
- format = rr:
- let
- # nsd-zonecheck: text string is longer than 255 characters, try splitting it into multiple parts
- txtFragments = text:
- let
- max = 255;
- length = stringLength text;
- in
- singleton (substring 0 max text) ++ optionals (length > max) (txtFragments (substring max length text));
- in
- map (fragment: "\"${fragment}\"") (txtFragments rr.text);
- })
- ];
+ txt = rrType {
+ text = mkOption {
+ type = strMatching "[^\"\n\\]*\n?";
+ apply = removeSuffix "\n";
+ };
+ };
+ };
+
+ config = {
+ nsdConfig.data = config.content;
+
+ content =
+ let
+ rrLine = rr: concatMapStringsSep " " toString ([ rr.name rr.ttl rr.class rr.type ] ++ rr.data);
+ in
+ ''
+ $ORIGIN ${name}.
+ $TTL ${toString config.defaultTTL}
+ '' + concatLines (map rrLine config.rr);
+
+ rr = mkMerge [
+ (mkOrder 0 (singleton {
+ inherit (config.soa) ttl;
+
+ name = "${name}.";
+ type = "SOA";
+
+ data = with config.soa; [
+ primary
+ hostmaster
+ (throwIf (serial == null) "No serial defined for zone ${name}" serial)
+ refresh
+ retry
+ expire
+ negativeTTL
+ ];
+ }))
+
+ (mkOrder 1 (rrConfig {
+ rrs = config.ns;
+ type = "NS";
+ format = rr: [ rr.host ];
+ }))
+
+ (rrConfig {
+ rrs = config.a;
+ type = "A";
+ format = rr: [ rr.ipv4 ];
+ })
+
+ (rrConfig {
+ rrs = config.aaaa;
+ type = "AAAA";
+ format = rr: [ rr.ipv6 ];
+ })
+
+ (rrConfig {
+ rrs = config.cname;
+ type = "CNAME";
+ format = rr: [ rr.target ];
+ })
+
+ (rrConfig {
+ rrs = config.mx;
+ type = "MX";
+ format = rr: [ rr.priority rr.host ];
+ })
+
+ (rrConfig {
+ rrs = config.srv;
+ type = "SRV";
+
+ format = rr: [ rr.priority rr.weight rr.port rr.host ];
+ applyName = rr: "_${rr.service}._${rr.proto}.${rr.name}";
+ })
+
+ (rrConfig {
+ rrs = config.txt;
+ type = "TXT";
+
+ format = rr:
+ let
+ # nsd-zonecheck: text string is longer than 255 characters, try splitting it into multiple parts
+ txtFragments = text:
+ let
+ max = 255;
+ length = stringLength text;
+ in
+ singleton (substring 0 max text) ++ optionals (length > max) (txtFragments (substring max length text));
+ in
+ map (fragment: "\"${fragment}\"") (txtFragments rr.text);
+ })
+ ];
+ };
+ }));
+ };
+ };
+
+ config = {
+ assertions = [
+ (
+ let
+ badZones = attrNames (filterAttrs (name: zone: (zoneHashCheck name zone).needsUpdate) cfg.zones);
+ in
+ {
+ assertion = badZones == [ ];
+ message = "Update serials for these zones (null-serial hash mismatch): ${concatStringsSep ", " badZones}";
+ }
+ )
+ ];
+
+ lib.local.zoneSerialUpdates =
+ let
+ zoneChecks = mapAttrs zoneHashCheck cfg.zones;
+ updateInfo = check: {
+ inherit (check) expected;
+ inherit (check.zone.soa) serial;
};
- }));
+ in
+ mapAttrs (_: updateInfo) (filterAttrs (_: check: check.needsUpdate) zoneChecks);
+
+ local.ns.nullSerialZones =
+ mapAttrs
+ (_: zone: mkMerge [
+ (filterAttrs (name: _: elem name ([ "defaultTTL" ] ++ map toLower rrTypes)) zone)
+ { soa.serial = mkOverride 0 0; }
+ ])
+ cfg.zones;
};
}