@@ -250,6 +250,46 @@ let
250250 Use imports or pkgs.lib.readFile if you don't want this data in your config file.
251251 '';
252252 };
253+
254+ dnssec = mkEnableOption "DNSSEC";
255+
256+ dnssecPolicy = {
257+ algorithm = mkOption {
258+ type = types.str;
259+ default = "RSASHA256";
260+ description = "Which algorithm to use for DNSSEC";
261+ };
262+ keyttl = mkOption {
263+ type = types.str;
264+ default = "1h";
265+ description = "TTL for dnssec records";
266+ };
267+ coverage = mkOption {
268+ type = types.str;
269+ default = "1y";
270+ description = ''
271+ The length of time to ensure that keys will be correct; no action will be taken to create new keys to be activated after this time.
272+ '';
273+ };
274+ zsk = mkOption {
275+ type = keyPolicy;
276+ default = { keySize = 2048;
277+ prePublish = "1w";
278+ postPublish = "1w";
279+ rollPeriod = "1mo";
280+ };
281+ description = "Key policy for zone signing keys";
282+ };
283+ ksk = mkOption {
284+ type = keyPolicy;
285+ default = { keySize = 4096;
286+ prePublish = "1mo";
287+ postPublish = "1mo";
288+ rollPeriod = "0";
289+ };
290+ description = "Key policy for key signing keys";
291+ };
292+ };
253293
254294 maxRefreshSecs = mkOption {
255295 type = types.nullOr types.int;
@@ -367,10 +407,61 @@ let
367407 and stats_noreset.
368408 '';
369409 };
410+ };
411+ };
370412
413+ keyPolicy = types.submodule {
414+ options = {
415+ keySize = mkOption {
416+ type = types.int;
417+ description = "Key size in bits";
418+ };
419+ prePublish = mkOption {
420+ type = types.str;
421+ description = "How long in advance to publish new keys";
422+ };
423+ postPublish = mkOption {
424+ type = types.str;
425+ description = "How long after deactivation to keep a key in the zone";
426+ };
427+ rollPeriod = mkOption {
428+ type = types.str;
429+ description = "How frequently to change keys";
430+ };
371431 };
372432 };
373433
434+ dnssecZones = (filterAttrs (n: v: if v ? dnssec then v.dnssec else false) zoneConfigs);
435+
436+ dnssec = length (attrNames dnssecZones) != 0;
437+
438+ signZones = optionalString dnssec ''
439+ mkdir -p ${stateDir}/dnssec
440+ chown ${username}:${username} ${stateDir}/dnssec
441+ chmod 0600 ${stateDir}/dnssec
442+
443+ ${concatStrings (mapAttrsToList signZone dnssecZones)}
444+ '';
445+ signZone = name: zone: ''
446+ ${pkgs.bind}/bin/dnssec-keymgr -g ${pkgs.bind}/bin/dnssec-keygen -s ${pkgs.bind}/bin/dnssec-settime -K ${stateDir}/dnssec -c ${policyFile name zone.dnssecPolicy} ${name}
447+ ${pkgs.bind}/bin/dnssec-signzone -S -K ${stateDir}/dnssec -o ${name} -O full -N date ${stateDir}/zones/${name}
448+ ${nsdPkg}/sbin/nsd-checkzone ${name} ${stateDir}/zones/${name}.signed && mv -v ${stateDir}/zones/${name}.signed ${stateDir}/zones/${name}
449+ '';
450+ policyFile = name: policy: pkgs.writeText "${name}.policy" ''
451+ zone ${name} {
452+ algorithm ${policy.algorithm};
453+ key-size zsk ${toString policy.zsk.keySize};
454+ key-size ksk ${toString policy.ksk.keySize};
455+ keyttl ${policy.keyttl};
456+ pre-publish zsk ${policy.zsk.prePublish};
457+ pre-publish ksk ${policy.ksk.prePublish};
458+ post-publish zsk ${policy.zsk.postPublish};
459+ post-publish ksk ${policy.ksk.postPublish};
460+ roll-period zsk ${policy.zsk.rollPeriod};
461+ roll-period ksk ${policy.ksk.rollPeriod};
462+ coverage ${policy.coverage};
463+ };
464+ '';
374465in
375466{
376467 # options are ordered alphanumerically
@@ -380,6 +471,14 @@ in
380471
381472 bind8Stats = mkEnableOption "BIND8 like statistics";
382473
474+ dnssecInterval = mkOption {
475+ type = types.str;
476+ default = "1h";
477+ description = ''
478+ How often to check whether dnssec key rollover is required
479+ '';
480+ };
481+
383482 extraConfig = mkOption {
384483 type = types.str;
385484 default = "";
@@ -741,7 +840,6 @@ in
741840
742841 };
743842
744−
745843 zones = mkOption {
746844 type = types.attrsOf zoneOptions;
747845 default = {};
@@ -785,7 +883,6 @@ in
785883 serverGroup1.
786884 '';
787885 };
788−
789886 };
790887
791888 config = mkIf cfg.enable {
@@ -832,9 +929,9 @@ in
832929 mkdir -m 0700 -p "${stateDir}/var"
833930
834931 cat > "${stateDir}/don't touch anything in here" << EOF
835− Everything in this directory except NSD's state in var is
836− automatically generated and will be purged and redeployed
837− by the nsd.service pre-start script.
932+ Everything in this directory except NSD's state in var and dnssec
933+ is automatically generated and will be purged and redeployed by
934+ the nsd.service pre-start script.
838935 EOF
839936
840937 chown ${username}:${username} -R "${stateDir}/private"
@@ -848,6 +945,34 @@ in
848945 '';
849946 };
850947
948+ nixpkgs.config = mkIf dnssec {
949+ bind.enablePython = true;
950+ };
951+
952+ systemd.timers."nsd-dnssec" = mkIf dnssec {
953+ description = "Automatic DNSSEC key rollover";
954+
955+ wantedBy = [ "nsd.service" ];
956+
957+ timerConfig = {
958+ OnActiveSec = cfg.dnssecInterval;
959+ OnUnitActiveSec = cfg.dnssecInterval;
960+ };
961+ };
962+
963+ systemd.services."nsd-dnssec" = mkIf dnssec {
964+ description = "DNSSEC key rollover";
965+
966+ wantedBy = [ "nsd.service" ];
967+ before = [ "nsd.service" ];
968+
969+ script = signZones;
970+
971+ postStop = ''
972+ ${pkgs.systemd}/bin/systemctl kill -s SIGHUP nsd.service
973+ '';
974+ };
975+
851976 };
852977
853978 meta.maintainers = with lib.maintainers; [ hrdinka ];