summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/easytier.nix
blob: 127c72063facf4c64c223b3e32bb870eaefdd47c (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
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
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;
let
  cfg = config.services.easytier;
  settingsFormat = pkgs.formats.toml { };

  genFinalSettings =
    inst:
    attrsets.filterAttrsRecursive (_: v: v != { }) (
      attrsets.filterAttrsRecursive (_: v: v != null) (
        {
          inherit (inst.settings)
            instance_name
            hostname
            ipv4
            dhcp
            listeners
            ;
          network_identity = {
            inherit (inst.settings) network_name network_secret;
          };
          peer = map (p: { uri = p; }) inst.settings.peers;
        }
        // inst.extraSettings
      )
    );

  genConfigFile =
    name: inst:
    if inst.configFile == null then
      settingsFormat.generate "easytier-${name}.toml" (genFinalSettings inst)
    else
      inst.configFile;

  activeInsts = filterAttrs (_: inst: inst.enable) cfg.instances;

  settingsModule = name: {
    options = {
      instance_name = mkOption {
        type = types.str;
        default = name;
        description = "Identify different instances on same host";
      };

      hostname = mkOption {
        type = with types; nullOr str;
        default = null;
        description = "Hostname shown in peer list and web console.";
      };

      network_name = mkOption {
        type = with types; nullOr str;
        default = null;
        description = "EasyTier network name.";
      };

      network_secret = mkOption {
        type = with types; nullOr str;
        default = null;
        description = ''
          EasyTier network credential used for verification and
          encryption. It can also be set in environmentFile.
        '';
      };

      ipv4 = mkOption {
        type = with types; nullOr str;
        default = null;
        description = ''
          IPv4 cidr address of this peer in the virtual network. If
          empty, this peer will only forward packets and no TUN device
          will be created.
        '';
        example = "10.144.144.1/24";
      };

      dhcp = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Automatically determine the IPv4 address of this peer based on
          existing peers on network.
        '';
      };

      listeners = mkOption {
        type = with types; listOf str;
        default = [
          "tcp://0.0.0.0:11010"
          "udp://0.0.0.0:11010"
        ];
        description = ''
          Listener addresses to accept connections from other peers.
          Valid format is: `<proto>://<addr>:<port>`, where the protocol
          can be `tcp`, `udp`, `ring`, `wg`, `ws`, `wss`.
        '';
      };

      peers = mkOption {
        type = with types; listOf str;
        default = [ ];
        description = ''
          Peers to connect initially. Valid format is: `<proto>://<addr>:<port>`.
        '';
        example = [
          "tcp://example.com:11010"
        ];
      };
    };
  };

  instanceModule =
    { name, ... }:
    {
      options = {
        enable = mkOption {
          type = types.bool;
          default = true;
          description = "Enable the instance.";
        };

        configServer = mkOption {
          type = with types; nullOr str;
          default = null;
          description = ''
            Configure the instance from config server. When this option
            set, any other settings for configuring the instance manually
            except `hostname` will be ignored. Valid formats are:

            - full uri for custom server: `udp://example.com:22020/<token>`
            - username only for official server: `<token>`
          '';
          example = "udp://example.com:22020/myusername";
        };

        configFile = mkOption {
          type = with types; nullOr path;
          default = null;
          description = ''
            Path to easytier config file. Setting this option will
            override `settings` and `extraSettings` of this instance.
          '';
        };

        environmentFiles = mkOption {
          type = with types; listOf path;
          default = [ ];
          description = ''
            Environment files for this instance. All command-line args
            have corresponding environment variables.
          '';
          example = literalExpression ''
            [
              /path/to/.env
              /path/to/.env.secret
            ]
          '';
        };

        settings = mkOption {
          type = types.submodule (settingsModule name);
          default = { };
          description = ''
            Settings to generate {file}`easytier-${name}.toml`
          '';
        };

        extraSettings = mkOption {
          type = settingsFormat.type;
          default = { };
          description = ''
            Extra settings to add to {file}`easytier-${name}.toml`.
          '';
        };

        extraArgs = mkOption {
          type = with types; listOf str;
          default = [ ];
          description = ''
            Extra args append to the easytier command-line.
          '';
        };
      };
    };

in
{
  options.services.easytier = {
    enable = mkEnableOption "EasyTier daemon";

    package = mkPackageOption pkgs "easytier" { };

    allowSystemForward = mkEnableOption ''
      Allow the system to forward packets from easytier. Useful when
      `proxy_forward_by_system` enabled.
    '';

    instances = mkOption {
      description = ''
        EasyTier instances.
      '';
      type = types.attrsOf (types.submodule instanceModule);
      default = { };
      example = {
        settings = {
          network_name = "easytier";
          network_secret = "easytier";
          ipv4 = "10.144.144.1/24";
          peers = [
            "tcp://public.easytier.cn:11010"
            "wss://example.com:443"
          ];
        };
        extraSettings = {
          flags.dev_name = "tun1";
        };
      };
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];

    systemd.services = mapAttrs' (
      name: inst:
      let
        configFile = genConfigFile name inst;
      in
      nameValuePair "easytier-${name}" {
        description = "EasyTier Daemon - ${name}";
        wants = [
          "network-online.target"
          "nss-lookup.target"
        ];
        after = [
          "network-online.target"
          "nss-lookup.target"
        ];
        wantedBy = [ "multi-user.target" ];
        path = with pkgs; [
          cfg.package
          iproute2
          bash
        ];
        restartTriggers = inst.environmentFiles ++ (optionals (inst.configServer == null) [ configFile ]);
        serviceConfig = {
          Type = "simple";
          Restart = "on-failure";
          EnvironmentFile = inst.environmentFiles;
          StateDirectory = "easytier/easytier-${name}";
          StateDirectoryMode = "0700";
          WorkingDirectory = "/var/lib/easytier/easytier-${name}";
          ExecStart = escapeShellArgs (
            [
              "${cfg.package}/bin/easytier-core"
            ]
            ++ optionals (inst.configServer != null) (
              [
                "-w"
                "${inst.configServer}"
              ]
              ++ (optionals (inst.settings.hostname != null) [
                "--hostname"
                "${inst.settings.hostname}"
              ])
            )
            ++ optionals (inst.configServer == null) [
              "-c"
              "${configFile}"
            ]
            ++ inst.extraArgs
          );
        };
      }
    ) activeInsts;

    boot.kernel.sysctl = mkIf cfg.allowSystemForward {
      "net.ipv4.conf.all.forwarding" = mkOverride 97 true;
      "net.ipv6.conf.all.forwarding" = mkOverride 97 true;
    };
  };

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