ctucx.git: dns-nix

fork of https://github.com/kirelagin/dns.nix

commit e206dc337c36fa5eb73abd7ba83da87c9f992846
parent 36a558e3efd1ac20af0fb265a71e08b884de28d6
Author: Kirill Elagin <kirelagin@gmail.com>
Date: Fri, 8 Mar 2019 23:47:14 +0100

Abstract away subzone type
2 files changed, 52 insertions(+), 39 deletions(-)
M
dns/types/default.nix
|
2
+-
M
dns/types/zone.nix
|
89
+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
diff --git a/dns/types/default.nix b/dns/types/default.nix
@@ -7,7 +7,7 @@
 { pkgs }:
 
 {
-  zone = import ./zone.nix { inherit pkgs; };
+  inherit (import ./zone.nix { inherit pkgs; }) zone subzone;
   record = import ./record.nix { inherit pkgs; };
   records = import ./records { inherit pkgs; };
 }
diff --git a/dns/types/zone.nix b/dns/types/zone.nix
@@ -12,46 +12,59 @@ let
   inherit (pkgs.lib) mkOption types;
 
   record = import ./record.nix { inherit pkgs; };
-  recordTypes = import ./records { inherit pkgs; };
 
+  recordTypes = import ./records { inherit pkgs; };
   recordTypes' = filterAttrs (n: v: n != "SOA") recordTypes;
+
+  subzoneOptions = name: {
+  } //
+    mapAttrs (n: t: mkOption rec {
+      type = types.listOf (record t name);
+      default = [];
+      example = [ t.example ];
+      description = "List of ${t} records for this zone/subzone";
+    }) recordTypes';
+
+  writeSubzone = zone:
+    let
+      groupToString = n:
+        concatMapStringsSep "\n" toString (zone."${n}");
+      groups = map groupToString (builtins.attrNames recordTypes');
+      groups' = filter (s: s != "") groups;
+    in
+      concatStringsSep "\n\n" groups';
 in
 
-types.submodule ({name, ...}: {
-  options = {
-    SOA = mkOption rec {
-      type = record recordTypes.SOA name;
-      example = {
-        ttl = 24 * 60 * 60;
-      } // type.example;
-      description = "SOA record";
-    };
-    __toString = mkOption {
-      readOnly = true;
-      visible = false;
+{
+  zone = types.submodule ({name, ...}: {
+    options = {
+      SOA = mkOption rec {
+        type = record recordTypes.SOA name;
+        example = {
+          ttl = 24 * 60 * 60;
+        } // type.example;
+        description = "SOA record";
+      };
+      __toString = mkOption {
+        readOnly = true;
+        visible = false;
+      };
+    } // subzoneOptions name;
+
+    config = {
+      __toString = zone@{SOA, ...}:
+          ''
+            $TTL 24h
+
+            $ORIGIN ${SOA.name}.
+
+            ${toString SOA}
+
+          '' + writeSubzone zone + "\n";
     };
-  }  // mapAttrs (n: t: mkOption rec {
-          type = types.listOf (record t name);
-          default = [];
-          example = [ t.example ];
-          description = "List of ${t} records for this zone/subzone";
-        }) recordTypes';
-
-  config = {
-    __toString = zone@{SOA, ...}:
-      let
-        groupToString = n:
-          concatMapStringsSep "\n" toString (zone."${n}");
-        groups = map groupToString (builtins.attrNames recordTypes');
-        groups' = filter (s: s != "") groups;
-      in
-        ''
-          $TTL 24h
-
-          $ORIGIN ${SOA.name}.
-
-          ${toString SOA}
-
-        '' + concatStringsSep "\n\n" groups' + "\n";
-  };
-})
+  });
+
+  subzone = types.submodule ({name, ...}: {
+    options = subzoneOptions name;
+  });
+}