-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver-module.nix
207 lines (178 loc) · 6.25 KB
/
server-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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{ config, lib, pkgs, ... }:
let
cfg = config.services.connet-server;
in
{
options.services.connet-server = {
enable = lib.mkEnableOption "connet server";
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 = "Server log level to use.";
};
logFormat = lib.mkOption {
default = "text";
type = lib.types.enum [ "text" "json" ];
description = "Server log format to use.";
};
tokensFile = lib.mkOption {
type = lib.types.path;
description = "The file to read client tokens from.";
};
clientsPort = lib.mkOption {
default = 19190;
type = lib.types.port;
description = "The port to listen for incoming client connections.";
};
useACMEHost = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
description = ''
A host of an existing ACME certificate to use.
*Note that this option does not create any certificates, nor
does it add subdomains to existing ones – you will need to create them
manually using [](#opt-security.acme.certs).*
'';
example = "example.com";
};
serverCertFile = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.path;
description = "Server certificate file to use";
};
serverKeyFile = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.path;
description = "Server private key file to use";
};
relayPort = lib.mkOption {
default = 19191;
type = lib.types.port;
description = "The port to listen for incoming relay connections.";
};
relayHostname = lib.mkOption {
type = lib.types.str;
description = "Relay hostname to advertise to clients.";
example = "localhost";
};
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
:::
'';
};
};
config = lib.mkIf cfg.enable {
warnings = [ ]
++ lib.optionals (builtins.isString cfg.useACMEHost && builtins.isPath cfg.serverCertFile)
[ "When both useACMEHost and serverCertFile are set, connet will prefer useACMEHost" ];
assertions = [
{
assertion = builtins.isNull cfg.useACMEHost && builtins.isNull cfg.serverCertFile;
message = "connet server requires certificate, either provide useACMEHost or serverCertFile/serverKeyFile";
}
{
assertion = builtins.isPath cfg.serverCertFile && builtins.isNull cfg.serverKeyFile;
message = "serverKeyFile is required when serverCertFile is set";
}
{
assertion = builtins.isNull cfg.serverCertFile && builtins.isPath cfg.serverKeyFile;
message = "serverCertFile is required when serverKeyFile is set";
}
];
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.clientsPort cfg.relayPort ];
environment.etc."connet-server.toml" = {
user = cfg.user;
group = cfg.group;
source = (pkgs.formats.toml { }).generate "connet-server-config.toml" {
log-level = cfg.logLevel;
log-format = cfg.logFormat;
server = {
tokens-file = cfg.tokenFile;
addr = ":${toString cfg.clientsPort}";
relay-addr = ":${toString cfg.relayPort}";
relay-hostname = cfg.relayHostname;
store-dir = "/var/lib/connet-server";
} // (if (builtins.isString cfg.useACMEHost) then
let
sslCertDir = config.security.acme.certs.${cfg.useACMEHost}.directory;
in
{
cert-file = "${sslCertDir}/cert.pem";
key-file = "${sslCertDir}/key.pem";
}
else {
cert-file = cfg.serverCertFile;
key-file = cfg.serverKeyFile;
}) // lib.optionalAttrs (builtins.isString cfg.statusAddr) {
status-addr = cfg.statusAddr;
};
};
};
systemd.packages = [ cfg.package ];
systemd.services.connet-server = {
description = "connet server";
after = [ "network.target" "network-online.target" ];
requires = [ "network-online.target" ]
++ lib.optional (builtins.isString cfg.useACMEHost) [ "acme-finished-${cfg.useACMEHost}.target" ];
wantedBy = [ "multi-user.target" ];
restartTriggers = [ config.environment.etc."connet-server.toml".source ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${cfg.package}/bin/connet server --config /etc/connet-server.toml";
Restart = "on-failure";
StateDirectory = "connet-server";
StateDirectoryMode = "0700";
};
};
};
}