{ config, lib, pkgs, ... }: with lib; let cfg = config.services.minecraft-servers; mkOpt = type: default: mkOption { inherit type default; }; mkOpt' = type: default: description: mkOption { inherit type default description; }; mkBoolOpt = default: mkOption { inherit default; type = types.bool; example = true; }; mkBoolOpt' = default: description: mkOption { inherit default description; type = types.bool; example = true; }; mkEnableOpt = description: mkBoolOpt' false description; in { options.services.minecraft-servers = { enable = mkEnableOpt '' If enabled, the servers in will be created and started as applicable. The data for the servers will be loaded from and saved to ''; eula = mkEnableOpt '' Whether you agree to Mojang's EULA. This option must be set to true to run Minecraft server. ''; openFirewall = mkEnableOpt '' Whether to open ports in the firewall for each server. Sets the default for . ''; dataDir = mkOpt' types.path "/srv/minecraft" '' Directory to store the Minecraft servers. Each server will be under a subdirectory named after the server name in this directory, such as /srv/minecraft/servername. ''; runDir = mkOpt' types.path "/run/minecraft" '' Directory to place the runtime console sockets into. Each server's console will be a socket file in the form of servername.sock. To connect to the console, run `abduco -a /run/minecraft/servername.sock attach`, press `Ctrl + \` to detach. ''; user = mkOption { type = types.string; default = "minecraft"; description = '' Name of the user to create and run servers under. It is recommended to leave this as the default, as it is the same user as . ''; internal = true; visible = false; }; group = mkOption { type = types.string; default = "minecraft"; description = '' Name of the group to create and run servers under. In order to modify the server files or attach to the console socket, your user must be a part of this group. It is recommended to leave this as the default, as it is the same group as . ''; }; servers = mkOption { default = { }; description = '' Servers to create and manage using this module. Each server can be stopped with systemctl stop minecraft-server-servername. ::: {.warning} If the server is not stopped using `systemctl`, the service will automatically restart the server. See . ::: ''; type = types.attrsOf (types.submodule { options = { enable = mkEnableOpt '' Whether to enable this server. If set to false, does NOT delete any data in the data directory, just does not generate the service file. ''; autoStart = mkBoolOpt' true '' Whether to start this server on boot. If set to false, can still be started with systemctl start minecraft-server-servername. Requires the server to be enabled. ''; openFirewall = mkOption { type = types.bool; default = cfg.openFirewall; defaultText = "The value of services.minecraft-servers.openFirewall"; description = '' Whether to open ports in the firewall for this server. ''; }; restart = mkOpt' types.str "always" '' Value of systemd's Restart= service configuration option. Due to the servers being started in console sockets, values other than "no" and "always" may not work properly. As a consequence of the "always" option, stopping the server in-game with the stop command will cause the server to automatically restart. ''; whitelist = mkOption { type = let minecraftUUID = types.strMatching "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" // { description = "Minecraft UUID"; }; in types.attrsOf minecraftUUID; default = { }; description = '' Whitelisted players, only has an effect when enabled via by setting white-list to true for documentation on these values. ''; }; package = mkOption { description = "The Minecraft server package to use."; type = types.package; default = pkgs.minecraft-server; defaultText = literalExpression "pkgs.minecraft-server"; example = "pkgs.minecraftServers.vanilla-1_18_2"; }; jvmOpts = mkOpt' (types.separatedString " ") "-Xmx2G -Xms1G" "JVM options for this server."; symlinks = mkOpt' (types.attrsOf types.package) { } '' Things to symlink into this server's data directory, in the form of a nix package/derivation. Can be used to declaratively manage arbitrary files in the server's data directory. ''; }; }); }; }; config = mkIf cfg.enable ( let servers = filterAttrs (_: cfg: cfg.enable) cfg.servers; in { users = { users.minecraft = { description = "Minecraft server service user"; home = cfg.dataDir; isSystemUser = true; group = "minecraft"; }; groups.minecraft = { }; }; assertions = [ { assertion = cfg.eula; message = "You must agree to Mojangs EULA to run minecraft-servers." + " Read https://account.mojang.com/documents/minecraft_eula and" + " set `services.minecraft-servers.eula` to `true` if you agree."; } { assertion = !config.services.minecraft-server.enable && cfg.dataDir != config.services.minecraft-server.dataDir; message = "`services.minecraft-servers.dataDir` and `services.minecraft-server.dataDir` conflict." + " Set one to use a different data directory."; } { assertion = let serverPorts = mapAttrsToList (name: conf: conf.serverProperties.server-port or 25565) (filterAttrs (_: cfg: cfg.openFirewall) servers); counts = map (port: count (x: x == port) serverPorts) (unique serverPorts); in lib.all (x: x == 1) counts; } ]; networking.firewall = let toOpen = filterAttrs (_: cfg: cfg.openFirewall) servers; UDPPorts = mapAttrsToList (name: conf: conf.serverProperties.server-port or 25565) toOpen; TCPPorts = concatLists (mapAttrsToList (name: conf: with conf; (optional (serverProperties.enable-rcon or false) (serverProperties."rcon.port" or 25575)) ++ (optional (serverProperties.enable-query or false) (serverProperties."query.port" or 25565)) ) toOpen ); in rec { allowedUDPPorts = UDPPorts; allowedTCPPorts = UDPPorts ++ TCPPorts; }; system.activationScripts.minecraft-server-data-dir.text = '' mkdir -p ${cfg.dataDir} chown minecraft:minecraft ${cfg.dataDir} chmod -R 775 ${cfg.dataDir} ''; systemd.services = mapAttrs' (name: conf: let serverDir = "${cfg.dataDir}/${name}"; runner = "${getBin pkgs.abduco}/bin/abduco"; socket = "${cfg.runDir}/${name}.sock"; startScript = pkgs.writeScript "minecraft-start-${name}" '' #!${pkgs.runtimeShell} umask u=rwx,g=rwx,o=rx cd ${serverDir} ${runner} -c ${socket} ${conf.package}/bin/minecraft-server ${conf.jvmOpts} ''; stopScript = pkgs.writeScript "minecraft-stop-${name}" '' #!${pkgs.runtimeShell} if ! [ -d "/proc/$1" ]; then exit 0 fi echo "stop" | ${runner} -p -a ${socket} ''; in { name = "minecraft-server-${name}"; value = { description = "Minecraft Server ${name}"; wantedBy = mkIf conf.autoStart [ "multi-user.target" ]; after = [ "network.target" ]; enable = conf.enable; serviceConfig = { ExecStart = "${startScript}"; ExecStop = "${stopScript} $MAINPID"; Restart = conf.restart; User = "minecraft"; Type = "forking"; GuessMainPID = true; RuntimeDirectory = "minecraft"; }; preStart = let eula = builtins.toFile "eula.txt" '' # eula.txt managed by NixOS Configuration eula=true ''; whitelist = pkgs.writeText "whitelist.json" (builtins.toJSON (mapAttrsToList (n: v: { name = n; uuid = v; }) conf.whitelist)); serverProperties = let cfgToString = v: if builtins.isBool v then boolToString v else toString v; in pkgs.writeText "server.properties" ('' # server.properties managed by NixOS configuration '' + concatStringsSep "\n" (mapAttrsToList (n: v: "${n}=${cfgToString v}") conf.serverProperties)); mkSymlinks = pkgs.writeShellScript "minecraft-server-${name}-symlinks" (concatStringsSep "\n" (mapAttrsToList (n: v: '' if [[ -L "${n}" ]]; then unlink ${n} elif [[ -e "${n}" ]]; then echo "${n} already exists, moving" mv "${n}" "${n}.bak" fi ln -sf ${v} ${n} '') conf.symlinks)); in '' umask u=rwx,g=rwx,o=rx mkdir -p ${serverDir} cd ${serverDir} ln -sf ${eula} eula.txt ${if conf.whitelist != {} then "ln -sf ${whitelist} whitelist.json" else ""} ${if conf.serverProperties != {} then "cp -f ${serverProperties} server.properties" else ""} ${mkSymlinks} ''; postStart = '' ${pkgs.coreutils}/bin/chmod 660 ${socket} ''; }; }) servers; } ); }