82 lines
2.4 KiB
Nix
82 lines
2.4 KiB
Nix
{ config, lib, pkgs, ... }:
|
|
with lib;
|
|
let
|
|
spigot-server = pkgs.callPackage ../packages/spigot-server.nix {};
|
|
cfg = config.services.spigot-server;
|
|
StateDirectory = "spigot-server";
|
|
in {
|
|
options = {
|
|
services.spigot-server = {
|
|
enable = mkOption {
|
|
type = types.bool;
|
|
default = false;
|
|
description = ''
|
|
Whether to turn on the Spigot Minecraft server.
|
|
'';
|
|
};
|
|
|
|
user = mkOption {
|
|
type = types.str;
|
|
default = "spigot-server";
|
|
description = ''
|
|
The user account and group that Spigot runs as.
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
users.users = {
|
|
${cfg.user} = {
|
|
isSystemUser = true;
|
|
group = cfg.user;
|
|
description = "Spigot Minecraft server user";
|
|
};
|
|
};
|
|
users.groups = {
|
|
${cfg.user} = {
|
|
};
|
|
};
|
|
|
|
systemd = {
|
|
services.spigot-server = {
|
|
description = "Spigot Minecraft server";
|
|
wantedBy = [ "multi-user.target" ];
|
|
after = [ "network.target" ];
|
|
serviceConfig = {
|
|
User = "${cfg.user}";
|
|
|
|
Sockets = "spigot-server.socket";
|
|
StandardInput = "socket";
|
|
StandardOutput = "journal";
|
|
StandardError = "journal";
|
|
|
|
inherit StateDirectory;
|
|
WorkingDirectory = "/var/lib/${StateDirectory}";
|
|
ExecStart = "${spigot-server}/bin/spigot-server -nogui";
|
|
ExecStop = [
|
|
"${pkgs.bash}/bin/bash -c '${pkgs.coreutils}/bin/echo save-all > /run/spigot-server.stdin'"
|
|
"${pkgs.bash}/bin/bash -c '${pkgs.coreutils}/bin/echo stop > /run/spigot-server.stdin'"
|
|
# Wait for the main process to exit
|
|
# If we don't do this systemd tries to nudge Java to stop, causing a race condition
|
|
# that leads to an ungraceful shutdown
|
|
"${pkgs.coreutils}/bin/echo \"Waiting for \${MAINPID} to exit...\""
|
|
"${pkgs.bash}/bin/bash -c 'while ${pkgs.coreutils}/bin/kill -s 0 $MAINPID 2>/dev/null; do sleep 0.5; done'"
|
|
];
|
|
};
|
|
};
|
|
|
|
sockets.spigot-server = {
|
|
description = "Spigot Minecraft server socket for commands and stuff";
|
|
unitConfig = {
|
|
# Automatically start and stop socket along with the service
|
|
PartOf = "spigot-server.service";
|
|
};
|
|
socketConfig = {
|
|
ListenFIFO = "/run/spigot-server.stdin";
|
|
};
|
|
};
|
|
};
|
|
};
|
|
}
|