about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/networking/dnschain.nix
blob: 003609ea7054aaf17fcaac8346de2508bb98b483 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfgs = config.services;
  cfg  = cfgs.dnschain;

  dataDir  = "/var/lib/dnschain";
  username = "dnschain";

  configFile = pkgs.writeText "dnschain.conf" ''
    [log]
    level = info

    [dns]
    host = ${cfg.dns.address}
    port = ${toString cfg.dns.port}
    oldDNSMethod = NO_OLD_DNS
    externalIP = ${cfg.dns.externalAddress}

    [http]
    host = ${cfg.api.hostname}
    port = ${toString cfg.api.port}
    tlsPort = ${toString cfg.api.tlsPort}

    ${cfg.extraConfig}
  '';

in

{

  ###### interface

  options = {

    services.dnschain = {

      enable = mkEnableOption ''
        DNSChain, a blockchain based DNS + HTTP server.
        To resolve .bit domains set <literal>services.namecoind.enable = true;</literal>
        and an RPC username/password.
      '';

      dns.address = mkOption {
        type = types.str;
        default = "127.0.0.1";
        description = ''
          The IP address the DNSChain resolver will bind to.
          Leave this unchanged if you do not wish to directly expose the resolver.
        '';
      };

      dns.externalAddress = mkOption {
        type = types.str;
        default = cfg.dns.address;
        description = ''
           The IP address used by clients to reach the resolver and the value of
           the <literal>namecoin.dns</literal> record. Set this in case the bind address
           is not the actual IP address (e.g. the machine is behind a NAT).
        '';
      };

      dns.port = mkOption {
        type = types.int;
        default = 5333;
        description = ''
          The port the DNSChain resolver will bind to.
        '';
      };

      api.hostname = mkOption {
        type = types.str;
        default = "0.0.0.0";
        description = ''
          The hostname (or IP address) the DNSChain API server will bind to.
        '';
      };

      api.port = mkOption {
        type = types.int;
        default = 8080;
        description = ''
          The port the DNSChain API server (HTTP) will bind to.
        '';
      };

      api.tlsPort = mkOption {
        type = types.int;
        default = 4433;
        description = ''
          The port the DNSChain API server (HTTPS) will bind to.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        example = ''
          [log]
          level = debug
        '';
        description = ''
          Additional options that will be appended to the configuration file.
        '';
      };

    };

    services.dnsmasq.resolveDNSChainQueries = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Resolve <literal>.bit</literal> top-level domains using DNSChain and namecoin.
      '';
    };

    services.pdns-recursor.resolveDNSChainQueries = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Resolve <literal>.bit</literal> top-level domains using DNSChain and namecoin.
      '';
    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    services.dnsmasq.servers = optionals cfgs.dnsmasq.resolveDNSChainQueries
      [ "/.bit/127.0.0.1#${toString cfg.dns.port}"
        "/.dns/127.0.0.1#${toString cfg.dns.port}"
      ];

    services.pdns-recursor = mkIf cfgs.pdns-recursor.resolveDNSChainQueries {
      forwardZonesRecurse =
        { bit = "127.0.0.1:${toString cfg.dns.port}";
          dns = "127.0.0.1:${toString cfg.dns.port}";
        };
      luaConfig =''
        addNTA("bit", "namecoin doesn't support DNSSEC")
        addNTA("dns", "namecoin doesn't support DNSSEC")
      '';
    };

    users.users.${username} = {
      description = "DNSChain daemon user";
      home = dataDir;
      createHome = true;
      uid = config.ids.uids.dnschain;
      extraGroups = optional cfgs.namecoind.enable "namecoin";
    };

    systemd.services.dnschain = {
      description = "DNSChain daemon";
      after    = optional cfgs.namecoind.enable "namecoind.target";
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        User = "dnschain";
        Restart = "on-failure";
        ExecStart = "${pkgs.nodePackages.dnschain}/bin/dnschain";
      };

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

  };

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

}