summaryrefslogtreecommitdiffstats
path: root/common/tinc.nix
blob: d86b19cbe85fa78c7d8cee755615627035d18038 (plain)
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
{ config, lib, ... }:

with lib;
with builtins;
let
  networks = attrNames (readDir ./tinc/private);
in {
  imports = map (network: (./tinc/private + "/${network}")) networks;

  options.topology = {
    mainVpn = {
      interfaceName = mkOption {
        type = types.str;
        description = "Interface name of the main VPN";
      };

      subnet = mkOption {
        type = types.str;
        description = "CIDR subnet of the main VPN";
      };

      currentNodeIP = mkOption {
        type = types.str;
        description = "The current node's IP address in the VPN";
      };
    };
  };

  config = {
    services.tinc.networks = genAttrs networks (network: {
      name = config.networking.hostName;

      hosts = let
        hosts = attrNames (readDir (./tinc/private + "/${network}/hosts"));
        config = hostname: readFile (./tinc/private + "/${network}/hosts/${hostname}");
      in genAttrs hosts config;
    });

    networking.firewall.allowedUDPPorts = [ 655 ];
    networking.firewall.allowedTCPPorts = [ 655 ];
  };
}