-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.nix
148 lines (128 loc) · 4.34 KB
/
service.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
{ config, lib, self, ... }:
let
packages = config.flake.packages.x86_64-linux;
# TODO: dry
toArgs = args @ { commands ? [ ], ... }:
lib.escapeShellArgs
(lib.cli.toGNUCommandLine
{ mkOptionName = k: "-${k}"; }
(builtins.removeAttrs args [ "commands" ])
++ (map (c: "+${c}") commands));
in
{
flake.nixosModules = {
tf2ds = { config, lib, pkgs, ... }:
let
inherit (lib)
mkOption
types
;
cfg = config.services.tf2ds;
mkService = name: opts:
let
args = {
game = "tf";
ip = "0.0.0.0";
scriptportbind = true;
inherit (opts) port;
enablefakeip = true;
}
// opts.args
// {
commands = [
''hostname "i71.tf - ${name}"''
"tv_port ${toString opts.stvPort}"
"clientport ${toString (50000 + opts.port)}"
]
++ opts.args.commands;
};
in
{
"tf2ds-${name}" = {
description = "Team Fortress 2 Dedicated Server - ${name}";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
inherit (opts) restartIfChanged;
preStart = ''
mkdir -p tf/{addons,cfg} .steam/sdk32
${pkgs.findutils}/bin/find . -type l -delete
ln -fns ${packages.gameinfo} tf/gameinfo.txt
ln -fns ${packages.tf2ds}/bin/steamclient.so .steam/sdk32/
ln -fns ${config.age.secrets."apikeys.cfg".path} tf/cfg/
${lib.getExe pkgs.jq} -r --arg name "${name}" '
.[$name] // [] | to_entries[] | "\(.key) \"\(.value)\""
' ${config.age.secrets."passwords.json".path} > tf/cfg/passwords.cfg
${lib.getExe pkgs.xorg.lndir} -silent ${packages.all-plugins} ./
'';
script = ''
HOME=$STATE_DIRECTORY \
LD_LIBRARY_PATH=${packages.tf2ds}/bin:${pkgs.pkgsi686Linux.ncurses5}/lib \
exec -a "$STATE_DIRECTORY"/srcds_linux \
${packages.tf2ds}/srcds_linux \
${toArgs args}
'';
serviceConfig = {
# ExecStop = ''${lib.getExe pkgs.rcon} -H 127.0.0.1 -p ${toString opts.port} -P $(${lib.getExe pkgs.jq} -r --arg name "${name}" '.[$name].rcon_password' ${config.age.secrets."passwords.json".path}) 'quit' '';
Restart = "always";
DynamicUser = "true";
SupplementaryGroups = "tf2ds";
StateDirectory = "tf2ds/${name}";
WorkingDirectory = "%S/tf2ds/${name}";
};
};
};
in
{
options.services.tf2ds = {
instances = mkOption {
type = types.attrsOf (types.submodule {
options = {
port = mkOption { type = types.port; };
stvPort = mkOption { type = types.port; };
restartIfChanged = mkOption {
type = types.bool;
default = false;
};
args = mkOption {
type = types.raw;
default = {
commands = [
"sv_pure 2"
"map itemtest"
];
};
};
};
});
default = { };
};
};
config = {
age.secrets = {
"apikeys.cfg" = {
file = "${self}/cfg/apikeys.cfg.age";
mode = "0440";
group = "tf2ds";
};
"passwords.json" = {
file = "${self}/passwords.json.age";
mode = "0440";
group = "tf2ds";
};
};
networking.firewall = lib.mkMerge (
lib.mapAttrsToList
(_: opts: {
allowedTCPPorts = [ opts.port ];
allowedUDPPorts = [ opts.port opts.stvPort ];
})
cfg.instances
);
systemd.services = lib.mkMerge (
lib.mapAttrsToList mkService cfg.instances
);
users.groups.tf2ds = { };
};
};
};
}