-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrelay-server-module.nix
159 lines (135 loc) · 4.55 KB
/
relay-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
{ config, lib, pkgs, ... }:
let
cfg = config.services.connet-relay-server;
in
{
options.services.connet-relay-server = {
enable = lib.mkEnableOption "connet relay 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.";
};
tokenFile = lib.mkOption {
type = lib.types.path;
description = "The file to read relay token from.";
};
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";
};
controlAddr = lib.mkOption {
type = lib.types.str;
description = "Control server address to connect to";
example = "localhost:19189";
};
controlCA = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.path;
description = "Control server Certificate Authority file to use, required when running self-signed server";
};
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 {
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.relayPort ];
environment.etc."connet-relay-server.toml" = {
user = cfg.user;
group = cfg.group;
source = (pkgs.formats.toml { }).generate "connet-relay-server-config.toml" {
log-level = cfg.logLevel;
log-format = cfg.logFormat;
relay = {
token-file = cfg.tokenFile;
addr = ":${toString cfg.relayPort}";
hostname = cfg.relayHostname;
control-addr = cfg.controlAddr;
store-dir = "/var/lib/connet-relay-server";
} // lib.optionalAttrs (builtins.isPath cfg.controlCA) {
control-cas = cfg.controlCA;
} // lib.optionalAttrs (builtins.isString cfg.statusAddr) {
status-addr = cfg.statusAddr;
};
};
};
systemd.packages = [ cfg.package ];
systemd.services.connet-relay-server = {
description = "connet relay server";
after = [ "network.target" "network-online.target" ];
requires = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
restartTriggers = [ config.environment.etc."connet-relay-server.toml".source ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${cfg.package}/bin/connet relay --config /etc/connet-relay-server.toml";
Restart = "on-failure";
StateDirectory = "connet-relay-server";
StateDirectoryMode = "0700";
};
};
};
}