ctucx.git: dns.nix

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

commit b8d53c7a47bb0af55d06760f4a67c78d1d51d8f1
Author: Kirill Elagin <kirelagin@gmail.com>
Date: Fri, 8 Mar 2019 18:27:53 +0100

Implement initial prototype
8 files changed, 259 insertions(+), 0 deletions(-)
A
LICENSES/MIT.txt
|
7
+++++++
A
README.md
|
12
++++++++++++
A
default.nix
|
40
++++++++++++++++++++++++++++++++++++++++
A
dns/default.nix
|
21
+++++++++++++++++++++
A
dns/types/default.nix
|
13
+++++++++++++
A
dns/types/record.nix
|
56
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
dns/types/records/default.nix
|
65
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
dns/types/zone.nix
|
45
+++++++++++++++++++++++++++++++++++++++++++++
diff --git a/LICENSES/MIT.txt b/LICENSES/MIT.txt
@@ -0,0 +1,7 @@
+Copyright 2019 Kirill Elagin <kirelagin@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
@@ -0,0 +1,12 @@
+_Nix DSL for defining DNS zones_
+
+nix-dns
+========
+
+This repository provies:
+
+1. NixOS-style module definitions that describe DNS zones.
+2. A DSL to make building DNS zones easier. (Not implemented yet.)
+
+See `default.nix` for an example module.
+Run `nix-build` to get an example of a zone built from a module.
diff --git a/default.nix b/default.nix
@@ -0,0 +1,40 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs ? import <nixpkgs> {} }:
+
+let
+  inherit (pkgs) lib;
+  dns = import ./dns { inherit pkgs; };
+
+  module = {
+    options = {
+      zones = lib.mkOption {
+        type = lib.types.attrsOf dns.types.zone;
+        default = {};
+        description = "DNS zones";
+      };
+    };
+  };
+
+  testZones = {
+    "test.com" = {
+      soa = {
+        nameServer = "ns.test.com";
+        adminEmail = "admin@test.com";
+        serial = 2019030800;
+      };
+    };
+  };
+
+  testConfig = (lib.evalModules {
+    modules = [
+      (module // { config = { zones = testZones; }; })
+    ];
+  }).config;
+in
+
+dns.mkZone "test.com" (testConfig.zones."test.com")
diff --git a/dns/default.nix b/dns/default.nix
@@ -0,0 +1,21 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs }:
+
+let
+  mkZone = name: zone:
+    pkgs.writeTextFile {
+      name = "${name}.zone";
+      text = toString zone;
+    };
+in
+
+{
+  inherit mkZone;
+
+  types = import ./types { inherit pkgs; };
+}
diff --git a/dns/types/default.nix b/dns/types/default.nix
@@ -0,0 +1,13 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs }:
+
+{
+  zone = import ./zone.nix { inherit pkgs; };
+  record = import ./record.nix { inherit pkgs; };
+  records = import ./records { inherit pkgs; };
+}
diff --git a/dns/types/record.nix b/dns/types/record.nix
@@ -0,0 +1,56 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs }:
+
+let
+  inherit (pkgs) lib;
+  inherit (lib) mkOption types;
+
+  recordTypes = import ./records { inherit pkgs; };
+in
+
+recordType: types.submodule {
+  options = {
+    name = mkOption {
+      type = types.str;
+      example = "example.com";
+      description = "Name of the node to which this resource record pertains";
+    };
+    rtype = mkOption {
+      type = types.enum (lib.mapAttrsToList (n: v: v.rtype) recordTypes);
+      readOnly = true;
+      visible = false;
+      description = "Type of the record. Do not set this option yourself!";
+    };
+    _rtype = mkOption {
+      readOnly = true;
+      visible = false;
+    };
+    class = mkOption {
+      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 = 24 * 60 * 60;
+      example = 300;
+      description = "Record caching duration (in seconds)";
+    };
+    __toString = mkOption {
+      readOnly = true;
+      visible = false;
+    };
+  } // recordType.options;
+  config = {
+    rtype  = recordType.rtype;
+    _rtype = recordType;
+    __toString = data@{name, rtype, class, ttl, _rtype, ...}:
+      "${name}. ${toString ttl} ${class} ${rtype} ${_rtype.dataToString data}";
+  };
+}
diff --git a/dns/types/records/default.nix b/dns/types/records/default.nix
@@ -0,0 +1,65 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs }:
+
+let
+  inherit (pkgs.lib) concatStringsSep replaceStrings;
+  inherit (pkgs.lib) mkOption types;
+
+in
+
+{
+  SOA = {
+    rtype = "SOA";
+    options = {
+      nameServer = mkOption {
+        type = types.str;
+        example = "ns1.example.com";
+        description = "The <domain-name> of the name server that was the original or primary source of data for this zone";
+      };
+      adminEmail = mkOption {
+        type = types.str;
+        example = "admin@example.com";
+        description = "An email address of the person responsible for this zone. (Note: in traditional zone files you are supposed to put a dot instead of `@` in your address; you can use `@` with this module and it is recommended to do so.)";
+        apply = replaceStrings ["@"] ["."];
+      };
+      serial = mkOption {
+        type = types.ints.unsigned;  # TODO: u32
+        example = 20;
+        description = "Version number of the original copy of the zone";
+      };
+      refresh = mkOption {
+        type = types.ints.unsigned;  # TODO: u32
+        default = 24 * 60 * 60;
+        example = 7200;
+        description = "Time interval before the zone should be refreshed";
+      };
+      retry = mkOption {
+        type = types.ints.unsigned;  # TODO: u32
+        default = 10 * 60;
+        example = 600;
+        description = "Time interval that should elapse before a failed refresh should be retried";
+      };
+      expire = mkOption {
+        type = types.ints.unsigned;  # TODO: u32
+        default = 10 * 24 * 60 * 60;
+        example = 3600000;
+        description = "Time value that specifies the upper limit on the time interval that can elapse before the zone is no longer authoritative";
+      };
+      minimum = mkOption {
+        type = types.ints.unsigned;  # TODO: u32
+        default = 60;
+        example = 60;
+        description = "Minimum TTL field that should be exported with any RR from this zone";
+      };
+    };
+    dataToString = data@{nameServer, adminEmail, ...}:
+      let
+        numbers = map toString (with data; [serial refresh retry expire minimum]);
+      in "${nameServer} ${adminEmail} (${concatStringsSep " " numbers})";
+  };
+}
diff --git a/dns/types/zone.nix b/dns/types/zone.nix
@@ -0,0 +1,45 @@
+#
+# © 2019 Kirill Elagin <kirelagin@gmail.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+{ pkgs }:
+
+let
+  inherit (pkgs.lib) mkOption types;
+
+  record = import ./record.nix { inherit pkgs; };
+  recordTypes = import ./records { inherit pkgs; };
+
+in
+
+types.submodule ({name, ...}: {
+  options = {
+    soa = mkOption rec {
+      type = record recordTypes.SOA;
+      example = {
+        ttl = 24 * 60 * 60;
+      } // type.example;
+      description = "SOA record";
+    };
+    __toString = mkOption {
+      readOnly = true;
+      visible = false;
+    };
+  };
+
+  config = {
+    soa.name = name;
+    soa.class = "IN";
+    __toString = { soa, ... }:
+      ''
+        $TTL 24h
+
+        $ORIGIN ${soa.name}.
+
+        ${toString soa}
+      '';
+
+  };
+})