-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient-module.nix
192 lines (168 loc) · 5.32 KB
/
client-module.nix
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
{ config, lib, pkgs, ... }:
let
cfg = config.services.connet;
in
{
options.services.connet = {
enable = lib.mkEnableOption "connet client";
package = lib.mkOption {
default = pkgs.callPackage ./package.nix { };
type = lib.types.package;
};
user = lib.mkOption {
default = "connet";
type = lib.types.str;
description = ''
User account under which connet runs.
::: {.note}
If left as the default value this user will automatically be created
on system activation, otherwise you are responsible for
ensuring the user exists before the connet service starts.
:::
'';
};
group = lib.mkOption {
default = "connet";
type = lib.types.str;
description = ''
Group under which connet runs.
::: {.note}
If left as the default value this group will automatically be created
on system activation, otherwise you are responsible for
ensuring the group exists before the connet service starts.
:::
'';
};
openFirewall = lib.mkOption {
default = false;
type = lib.types.bool;
description = "Whether to open the firewall for the specified port.";
};
logLevel = lib.mkOption {
default = "info";
type = lib.types.enum [ "debug" "info" "warn" "error" ];
description = "Client log level to use";
};
logFormat = lib.mkOption {
default = "text";
type = lib.types.enum [ "text" "json" ];
description = "Client log format to use";
};
tokenFile = lib.mkOption {
type = lib.types.path;
description = "The file to read the token from";
};
serverAddr = lib.mkOption {
type = lib.types.str;
description = "Server address to connect to";
example = "localhost:19190";
};
serverCA = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.path;
description = "Server Certificate Authority file to use, required when running self-signed server";
};
directPort = lib.mkOption {
default = 19192;
type = lib.types.port;
description = "The port to listen for incoming direct connections.";
};
statusAddr = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
description = ''
The address to listen for status connections.
::: {.note}
openFirewall will not open the port of the status address
:::
'';
};
destinations = lib.mkOption {
default = { };
type = lib.types.attrsOf
(lib.types.submodule {
options = {
addr = lib.mkOption {
type = lib.types.str;
description = "The address of the destination";
};
route = lib.mkOption {
default = "any";
type = lib.types.enum [ "any" "direct" "relay" ];
description = "The route to use for this destination";
};
};
});
example = ''
express.addr = "localhost:3000";
'';
};
sources = lib.mkOption {
default = { };
type = lib.types.attrsOf
(lib.types.submodule {
options = {
addr = lib.mkOption {
type = lib.types.str;
description = "The address of the source";
};
route = lib.mkOption {
default = "any";
type = lib.types.enum [ "any" "direct" "relay" ];
description = "The route to use for this source";
};
};
});
example = ''
express.addr = ":8000";
'';
};
};
config = lib.mkIf cfg.enable {
boot.kernel.sysctl."net.core.rmem_max" = lib.mkDefault 7500000;
boot.kernel.sysctl."net.core.wmem_max" = lib.mkDefault 7500000;
users.users = lib.optionalAttrs (cfg.user == "connet") {
connet = {
isSystemUser = true;
group = cfg.group;
};
};
users.groups = lib.optionalAttrs (cfg.group == "connet") {
connet = { };
};
networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [ cfg.directPort ];
environment.etc."connet.toml" = {
user = cfg.user;
group = cfg.group;
source = (pkgs.formats.toml { }).generate "connet-config.toml" {
log-level = cfg.logLevel;
log-format = cfg.logFormat;
client = {
token-file = cfg.tokenFile;
server-addr = cfg.serverAddr;
direct-addr = ":${toString cfg.directPort}";
destinations = cfg.destinations;
sources = cfg.sources;
} // lib.optionalAttrs (builtins.isPath cfg.serverCA) {
server-cas = cfg.serverCA;
} // lib.optionalAttrs (builtins.isString cfg.statusAddr) {
status-addr = cfg.statusAddr;
};
};
};
systemd.packages = [ cfg.package ];
systemd.services.connet = {
description = "connet client";
after = [ "network.target" "network-online.target" ];
requires = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
restartTriggers = [ config.environment.etc."connet.toml".source ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${cfg.package}/bin/connet --config /etc/connet.toml";
Restart = "on-failure";
};
};
};
}