2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.terraria;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.terraria;
|
2020-04-24 23:36:52 +00:00
|
|
|
worldSizeMap = { small = 1; medium = 2; large = 3; };
|
|
|
|
valFlag = name: val: optionalString (val != null) "-${name} \"${escape ["\\" "\""] (toString val)}\"";
|
|
|
|
boolFlag = name: val: optionalString val "-${name}";
|
2020-08-20 17:08:02 +00:00
|
|
|
flags = [
|
2020-04-24 23:36:52 +00:00
|
|
|
(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)
|
|
|
|
];
|
|
|
|
stopScript = pkgs.writeScript "terraria-stop" ''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
|
|
|
|
if ! [ -d "/proc/$1" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
${getBin pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/terraria.sock send-keys Enter exit Enter
|
2020-04-24 23:36:52 +00:00
|
|
|
${getBin pkgs.coreutils}/bin/tail --pid="$1" -f /dev/null
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.terraria = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
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).
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2021-06-28 23:13:55 +00:00
|
|
|
type = types.port;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 7777;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Specifies the port to listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
maxPlayers = mkOption {
|
2021-06-28 23:13:55 +00:00
|
|
|
type = types.ints.u8;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 255;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Sets the max number of players (between 1 and 255).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
password = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Sets the server password. Leave `null` for no password.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
messageOfTheDay = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Set the server message of the day text.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
worldPath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The path to the world file (`.wld`) which should be loaded.
|
2020-04-24 23:36:52 +00:00
|
|
|
If no world exists at this path, one will be created with the size
|
2022-08-12 12:06:08 +00:00
|
|
|
specified by `autoCreatedWorldSize`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
autoCreatedWorldSize = mkOption {
|
|
|
|
type = types.enum [ "small" "medium" "large" ];
|
|
|
|
default = "medium";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Specifies the size of the auto-created world if `worldPath` does not
|
2020-04-24 23:36:52 +00:00
|
|
|
point to an existing world.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
banListPath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The path to the ban list.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
secure = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Adds additional cheat protection to the server.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
noUPnP = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Disables automatic Universal Plug and Play.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2021-05-20 23:08:51 +00:00
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-17 10:02:37 +00:00
|
|
|
description = lib.mdDoc "Whether to open ports in the firewall";
|
2021-05-20 23:08:51 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/terraria";
|
|
|
|
example = "/srv/terraria";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path to variable state data directory for terraria.";
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
users.users.terraria = {
|
|
|
|
description = "Terraria server service user";
|
2022-09-09 14:08:57 +00:00
|
|
|
group = "terraria";
|
2020-09-25 04:45:31 +00:00
|
|
|
home = cfg.dataDir;
|
2020-04-24 23:36:52 +00:00
|
|
|
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";
|
|
|
|
Type = "forking";
|
|
|
|
GuessMainPID = true;
|
2020-09-25 04:45:31 +00:00
|
|
|
ExecStart = "${getBin pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/terraria.sock new -d ${pkgs.terraria-server}/bin/TerrariaServer ${concatStringsSep " " flags}";
|
2020-04-24 23:36:52 +00:00
|
|
|
ExecStop = "${stopScript} $MAINPID";
|
|
|
|
};
|
|
|
|
|
|
|
|
postStart = ''
|
2020-09-25 04:45:31 +00:00
|
|
|
${pkgs.coreutils}/bin/chmod 660 ${cfg.dataDir}/terraria.sock
|
|
|
|
${pkgs.coreutils}/bin/chgrp terraria ${cfg.dataDir}/terraria.sock
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2021-05-20 23:08:51 +00:00
|
|
|
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
allowedUDPPorts = [ cfg.port ];
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|