blob: b0726dd5fafedd2666c23e315e4f7b5f19af4c60 (
plain)
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
|
{
config,
lib,
options,
pkgs,
...
}:
let
cfg = config.services.terraria;
opt = options.services.terraria;
worldSizeMap = {
small = 1;
medium = 2;
large = 3;
};
valFlag =
name: val:
lib.optionalString (val != null) "-${name} \"${lib.escape [ "\\" "\"" ] (toString val)}\"";
boolFlag = name: val: lib.optionalString val "-${name}";
flags = [
(valFlag "port" cfg.port)
(valFlag "maxPlayers" cfg.maxPlayers)
(valFlag "password" cfg.password)
(valFlag "motd" cfg.messageOfTheDay)
(valFlag "world" cfg.worldPath)
(valFlag "autocreate" (builtins.getAttr cfg.autoCreatedWorldSize worldSizeMap))
(valFlag "banlist" cfg.banListPath)
(boolFlag "secure" cfg.secure)
(boolFlag "noupnp" cfg.noUPnP)
];
tmuxCmd = "${lib.getExe pkgs.tmux} -S ${lib.escapeShellArg cfg.dataDir}/terraria.sock";
stopScript = pkgs.writeShellScript "terraria-stop" ''
if ! [ -d "/proc/$1" ]; then
exit 0
fi
lastline=$(${tmuxCmd} capture-pane -p | grep . | tail -n1)
# If the service is not configured to auto-start a world, it will show the world selection prompt
# If the last non-empty line on-screen starts with "Choose World", we know the prompt is open
if [[ "$lastline" =~ ^'Choose World' ]]; then
# In this case, nothing needs to be saved, so we can kill the process
${tmuxCmd} kill-session
else
# Otherwise, we send the `exit` command
${tmuxCmd} send-keys Enter exit Enter
fi
# Wait for the process to stop
tail --pid="$1" -f /dev/null
'';
in
{
options = {
services.terraria = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
If enabled, starts a Terraria server. The server can be connected to via `tmux -S ''${config.${opt.dataDir}}/terraria.sock attach`
for administration by users who are a part of the `terraria` group (use `C-b d` shortcut to detach again).
'';
};
package = lib.mkPackageOption pkgs "terraria" {
default = "terraria-server";
};
port = lib.mkOption {
type = lib.types.port;
default = 7777;
description = ''
Specifies the port to listen on.
'';
};
maxPlayers = lib.mkOption {
type = lib.types.ints.u8;
default = 255;
description = ''
Sets the max number of players (between 1 and 255).
'';
};
password = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Sets the server password. Leave `null` for no password.
'';
};
messageOfTheDay = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Set the server message of the day text.
'';
};
worldPath = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The path to the world file (`.wld`) which should be loaded.
If no world exists at this path, one will be created with the size
specified by `autoCreatedWorldSize`.
'';
};
autoCreatedWorldSize = lib.mkOption {
type = lib.types.enum [
"small"
"medium"
"large"
];
default = "medium";
description = ''
Specifies the size of the auto-created world if `worldPath` does not
point to an existing world.
'';
};
banListPath = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The path to the ban list.
'';
};
secure = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Adds additional cheat protection to the server.";
};
noUPnP = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Disables automatic Universal Plug and Play.";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to open ports in the firewall";
};
dataDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/terraria";
example = "/srv/terraria";
description = "Path to variable state data directory for terraria.";
};
};
};
config = lib.mkIf cfg.enable {
users.users.terraria = {
description = "Terraria server service user";
group = "terraria";
home = cfg.dataDir;
createHome = true;
uid = config.ids.uids.terraria;
};
users.groups.terraria = {
gid = config.ids.gids.terraria;
};
systemd.services.terraria = {
description = "Terraria Server Service";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
User = "terraria";
Group = "terraria";
Type = "forking";
GuessMainPID = true;
UMask = 7;
ExecStart = "${tmuxCmd} new -d ${lib.getExe cfg.package} ${lib.concatStringsSep " " flags}";
ExecStop = "${stopScript} $MAINPID";
};
};
networking.firewall = lib.mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.port ];
allowedUDPPorts = [ cfg.port ];
};
};
}
|