]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/dns.nix
Use attrs for secrets instead of lists
[perso/Immae/Config/Nix.git] / modules / private / dns.nix
index cb900ffad6ca725b9853666d6055c095117b716f..1d7fd52fb3fe6e11d27ea801da4f0518ee719069 100644 (file)
@@ -2,7 +2,25 @@
 {
   options.myServices.dns.enable = lib.mkEnableOption "enable DNS resolver";
   config = let
+    # taken from unstable
+    cartesianProductOfSets = attrsOfLists: with lib;
+      lib.foldl' (listOfAttrs: attrName:
+        concatMap (attrs:
+          map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName}
+        ) listOfAttrs
+      ) [{}] (attrNames attrsOfLists);
     cfg = config.services.bind;
+    keyIncludes = builtins.concatStringsSep "\n" (map (v: "include \"${config.secrets.fullPaths."bind/${v}.key"}\";") (builtins.attrNames config.myEnv.dns.keys));
+    cartProduct = lib.foldr
+      (s: servers: servers // { ${s.masters} = lib.unique ((servers.${s.masters} or []) ++ [s.keys]); })
+      {}
+      (lib.unique (lib.concatMap (z: cartesianProductOfSets { masters = z.masters or []; keys = z.keys or []; }) config.myEnv.dns.slaveZones));
+    toKeyList = servers: keys: builtins.concatStringsSep "\n" (map (s: ''
+      server ${s} {
+        keys { ${builtins.concatStringsSep ";" keys}; };
+      };
+    '') servers);
+    serverIncludes = builtins.concatStringsSep "\n" (lib.mapAttrsToList (n: toKeyList (lib.flatten (builtins.attrValues config.myEnv.dns.ns."${n}"))) cartProduct);
     configFile = pkgs.writeText "named.conf" ''
       include "/etc/bind/rndc.key";
       controls {
@@ -24,6 +42,9 @@
         ${cfg.extraOptions}
       };
 
+      ${keyIncludes}
+      ${serverIncludes}
+
       ${cfg.extraConfig}
 
       ${ lib.concatMapStrings
   in lib.mkIf config.myServices.dns.enable {
     networking.firewall.allowedUDPPorts = [ 53 ];
     networking.firewall.allowedTCPPorts = [ 53 ];
+    users.users.named.extraGroups = [ "keys" ];
+    secrets.keys = lib.mapAttrs' (k: v:
+      lib.nameValuePair "bind/${k}.key" {
+        permissions = "0400";
+        user = "named";
+        text = ''
+          key "${k}"
+            {
+              algorithm ${v.algorithm};
+              secret "${v.secret}";
+            };
+        '';
+      }
+    ) config.myEnv.dns.keys;
     services.bind = {
       enable = true;
       cacheNetworks = ["any"];