srb2k service: declaratively define more stuff

This commit is contained in:
Jill 2022-10-23 02:40:03 +02:00
parent dd7bfd7ea0
commit 2075549733
2 changed files with 71 additions and 28 deletions

View File

@ -141,6 +141,7 @@ in {
addonFileNames = filter (n: hasSuffix ".lua" n || hasSuffix ".kart" n || hasSuffix ".pk3" n || hasSuffix ".wad" n) fileNames;
in {
enable = true;
advertise = false;
addons = map (n: "${addonDir}${n}") addonFileNames;
};
@ -355,6 +356,10 @@ in {
{ address = "2001:41d0:0700:3308::";
prefixLength = 64;
}
{ address = "2001:41d0:0700:33ff::";
prefixLength = 64;
}
];
defaultGateway6 = {
@ -364,17 +369,6 @@ in {
interface = "eno1";
};
/*
dhcpcd.persistent = true;
dhcpcd.extraConfig = ''
clientid d0:50:99:d4:04:68:d0:50:99:d4:04:68
noipv6rs
interface eno1
ia_pd 1/2001:41d0:700:3308::/56 eno1
static ip6_address=2001:41d0:700:3308::1/56
'';
*/
firewall.allowPing = true;
# minecraft proximity voice chat
firewall.allowedTCPPorts = [ 24454 25567 ];

View File

@ -3,12 +3,16 @@
with lib;
let
cfg = config.modules.services.srb2k;
# https://wiki.srb2.org/wiki/Command_line_parameters
flags = [
"-dedicated"
"+advertise 1"
# todo: once declarative config is done,
# move this over to commands that are ran
# on startup, will you?
"+advertise ${toString (if cfg.advertise then 1 else 0)}"
"-port ${toString cfg.port}"
"-serverport ${toString cfg.port}"
];
] ++ cfg.flags;
in {
options.modules.services.srb2k = {
enable = mkOption {
@ -38,6 +42,30 @@ in {
default = [];
description = "Locations of srb2k addons and also fungus spore tasty in your body they grow happy you grow happy";
};
advertise = mkOption {
type = types.bool;
default = false;
description = "Whether to advertise this server on the SRB2Kart Master Server (https://ms.kartkrew.org/).";
};
flags = mkOption {
type = types.listOf types.str;
default = [];
description = "Additional flags to pass to the SRB2K executable. See https://wiki.srb2.org/wiki/Command_line_parameters";
};
autoStart = mkOption {
type = types.bool;
default = true;
description = "Automatically start the server on boot.";
};
openFirewall = mkOption {
type = types.bool;
default = true;
description = "Automatically open ports in the firewall.";
};
};
config = mkIf cfg.enable {
@ -49,32 +77,53 @@ in {
};
users.groups.srb2k = {};
systemd.services.srb2k = {
systemd.services.srb2k = let
tmux = "${getBin pkgs.tmux}/bin/tmux";
tmuxSock = "${cfg.dataDir}/srb2k.sock";
startScript = pkgs.writeScript "srb2k-start" ''
#!${pkgs.runtimeShell}
umask u=rwx,g=rwx,o=rx
cd ${cfg.dataDir}
${tmux} -S ${tmuxSock} new -d ${cfg.package}/bin/srb2kart ${concatStringsSep " " flags} -file ${concatStringsSep " " (map (path: "\"${path}\"") cfg.addons)}
'';
stopScript = pkgs.writeScript "srb2k-stop" ''
#!${pkgs.runtimeShell}
if ! [ -d "/proc/$1" ]; then
exit 0
fi
${tmux} -S ${tmuxSock} send-keys quit Enter
'';
in {
description = "srb2k server =)";
wantedBy = [ "multi-user.target" ];
wantedBy = mkIf cfg.autoStart [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
User = "srb2k";
ExecStart = "${startScript}";
ExecStop = "${stopScript} $MAINPID";
Restart = "always";
WorkingDirectory = cfg.dataDir;
ExecStart = #''
# ${getBin pkgs.tmux}/bin/tmux -S ${cfg.dataDir}/srb2k.sock new -d \
''
${cfg.package}/bin/srb2kart ${concatStringsSep " " flags} -file \
${concatStringsSep " " (map (path: "\"${path}\"") cfg.addons)}
'';
User = "srb2k";
Type = "forking";
GuessMainPID = true;
RuntimeDirectory = "srb2k";
};
preStart = ''
${pkgs.coreutils}/bin/chown srb2k:srb2k ${cfg.dataDir}
${pkgs.coreutils}/bin/chmod -R 775 ${cfg.dataDir}
'';
postStart = ''
${pkgs.coreutils}/bin/chmod 775 -R ${cfg.dataDir}
${pkgs.coreutils}/bin/chmod 660 ${cfg.dataDir}/srb2k.sock
${pkgs.coreutils}/bin/chgrp srb2k ${cfg.dataDir}/srb2k.sock
${pkgs.coreutils}/bin/chmod 660 ${tmuxSock}
'';
};
networking.firewall = {
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.port ];
allowedUDPPorts = [ cfg.port ];
};