ctucx.git: dns.nix

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

commit 35331dce32c4907c2176d3a1b09184776a934872
parent 914e2794d151e86495ba4b9e3ab64e48250b568d
Author: Naïm Favier <n@monade.li>
Date: Fri, 26 Mar 2021 16:35:03 +0100

Expose $TTL as an option
2 files changed, 36 insertions(+), 22 deletions(-)
M
dns/types/record.nix
|
32
++++++++++++++++++--------------
M
dns/types/zone.nix
|
26
++++++++++++++++++--------
diff --git a/dns/types/record.nix b/dns/types/record.nix
@@ -1,5 +1,6 @@
 #
 # © 2019 Kirill Elagin <kirelagin@gmail.com>
+# © 2021 Naïm Favier <n@monade.li>
 #
 # SPDX-License-Identifier: MIT
 #

@@ -7,26 +8,21 @@
 { lib }:
 
 let
-  inherit (lib) const isString mkOption types;
-
-  defaults = {
-    class = "IN";
-    ttl = 24 * 60 * 60;
-  };
+  inherit (lib) isString mkOption types;
 
   recordType = rsubt:
     let
       submodule = types.submodule {
         options = {
           class = mkOption {
-            type = types.enum ["IN"];
-            default = defaults.class;
+            type = types.enum [ "IN" ];
+            default = "IN";
             example = "IN";
             description = "Resource record class. Only IN is supported";
           };
           ttl = mkOption {
-            type = types.ints.unsigned;  # TODO: u32
-            default = defaults.ttl;
+            type = types.nullOr types.ints.unsigned;  # TODO: u32
+            default = null;
             example = 300;
             description = "Record caching duration (in seconds)";
           };

@@ -38,13 +34,21 @@ let
   writeRecord = name: rsubt: data:
     let
       data' =
-        if isString data && rsubt ? fromString
-        then defaults // rsubt.fromString data
+        if isString data && rsubt ? fromString then
+          # add default values for the record type
+          (recordType rsubt).merge [] [ { file = ""; value = rsubt.fromString data; } ]
         else data;
       name' = rsubt.nameFixup or (n: _: n) name data';
       rtype = rsubt.rtype;
-    in with data';
-      "${name'}. ${toString ttl} ${class} ${rtype} ${rsubt.dataToString data'}";
+    in lib.concatStringsSep " " (with data'; [
+        "${name'}."
+      ] ++ lib.optionals (ttl != null) [
+        (toString ttl)
+      ] ++ [
+        class
+        rtype
+        (rsubt.dataToString data')
+      ]);
 
 in
 
diff --git a/dns/types/zone.nix b/dns/types/zone.nix
@@ -1,5 +1,6 @@
 #
 # © 2019 Kirill Elagin <kirelagin@gmail.com>
+# © 2021 Naïm Favier <n@monade.li>
 #
 # SPDX-License-Identifier: MIT
 #

@@ -10,7 +11,7 @@ let
   inherit (builtins) attrValues filter map removeAttrs;
   inherit (lib) concatMapStringsSep concatStringsSep mapAttrs
                      mapAttrsToList optionalString;
-  inherit (lib) mkOption types;
+  inherit (lib) mkOption literalExample types;
 
   inherit (import ./record.nix { inherit lib; }) recordType writeRecord;
 

@@ -40,8 +41,8 @@ let
     }) rsubtypes';
 
   subzone = types.submodule {
-      options = subzoneOptions;
-    };
+    options = subzoneOptions;
+  };
 
   writeSubzone = name: zone:
     let

@@ -56,8 +57,14 @@ let
       concatStringsSep "\n\n" groups'
       + optionalString (sub != "") ("\n\n" + sub);
 
-  zone = types.submodule ({name, ...}: {
+  zone = types.submodule ({ name, ... }: {
     options = {
+      TTL = mkOption {
+        type = types.ints.unsigned;
+        default = 24 * 60 * 60;
+        example = literalExample "60 * 60";
+        description = "Default record caching duration. Sets the $TTL variable";
+      };
       SOA = mkOption rec {
         type = recordType rsubtypes.SOA;
         example = {

@@ -72,11 +79,14 @@ let
     } // subzoneOptions;
 
     config = {
-      __toString = zone@{SOA, ...}:
-          ''
-            ${writeRecord name rsubtypes.SOA SOA}
+      __toString = zone@{ TTL, SOA, ... }:
+        ''
+          $TTL ${toString TTL}
+
+          ${writeRecord name rsubtypes.SOA SOA}
 
-          '' + writeSubzone name zone + "\n";
+          ${writeSubzone name zone}
+        '';
     };
   });