Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
329 lines (290 loc) · 12.4 KB
/
Copy pathflake.nix
File metadata and controls
329 lines (290 loc) · 12.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{
description = "Generic SteamCMD dedicated server host";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
outputs =
{ self, nixpkgs }:
let
# Built-in presets. Each preset is a partial set of submodule options
# which are applied via mkDefault, so per-server config still overrides
# individual fields. Add new games here.
presets = {
palworld = {
appId = 2394010;
executable = "PalServer.sh";
args = [
"-useperfthreads"
"-NoAsyncLoadingThread"
"-UseMultithreadForDS"
];
udpPorts = [
8211
27015
];
tailscaleTcpPorts = [ 25575 ]; # RCON
};
};
in
{
inherit presets;
nixosModules.default =
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.steamcmd-servers;
enabledServers = filterAttrs (_: s: s.enable) cfg;
# Wrap any executable with steam-run so it sees the FHS layout / 32-bit
# libs that prebuilt Steam game-server binaries expect.
runner = serverCfg: if serverCfg.useSteamRun then "${pkgs.steam-run}/bin/steam-run" else "";
in
{
options.services.steamcmd-servers = mkOption {
default = { };
description = "SteamCMD-installed dedicated server instances";
type = types.attrsOf (
types.submodule (
{ name, config, ... }:
{
options = {
enable = mkEnableOption "this SteamCMD server instance";
preset = mkOption {
type = types.nullOr (types.enum (attrNames presets));
default = null;
description = ''
Use a built-in preset as defaults for this server. Any
option you set explicitly still overrides the preset's
value for that field.
'';
};
appId = mkOption {
type = types.int;
description = "Steam App ID of the dedicated server to install (e.g. 2394010 for Palworld).";
};
executable = mkOption {
type = types.str;
description = "Server executable, relative to the install directory (e.g. \"PalServer.sh\").";
};
args = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Arguments passed to the server executable.";
};
workingSubdir = mkOption {
type = types.str;
default = "";
description = "Working directory for the server, relative to the install dir. Empty = install dir itself.";
};
betaBranch = mkOption {
type = types.nullOr types.str;
default = null;
description = "Optional Steam beta branch (-beta <branch>).";
};
betaPassword = mkOption {
type = types.nullOr types.str;
default = null;
description = "Optional beta branch password (-betapassword <pw>).";
};
autoUpdate = mkOption {
type = types.bool;
default = true;
description = "Run steamcmd app_update on every service start.";
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = "Open tcpPorts/udpPorts on the global firewall.";
};
tcpPorts = mkOption {
type = types.listOf types.port;
default = [ ];
description = "TCP ports this server listens on (opened globally if openFirewall is true).";
};
udpPorts = mkOption {
type = types.listOf types.port;
default = [ ];
description = "UDP ports this server listens on (opened globally if openFirewall is true).";
};
tailscaleTcpPorts = mkOption {
type = types.listOf types.port;
default = [ ];
description = "TCP ports to open only on the tailscale0 interface (e.g. RCON).";
};
tailscaleUdpPorts = mkOption {
type = types.listOf types.port;
default = [ ];
description = "UDP ports to open only on the tailscale0 interface.";
};
environment = mkOption {
type = types.attrsOf types.str;
default = { };
description = "Extra environment variables passed to the server process.";
};
extraPackages = mkOption {
type = types.listOf types.package;
default = [ ];
description = "Extra packages to put on PATH for this server.";
};
useSteamRun = mkOption {
type = types.bool;
default = true;
description = "Wrap the executable with steam-run for FHS compatibility.";
};
preStartScript = mkOption {
type = types.lines;
default = "";
description = "Extra shell run as the server user before launch (cwd = install dir).";
};
};
# Apply preset values via mkDefault so per-server config still
# wins. Keys must be enumerated statically — making the set of
# defined attributes depend on `config.preset` causes infinite
# recursion in the module system.
config =
let
preset = if config.preset != null then presets.${config.preset} else { };
presetableOptions = [
"appId"
"executable"
"args"
"workingSubdir"
"betaBranch"
"betaPassword"
"autoUpdate"
"openFirewall"
"tcpPorts"
"udpPorts"
"tailscaleTcpPorts"
"tailscaleUdpPorts"
"environment"
"extraPackages"
"useSteamRun"
"preStartScript"
];
in
genAttrs presetableOptions (n: mkIf (preset ? ${n}) (mkDefault preset.${n}));
}
)
);
};
config = mkIf (enabledServers != { }) {
users.users.steam = {
isSystemUser = true;
group = "steam";
home = "/srv/steamcmd";
createHome = true;
};
users.groups.steam = { };
# Members can attach to running screen consoles without a password.
users.groups.steam-admin = { };
security.sudo.extraRules = [
{
groups = [ "steam-admin" ];
runAs = "steam";
commands = [
{
command = "${pkgs.screen}/bin/screen -r steam-*";
options = [ "NOPASSWD" ];
}
];
}
];
nixpkgs.config.allowUnfree = true;
networking.firewall.allowedTCPPorts = lib.flatten (
mapAttrsToList (_: s: s.tcpPorts) (filterAttrs (_: s: s.openFirewall) enabledServers)
);
networking.firewall.allowedUDPPorts = lib.flatten (
mapAttrsToList (_: s: s.udpPorts) (filterAttrs (_: s: s.openFirewall) enabledServers)
);
networking.firewall.interfaces.tailscale0 = {
allowedTCPPorts = lib.flatten (mapAttrsToList (_: s: s.tailscaleTcpPorts) enabledServers);
allowedUDPPorts = lib.flatten (mapAttrsToList (_: s: s.tailscaleUdpPorts) enabledServers);
};
systemd.tmpfiles.rules = [
"d /srv/steamcmd 2770 steam steam-admin -"
]
++ (mapAttrsToList (name: _: "d /srv/steamcmd/${name} 2770 steam steam-admin -") enabledServers);
systemd.services = mapAttrs' (
name: serverCfg:
let
serverDir = "/srv/steamcmd/${name}";
workDir =
if serverCfg.workingSubdir == "" then serverDir else "${serverDir}/${serverCfg.workingSubdir}";
betaArgs =
lib.optionalString (serverCfg.betaBranch != null) " -beta ${serverCfg.betaBranch}"
+ lib.optionalString (serverCfg.betaPassword != null) " -betapassword ${serverCfg.betaPassword}";
updateScript = pkgs.writeShellScript "steamcmd-update-${name}" ''
set -e
exec ${pkgs.steamcmd}/bin/steamcmd \
+force_install_dir ${serverDir} \
+login anonymous \
+app_update ${toString serverCfg.appId}${betaArgs} validate \
+quit
'';
argString = lib.concatStringsSep " " (map lib.escapeShellArg serverCfg.args);
in
nameValuePair "steamcmd-${name}" {
description = "SteamCMD dedicated server (${name}, appid ${toString serverCfg.appId})";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
path = [
pkgs.bash
pkgs.coreutils
pkgs.screen
pkgs.steamcmd
]
++ lib.optional serverCfg.useSteamRun pkgs.steam-run
++ serverCfg.extraPackages;
environment = serverCfg.environment;
preStart = ''
mkdir -p ${serverDir}
${lib.optionalString (serverCfg.workingSubdir != "") "mkdir -p ${workDir}"}
${lib.optionalString serverCfg.autoUpdate ''
${updateScript}
''}
cd ${workDir}
${serverCfg.preStartScript}
'';
script = ''
cd ${workDir}
exec ${pkgs.screen}/bin/screen -DmS steam-${name} \
${runner serverCfg} ${serverDir}/${serverCfg.executable} ${argString}
'';
serviceConfig = {
User = "steam";
Group = "steam";
WorkingDirectory = serverDir;
Restart = "always";
RestartSec = "10s";
TimeoutStartSec = "30min"; # steamcmd downloads can take a while
TimeoutStopSec = "60s";
KillSignal = "SIGTERM";
};
}
) enabledServers;
environment.systemPackages =
(mapAttrsToList (
name: _:
pkgs.writeShellScriptBin "console-${name}" ''
while true; do
TERM=xterm sudo -u steam ${pkgs.screen}/bin/screen -r steam-${name}
echo "Screen session detached or unavailable, retrying in 3 seconds..."
sleep 3
done
''
) enabledServers)
++ (mapAttrsToList (
name: _:
pkgs.writeShellScriptBin "edit-${name}" ''
cd /srv/steamcmd/${name}
exec ''${EDITOR:-nano} .
''
) enabledServers);
};
};
};
}