aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/ns/nsd/module.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/by-name/ns/nsd/module.nix')
-rw-r--r--modules/by-name/ns/nsd/module.nix121
1 files changed, 121 insertions, 0 deletions
diff --git a/modules/by-name/ns/nsd/module.nix b/modules/by-name/ns/nsd/module.nix
new file mode 100644
index 0000000..816cc7a
--- /dev/null
+++ b/modules/by-name/ns/nsd/module.nix
@@ -0,0 +1,121 @@
+{
+ config,
+ lib,
+ ...
+}: let
+ cfg = config.vhack.nsd;
+
+ localInterface = "127.0.0.1@5353";
+
+ zones =
+ builtins.mapAttrs (name: value: {
+ data =
+ dns.types.zone.renderToString name value;
+ })
+ cfg.zones;
+
+ stubZones =
+ lib.mapAttrsToList (name: value: {
+ name = "${name}";
+ stub-addr = localInterface;
+
+ # Don't use published NS records, always use stub-addr
+ stub-prime = false;
+
+ # Don't cache the stub responses, it is literally on the same host.
+ stub-no-cache = true;
+ })
+ cfg.zones;
+ zoneNames = builtins.attrNames cfg.zones;
+
+ dns = import ./dns {inherit lib;};
+
+ ports = let
+ parsePorts = listeners: let
+ splitAddress = addr: lib.splitString "@" addr;
+
+ extractPort = addr: let
+ split = splitAddress addr;
+ in
+ lib.toInt (
+ if (builtins.length split) == 2
+ then builtins.elemAt split 1
+ else "53"
+ );
+ in
+ builtins.map extractPort listeners;
+ in
+ lib.unique (parsePorts cfg.interfaces);
+in {
+ options.vhack.nsd = {
+ enable = lib.mkEnableOption "nsd, a authoritative dns server";
+
+ openFirewall = lib.mkOption {
+ type = lib.types.bool;
+ default = false;
+ description = ''
+ Open the following ports:
+ TCP (${lib.concatStringsSep ", " (map toString ports)})
+ UDP (${lib.concatStringsSep ", " (map toString ports)})
+ '';
+ };
+
+ interfaces = lib.mkOption {
+ type = lib.types.listOf lib.types.str;
+ description = ''
+ A list of the interfaces to bind to. To select the port add `@` to the end of the
+ interface. The default port is 53.
+ '';
+ example = [
+ "192.168.1.3"
+ "2001:db8:1::3"
+ ];
+ };
+
+ zones = lib.mkOption {
+ type = lib.types.attrsOf dns.types.zone.zone;
+ description = "DNS zones";
+ };
+ };
+
+ config = lib.mkIf cfg.enable {
+ services.unbound = {
+ settings = {
+ server = {
+ # Otherwise, unbound will never ask the local nsd via the stub zones.
+ do-not-query-localhost = false;
+ };
+
+ # Forward queries for in our zones to the local nsd.
+ # It makes resolves little bit faster for the real deployment,
+ # and importantly breaks the dependency on another ns server.
+ stub-zone = stubZones;
+
+ # Our local zones do not have DNSSEC, so we need to mark them as insecure.
+ domain-insecure = zoneNames;
+ };
+ };
+
+ services.nsd = {
+ enable = true;
+ verbosity = 4;
+ interfaces =
+ (lib.lists.optionals config.vhack.unbound.enable [localInterface])
+ ++ cfg.interfaces;
+ inherit zones;
+ };
+
+ networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall ports;
+ networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall ports;
+
+ systemd.services.nsd = {
+ requires = [
+ "network-online.target"
+ ];
+ after = [
+ "network.target"
+ "network-online.target"
+ ];
+ };
+ };
+}