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

with lib;

let

  cfg = config.services.nebula;
  nebulaDesc = "Nebula VPN service";

  format = pkgs.formats.yaml {};
  configFile = format.generate "nebula-config.yml" cfg.settings;

in
{
  # Interface

  options.services.nebula = {
    enable = mkEnableOption nebulaDesc;

    package = mkOption {
      type = types.package;
      default = pkgs.nebula;
      defaultText = "pkgs.nebula";
      description = "Nebula derivation to use.";
    };

    ca = mkOption {
      type = types.path;
      description = "Path to the certificate authority certificate.";
      example = "/etc/nebula/ca.crt";
    };

    cert = mkOption {
      type = types.path;
      description = "Path to the host certificate.";
      example = "/etc/nebula/host.crt";
    };

    key = mkOption {
      type = types.path;
      description = "Path to the host key.";
      example = "/etc/nebula/host.key";
    };

    staticHostMap = mkOption {
      type = types.attrsOf (types.listOf (types.str));
      default = {};
      description = ''
        The static host map defines a set of hosts with fixed IP addresses on the internet (or any network).
        A host can have multiple fixed IP addresses defined here, and nebula will try each when establishing a tunnel.
      '';
      example = literalExample ''
        { "192.168.100.1" = [ "100.64.22.11:4242" ]; }
      '';
    };

    isLighthouse = mkOption {
      type = types.bool;
      default = false;
      description = "Whether this node is a lighthouse.";
    };

    lighthouses = mkOption {
      type = types.listOf types.str;
      default = [];
      description = ''
        List of IPs of lighthouse hosts this node should report to and query from. This should be empty on lighthouse
        nodes. The IPs should be the lighthouse's Nebula IPs, not their external IPs.
      '';
      example = ''[ "192.168.100.1" ]'';
    };

    listen.host = mkOption {
      type = types.str;
      default = "0.0.0.0";
      description = "IP address to listen on.";
    };

    listen.port = mkOption {
      type = types.port;
      default = 4242;
      description = "Port number to listen on.";
    };

    punch = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Continues to punch inbound/outbound at a regular interval to avoid expiration of firewall nat mappings.
      '';
    };

    tun.disable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root).
      '';
    };

    tun.device = mkOption {
      type = types.str;
      default = "nebula1";
      description = "Name of the tun device.";
    };

    firewall.outbound = mkOption {
      type = types.listOf types.attrs;
      default = [];
      description = "Firewall rules for outbound traffic.";
      example = ''[ { port = "any"; proto = "any"; host = "any"; } ]'';
    };

    firewall.inbound = mkOption {
      type = types.listOf types.attrs;
      default = [];
      description = "Firewall rules for inbound traffic.";
      example = ''[ { port = "any"; proto = "any"; host = "any"; } ]'';
    };

    settings = {
      type = format.type;
      default = {};
      description = ''
        Nebula configuration. Refer to
        <link xlink:href="https://github.com/slackhq/nebula/blob/master/examples/config.yml"/>
        for details on supported values.
      '';
      example = literalExample ''
        {
          lighthouse.dns = {
            host = "0.0.0.0";
            port = 53;
          };
        }
      '';
    };
  };

  # Implementation

  config = mkIf cfg.enable {
    services.nebula.settings = {
      pki = {
        ca = cfg.ca;
        cert = cfg.cert;
        key = cfg.key;
      };
      static_host_map = cfg.staticHostMap;
      lighthouse = {
        am_lighthouse = cfg.isLighthouse;
        hosts = cfg.lighthouses;
      };
      listen = {
        host = cfg.listen.host;
        port = cfg.listen.port;
      };
      punchy = {
        punch = cfg.punch;
      };
      tun = {
        disabled = cfg.tun.disable;
        dev = cfg.tun.device;
      };
      firewall = {
        inbound = cfg.firewall.inbound;
        outbound = cfg.firewall.outbound;
      };
    };

    # Create systemd service for Nebula.
    systemd.services.nebula = {
      description = nebulaDesc;
      after = [ "network.target" ];
      before = [ "sshd.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = mkMerge [
        {
          Type = "simple";
          Restart = "always";
          ExecStart = "${cfg.package}/bin/nebula -config ${configFile}";
        }
        # The service needs to launch as root to access the tun device, if it's enabled.
        (mkIf cfg.tun.disable {
          User = "nebula";
          Group = "nebula";
        })
      ];
    };

    # Open the chosen port for UDP.
    networking.firewall.allowedUDPPorts = [ cfg.listen.port ];

    # Create the service user and its group.
    users = mkIf cfg.tun.disable {
      users.nebula = {
        group = "nebula";
        description = "Nebula service user";
        isSystemUser = true;
        packages = [ cfg.package ];
      };

      groups.nebula = {};
    };
  };
}