summary refs log tree commit diff
path: root/nixos/modules/services/networking/pdns-recursor.nix
blob: 6ff181377fccd57c830131f003612e1c0260eaee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{ config, lib, pkgs, ... }:

with lib;

let
  dataDir  = "/var/lib/pdns-recursor";
  username = "pdns-recursor";

  cfg = config.services.pdns-recursor;

  oneOrMore  = type: with types; either type (listOf type);
  valueType  = with types; oneOf [ int str bool path ];
  configType = with types; attrsOf (nullOr (oneOrMore valueType));

  toBool    = val: if val then "yes" else "no";
  serialize = val: with types;
         if str.check       val then val
    else if int.check       val then toString val
    else if path.check      val then toString val
    else if bool.check      val then toBool val
    else if builtins.isList val then (concatMapStringsSep "," serialize val)
    else "";

  configFile = pkgs.writeText "recursor.conf"
    (concatStringsSep "\n"
      (flip mapAttrsToList cfg.settings
        (name: val: "${name}=${serialize val}")));

  mkDefaultAttrs = mapAttrs (n: v: mkDefault v);

in {
  options.services.pdns-recursor = {
    enable = mkEnableOption "PowerDNS Recursor, a recursive DNS server";

    dns.address = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = ''
        IP address Recursor DNS server will bind to.
      '';
    };

    dns.port = mkOption {
      type = types.int;
      default = 53;
      description = ''
        Port number Recursor DNS server will bind to.
      '';
    };

    dns.allowFrom = mkOption {
      type = types.listOf types.str;
      default = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" ];
      example = [ "0.0.0.0/0" ];
      description = ''
        IP address ranges of clients allowed to make DNS queries.
      '';
    };

    api.address = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = ''
        IP address Recursor REST API server will bind to.
      '';
    };

    api.port = mkOption {
      type = types.int;
      default = 8082;
      description = ''
        Port number Recursor REST API server will bind to.
      '';
    };

    api.allowFrom = mkOption {
      type = types.listOf types.str;
      default = [ "0.0.0.0/0" ];
      description = ''
        IP address ranges of clients allowed to make API requests.
      '';
    };

    exportHosts = mkOption {
      type = types.bool;
      default = false;
      description = ''
       Whether to export names and IP addresses defined in /etc/hosts.
      '';
    };

    forwardZones = mkOption {
      type = types.attrs;
      default = {};
      description = ''
        DNS zones to be forwarded to other authoritative servers.
      '';
    };

    forwardZonesRecurse = mkOption {
      type = types.attrs;
      example = { eth = "127.0.0.1:5353"; };
      default = {};
      description = ''
        DNS zones to be forwarded to other recursive servers.
      '';
    };

    dnssecValidation = mkOption {
      type = types.enum ["off" "process-no-validate" "process" "log-fail" "validate"];
      default = "validate";
      description = ''
        Controls the level of DNSSEC processing done by the PowerDNS Recursor.
        See https://doc.powerdns.com/md/recursor/dnssec/ for a detailed explanation.
      '';
    };

    serveRFC1918 = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Whether to directly resolve the RFC1918 reverse-mapping domains:
        <literal>10.in-addr.arpa</literal>,
        <literal>168.192.in-addr.arpa</literal>,
        <literal>16-31.172.in-addr.arpa</literal>
        This saves load on the AS112 servers.
      '';
    };

    settings = mkOption {
      type = configType;
      default = { };
      example = literalExample ''
        {
          loglevel = 8;
          log-common-errors = true;
        }
      '';
      description = ''
        PowerDNS Recursor settings. Use this option to configure Recursor
        settings not exposed in a NixOS option or to bypass one.
        See the full documentation at
        <link xlink:href="https://doc.powerdns.com/recursor/settings.html"/>
        for the available options.
      '';
    };

    luaConfig = mkOption {
      type = types.lines;
      default = "";
      description = ''
        The content Lua configuration file for PowerDNS Recursor. See
        <link xlink:href="https://doc.powerdns.com/recursor/lua-config/index.html"/>.
      '';
    };
  };

  config = mkIf cfg.enable {

    services.pdns-recursor.settings = mkDefaultAttrs {
      local-address = cfg.dns.address;
      local-port    = cfg.dns.port;
      allow-from    = cfg.dns.allowFrom;

      webserver-address    = cfg.api.address;
      webserver-port       = cfg.api.port;
      webserver-allow-from = cfg.api.allowFrom;

      forward-zones         = mapAttrsToList (zone: uri: "${zone}.=${uri}") cfg.forwardZones;
      forward-zones-recurse = mapAttrsToList (zone: uri: "${zone}.=${uri}") cfg.forwardZonesRecurse;
      export-etc-hosts = cfg.exportHosts;
      dnssec           = cfg.dnssecValidation;
      serve-rfc1918    = cfg.serveRFC1918;
      lua-config-file  = pkgs.writeText "recursor.lua" cfg.luaConfig;

      log-timestamp  = false;
      disable-syslog = true;
    };

    users.users.${username} = {
      home = dataDir;
      createHome = true;
      uid = config.ids.uids.pdns-recursor;
      description = "PowerDNS Recursor daemon user";
    };

    systemd.services.pdns-recursor = {
      unitConfig.Documentation = "man:pdns_recursor(1) man:rec_control(1)";
      description = "PowerDNS recursive server";
      wantedBy = [ "multi-user.target" ];
      after    = [ "network.target" ];

      serviceConfig = {
        User = username;
        Restart    ="on-failure";
        RestartSec = "5";
        PrivateTmp = true;
        PrivateDevices = true;
        AmbientCapabilities = "cap_net_bind_service";
        ExecStart = ''${pkgs.pdns-recursor}/bin/pdns_recursor \
          --config-dir=${dataDir} \
          --socket-dir=${dataDir}
        '';
      };

      preStart = ''
        # Link configuration file into recursor home directory
        configPath=${dataDir}/recursor.conf
        if [ "$(realpath $configPath)" != "${configFile}" ]; then
          rm -f $configPath
          ln -s ${configFile} $configPath
        fi
      '';
    };
  };

  imports = [
   (mkRemovedOptionModule [ "services" "pdns-recursor" "extraConfig" ]
     "To change extra Recursor settings use services.pdns-recursor.settings instead.")
  ];

  meta.maintainers = with lib.maintainers; [ rnhmjoj ];

}