summary refs log tree commit diff
path: root/nixos/modules/services/networking/tinc.nix
blob: b6afd83a9abd86c16b4e50d61490f23a826a680e (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.tinc;

  mkValueString = value:
    if value == true then "yes"
    else if value == false then "no"
    else generators.mkValueStringDefault { } value;

  toTincConf = generators.toKeyValue {
    listsAsDuplicateKeys = true;
    mkKeyValue = generators.mkKeyValueDefault { inherit mkValueString; } "=";
  };

  tincConfType = with types;
    let
      valueType = oneOf [ bool str int ];
    in
    attrsOf (either valueType (listOf valueType));

  addressSubmodule = {
    options = {
      address = mkOption {
        type = types.str;
        description = "The external IP address or hostname where the host can be reached.";
      };

      port = mkOption {
        type = types.nullOr types.port;
        default = null;
        description = ''
          The port where the host can be reached.

          If no port is specified, the default Port is used.
        '';
      };
    };
  };

  subnetSubmodule = {
    options = {
      address = mkOption {
        type = types.str;
        description = ''
          The subnet of this host.

          Subnets can either be single MAC, IPv4 or IPv6 addresses, in which case
          a subnet consisting of only that single address is assumed, or they can
          be a IPv4 or IPv6 network address with a prefix length.

          IPv4 subnets are notated like 192.168.1.0/24, IPv6 subnets are notated
          like fec0:0:0:1::/64. MAC addresses are notated like 0:1a:2b:3c:4d:5e.

          Note that subnets like 192.168.1.1/24 are invalid.
        '';
      };

      prefixLength = mkOption {
        type = with types; nullOr (addCheck int (n: n >= 0 && n <= 128));
        default = null;
        description = ''
          The prefix length of the subnet.

          If null, a subnet consisting of only that single address is assumed.

          This conforms to standard CIDR notation as described in RFC1519.
        '';
      };

      weight = mkOption {
        type = types.ints.unsigned;
        default = 10;
        description = ''
          Indicates the priority over identical Subnets owned by different nodes.

          Lower values indicate higher priority. Packets will be sent to the
          node with the highest priority, unless that node is not reachable, in
          which case the node with the next highest priority will be tried, and
          so on.
        '';
      };
    };
  };

  hostSubmodule = { config, ... }: {
    options = {
      addresses = mkOption {
        type = types.listOf (types.submodule addressSubmodule);
        default = [ ];
        description = ''
          The external address where the host can be reached. This will set this
          host's <option>settings.Address</option> option.

          This variable is only required if you want to connect to this host.
        '';
      };

      subnets = mkOption {
        type = types.listOf (types.submodule subnetSubmodule);
        default = [ ];
        description = ''
          The subnets which this tinc daemon will serve. This will set this
          host's <option>settings.Subnet</option> option.

          Tinc tries to look up which other daemon it should send a packet to by
          searching the appropriate subnet. If the packet matches a subnet, it
          will be sent to the daemon who has this subnet in his host
          configuration file.
        '';
      };

      rsaPublicKey = mkOption {
        type = types.str;
        default = "";
        description = ''
          Legacy RSA public key of the host in PEM format, including start and
          end markers.

          This will be appended as-is in the host's configuration file.

          The ed25519 public key can be specified using the
          <option>settings.Ed25519PublicKey</option> option instead.
        '';
      };

      settings = mkOption {
        default = { };
        type = types.submodule { freeformType = tincConfType; };
        description = ''
          Configuration for this host.

          See <link xlink:href="https://tinc-vpn.org/documentation-1.1/Host-configuration-variables.html"/>
          for supported values.
        '';
      };
    };

    config.settings = {
      Address = mkDefault (map
        (address: "${address.address} ${toString address.port}")
        config.addresses);

      Subnet = mkDefault (map
        (subnet:
          if subnet.prefixLength == null then "${subnet.address}#${toString subnet.weight}"
          else "${subnet.address}/${toString subnet.prefixLength}#${toString subnet.weight}")
        config.subnets);
    };
  };

in
{

  ###### interface

  options = {

    services.tinc = {

      networks = mkOption {
        default = { };
        type = with types; attrsOf (submodule ({ config, ... }: {
          options = {

            extraConfig = mkOption {
              default = "";
              type = types.lines;
              description = ''
                Extra lines to add to the tinc service configuration file.

                Note that using the declarative <option>service.tinc.networks.&lt;name&gt;.settings</option>
                option is preferred.
              '';
            };

            name = mkOption {
              default = null;
              type = types.nullOr types.str;
              description = ''
                The name of the node which is used as an identifier when communicating
                with the remote nodes in the mesh. If null then the hostname of the system
                is used to derive a name (note that tinc may replace non-alphanumeric characters in
                hostnames by underscores).
              '';
            };

            ed25519PrivateKeyFile = mkOption {
              default = null;
              type = types.nullOr types.path;
              description = ''
                Path of the private ed25519 keyfile.
              '';
            };

            rsaPrivateKeyFile = mkOption {
              default = null;
              type = types.nullOr types.path;
              description = ''
                Path of the private RSA keyfile.
              '';
            };

            debugLevel = mkOption {
              default = 0;
              type = types.addCheck types.int (l: l >= 0 && l <= 5);
              description = ''
                The amount of debugging information to add to the log. 0 means little
                logging while 5 is the most logging. <command>man tincd</command> for
                more details.
              '';
            };

            hosts = mkOption {
              default = { };
              type = types.attrsOf types.lines;
              description = ''
                The name of the host in the network as well as the configuration for that host.
                This name should only contain alphanumerics and underscores.

                Note that using the declarative <option>service.tinc.networks.&lt;name&gt;.hostSettings</option>
                option is preferred.
              '';
            };

            hostSettings = mkOption {
              default = { };
              example = literalExample ''
                {
                  host1 = {
                    addresses = [
                      { address = "192.168.1.42"; }
                      { address = "192.168.1.42"; port = 1655; }
                    ];
                    subnets = [ { address = "10.0.0.42"; } ];
                    rsaPublicKey = "...";
                    settings = {
                      Ed25519PublicKey = "...";
                    };
                  };
                  host2 = {
                    subnets = [ { address = "10.0.1.0"; prefixLength = 24; weight = 2; } ];
                    rsaPublicKey = "...";
                    settings = {
                      Compression = 10;
                    };
                  };
                }
              '';
              type = types.attrsOf (types.submodule hostSubmodule);
              description = ''
                The name of the host in the network as well as the configuration for that host.
                This name should only contain alphanumerics and underscores.
              '';
            };

            interfaceType = mkOption {
              default = "tun";
              type = types.enum [ "tun" "tap" ];
              description = ''
                The type of virtual interface used for the network connection.
              '';
            };

            listenAddress = mkOption {
              default = null;
              type = types.nullOr types.str;
              description = ''
                The ip address to listen on for incoming connections.
              '';
            };

            bindToAddress = mkOption {
              default = null;
              type = types.nullOr types.str;
              description = ''
                The ip address to bind to (both listen on and send packets from).
              '';
            };

            package = mkOption {
              type = types.package;
              default = pkgs.tinc_pre;
              defaultText = "pkgs.tinc_pre";
              description = ''
                The package to use for the tinc daemon's binary.
              '';
            };

            chroot = mkOption {
              default = true;
              type = types.bool;
              description = ''
                Change process root directory to the directory where the config file is located (/etc/tinc/netname/), for added security.
                The chroot is performed after all the initialization is done, after writing pid files and opening network sockets.

                Note that tinc can't run scripts anymore (such as tinc-down or host-up), unless it is setup to be runnable inside chroot environment.
              '';
            };

            settings = mkOption {
              default = { };
              type = types.submodule { freeformType = tincConfType; };
              example = literalExample ''
                {
                  Interface = "custom.interface";
                  DirectOnly = true;
                  Mode = "switch";
                }
              '';
              description = ''
                Configuration of the Tinc daemon for this network.

                See <link xlink:href="https://tinc-vpn.org/documentation-1.1/Main-configuration-variables.html"/>
                for supported values.
              '';
            };
          };

          config = {
            hosts = mapAttrs
              (hostname: host: ''
                ${toTincConf host.settings}
                ${host.rsaPublicKey}
              '')
              config.hostSettings;

            settings = {
              DeviceType = mkDefault config.interfaceType;
              Name = mkDefault (if config.name == null then "$HOST" else config.name);
              Ed25519PrivateKeyFile = mkIf (config.ed25519PrivateKeyFile != null) (mkDefault config.ed25519PrivateKeyFile);
              PrivateKeyFile = mkIf (config.rsaPrivateKeyFile != null) (mkDefault config.rsaPrivateKeyFile);
              ListenAddress = mkIf (config.listenAddress != null) (mkDefault config.listenAddress);
              BindToAddress = mkIf (config.bindToAddress != null) (mkDefault config.bindToAddress);
            };
          };
        }));

        description = ''
          Defines the tinc networks which will be started.
          Each network invokes a different daemon.
        '';
      };
    };

  };


  ###### implementation

  config = mkIf (cfg.networks != { }) {

    environment.etc = fold (a: b: a // b) { }
      (flip mapAttrsToList cfg.networks (network: data:
        flip mapAttrs' data.hosts (host: text: nameValuePair
          ("tinc/${network}/hosts/${host}")
          ({ mode = "0644"; user = "tinc.${network}"; inherit text; })
        ) // {
          "tinc/${network}/tinc.conf" = {
            mode = "0444";
            text = ''
              ${toTincConf ({ Interface = "tinc.${network}"; } // data.settings)}
              ${data.extraConfig}
            '';
          };
        }
      ));

    systemd.services = flip mapAttrs' cfg.networks (network: data: nameValuePair
      ("tinc.${network}")
      ({
        description = "Tinc Daemon - ${network}";
        wantedBy = [ "multi-user.target" ];
        path = [ data.package ];
        restartTriggers = [ config.environment.etc."tinc/${network}/tinc.conf".source ];
        serviceConfig = {
          Type = "simple";
          Restart = "always";
          RestartSec = "3";
          ExecReload = mkIf (versionAtLeast (getVersion data.package) "1.1pre") "${data.package}/bin/tinc -n ${network} reload";
          ExecStart = "${data.package}/bin/tincd -D -U tinc.${network} -n ${network} ${optionalString (data.chroot) "-R"} --pidfile /run/tinc.${network}.pid -d ${toString data.debugLevel}";
        };
        preStart = ''
          mkdir -p /etc/tinc/${network}/hosts
          chown tinc.${network} /etc/tinc/${network}/hosts
          mkdir -p /etc/tinc/${network}/invitations
          chown tinc.${network} /etc/tinc/${network}/invitations

          # Determine how we should generate our keys
          if type tinc >/dev/null 2>&1; then
            # Tinc 1.1+ uses the tinc helper application for key generation
          ${if data.ed25519PrivateKeyFile != null then "  # ed25519 Keyfile managed by nix" else ''
            # Prefer ED25519 keys (only in 1.1+)
            [ -f "/etc/tinc/${network}/ed25519_key.priv" ] || tinc -n ${network} generate-ed25519-keys
          ''}
          ${if data.rsaPrivateKeyFile != null then "  # RSA Keyfile managed by nix" else ''
            [ -f "/etc/tinc/${network}/rsa_key.priv" ] || tinc -n ${network} generate-rsa-keys 4096
          ''}
            # In case there isn't anything to do
            true
          else
            # Tinc 1.0 uses the tincd application
            [ -f "/etc/tinc/${network}/rsa_key.priv" ] || tincd -n ${network} -K 4096
          fi
        '';
      })
    );

    environment.systemPackages = let
      cli-wrappers = pkgs.stdenv.mkDerivation {
        name = "tinc-cli-wrappers";
        buildInputs = [ pkgs.makeWrapper ];
        buildCommand = ''
          mkdir -p $out/bin
          ${concatStringsSep "\n" (mapAttrsToList (network: data:
            optionalString (versionAtLeast data.package.version "1.1pre") ''
              makeWrapper ${data.package}/bin/tinc "$out/bin/tinc.${network}" \
                --add-flags "--pidfile=/run/tinc.${network}.pid" \
                --add-flags "--config=/etc/tinc/${network}"
            '') cfg.networks)}
        '';
      };
    in [ cli-wrappers ];

    users.users = flip mapAttrs' cfg.networks (network: _:
      nameValuePair ("tinc.${network}") ({
        description = "Tinc daemon user for ${network}";
        isSystemUser = true;
      })
    );

  };

  meta.maintainers = with maintainers; [ minijackson ];
}