fork/nix-minecraft
git clone https://git.julia.blue/fork/nix-minecraft
8682670
feat/abduco
1{ config , lib , pkgs , ...} : 2with lib ; 3let 4cfg = config . services . minecraft-servers ; 5 6mkOpt = type :default : 7mkOption { inherit type default ; }; 8 9mkOpt' = type :default :description : 10mkOption { inherit type default description ; }; 11 12mkBoolOpt = default :mkOption { 13inherit default ; 14type = types . bool ; 15example = true ; 16}; 17 18mkBoolOpt' = default :description :mkOption { 19inherit default description ; 20type = types . bool ; 21example = true ; 22}; 23 24mkEnableOpt = description :mkBoolOpt' false description ; 25in 26{ 27options . services . minecraft-servers = { 28enable = mkEnableOpt '' 29If enabled, the servers in <option>services.minecraft-servers.servers</option> 30will be created and started as applicable. 31The data for the servers will be loaded from and 32saved to <option>services.minecraft-servers.dataDir</option> 33'' ; 34 35eula = mkEnableOpt '' 36Whether you agree to 37<link xlink:href="https://account.mojang.com/documents/minecraft_eula"> 38Mojang's EULA</link>. This option must be set to 39<literal>true</literal> to run Minecraft server. 40'' ; 41 42openFirewall = mkEnableOpt '' 43Whether to open ports in the firewall for each server. 44Sets the default for <option>services.minecraft-servers.servers.<name>.openFirewall</option>. 45'' ; 46 47dataDir = mkOpt' types . path "/srv/minecraft" '' 48Directory to store the Minecraft servers. 49Each server will be under a subdirectory named after 50the server name in this directory, such as <literal>/srv/minecraft/servername</literal>. 51'' ; 52 53runDir = mkOpt' types . path "/run/minecraft" '' 54Directory to place the runtime console sockets into. 55Each server's console will be a socket file in the form of <literal>servername.sock</literal>. 56To connect to the console, run `abduco -a /run/minecraft/servername.sock attach`, 57press `Ctrl + \` to detach. 58'' ; 59 60user = mkOption { 61type = types . string ; 62default = "minecraft" ; 63description = '' 64Name of the user to create and run servers under. 65It is recommended to leave this as the default, as it is 66the same user as <option>services.minecraft-server</option>. 67'' ; 68internal = true ; 69visible = false ; 70}; 71 72group = mkOption { 73type = types . string ; 74default = "minecraft" ; 75description = '' 76Name of the group to create and run servers under. 77In order to modify the server files or attach to the console socket, 78your user must be a part of this group. 79It is recommended to leave this as the default, as it is 80the same group as <option>services.minecraft-server</option>. 81'' ; 82}; 83 84servers = mkOption { 85default = { }; 86description = '' 87Servers to create and manage using this module. 88Each server can be stopped with <literal>systemctl stop minecraft-server-servername</literal>. 89::: {.warning} 90If the server is not stopped using `systemctl`, the service will automatically restart the server. 91See <option>services.minecraft-servers.servers.<name>.restart</option>. 92::: 93'' ; 94type = types . attrsOf ( types . submodule { 95options = { 96enable = mkEnableOpt '' 97Whether to enable this server. 98If set to <literal>false</literal>, does NOT delete any data in the data directory, 99just does not generate the service file. 100'' ; 101 102autoStart = mkBoolOpt' true '' 103Whether to start this server on boot. 104If set to <literal>false</literal>, can still be started with 105<literal>systemctl start minecraft-server-servername</literal>. 106Requires the server to be enabled. 107'' ; 108 109openFirewall = mkOption { 110type = types . bool ; 111default = cfg . openFirewall ; 112defaultText = "The value of <literal>services.minecraft-servers.openFirewall</literal>" ; 113description = '' 114Whether to open ports in the firewall for this server. 115'' ; 116}; 117 118restart = mkOpt' types . str "always" '' 119Value of systemd's <literal>Restart=</literal> service configuration option. 120Due to the servers being started in console sockets, values other than 121<literal>"no"</literal> and <literal>"always"</literal> may not work properly. 122As a consequence of the <literal>"always"</literal> option, stopping the server 123in-game with the <literal>stop</literal> command will cause the server to automatically restart. 124'' ; 125 126whitelist = mkOption { 127type = 128let 129minecraftUUID = types . strMatching 130"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" // { 131description = "Minecraft UUID" ; 132}; 133in 134types . attrsOf minecraftUUID ; 135default = { }; 136description = '' 137Whitelisted players, only has an effect when 138enabled via <option>services.minecraft-servers.<name>.serverProperties</option> 139by setting <literal>white-list</literal> to <literal>true</literal. 140'' ; 141example = literalExpression '' 142{ 143username1 = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; 144username2 = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"; 145} 146'' ; 147}; 148 149serverProperties = mkOption { 150type = with types ; attrsOf ( oneOf [ bool int str ]); 151default = { }; 152example = literalExpression '' 153{ 154server-port = 43000; 155difficulty = 3; 156gamemode = 1; 157max-players = 5; 158motd = "NixOS Minecraft server!"; 159white-list = true; 160enable-rcon = true; 161"rcon.password" = "hunter2"; 162} 163'' ; 164description = '' 165Minecraft server properties for the server.properties file of this server. See 166<link xlink:href="https://minecraft.gamepedia.com/Server.properties#Java_Edition_3"/> 167for documentation on these values. 168'' ; 169}; 170 171package = mkOption { 172description = "The Minecraft server package to use." ; 173type = types . package ; 174default = pkgs . minecraft-server ; 175defaultText = literalExpression "pkgs.minecraft-server" ; 176example = "pkgs.minecraftServers.vanilla-1_18_2" ; 177}; 178 179jvmOpts = mkOpt' ( types . separatedString " " ) "-Xmx2G -Xms1G" "JVM options for this server." ; 180 181symlinks = mkOpt' ( types . attrsOf types . package ) { } '' 182Things to symlink into this server's data directory, in the form of 183a nix package/derivation. Can be used to declaratively manage 184arbitrary files in the server's data directory. 185'' ; 186}; 187}); 188}; 189}; 190 191config = mkIf cfg . enable 192( 193let 194servers = filterAttrs ( _ :cfg :cfg . enable ) cfg . servers ; 195in 196{ 197users = { 198users . minecraft = { 199description = "Minecraft server service user" ; 200home = cfg . dataDir ; 201isSystemUser = true ; 202group = "minecraft" ; 203}; 204groups . minecraft = { }; 205}; 206 207assertions = [ 208{ 209assertion = cfg . eula ; 210message = "You must agree to Mojangs EULA to run minecraft-servers." 211+ " Read https://account.mojang.com/documents/minecraft_eula and" 212+ " set `services.minecraft-servers.eula` to `true` if you agree." ; 213} 214{ 215assertion = ! config . services . minecraft-server . enable && cfg . dataDir != config . services . minecraft-server . dataDir ; 216message = "`services.minecraft-servers.dataDir` and `services.minecraft-server.dataDir` conflict." 217+ " Set one to use a different data directory." ; 218} 219{ 220assertion = 221let 222serverPorts = mapAttrsToList 223( name :conf :conf . serverProperties . server-port or 25565 ) 224( filterAttrs ( _ :cfg :cfg . openFirewall ) servers ); 225 226counts = map ( port :count ( x :x == port ) serverPorts ) ( unique serverPorts ); 227in 228lib . all ( x :x == 1 ) counts ; 229} 230]; 231 232networking . firewall = 233let 234toOpen = filterAttrs ( _ :cfg :cfg . openFirewall ) servers ; 235UDPPorts = mapAttrsToList ( name :conf :conf . serverProperties . server-port or 25565 ) toOpen ; 236TCPPorts = concatLists 237( mapAttrsToList 238( name :conf :with conf ; 239( optional ( serverProperties . enable-rcon or false ) ( serverProperties . "rcon.port" or 25575 )) ++ 240( optional ( serverProperties . enable-query or false ) ( serverProperties . "query.port" or 25565 )) 241) 242toOpen 243); 244in 245rec { 246allowedUDPPorts = UDPPorts ; 247allowedTCPPorts = UDPPorts ++ TCPPorts ; 248}; 249 250system . activationScripts . minecraft-server-data-dir . text = '' 251mkdir -p ${ cfg . dataDir } 252chown minecraft:minecraft ${ cfg . dataDir } 253chmod -R 775 ${ cfg . dataDir } 254'' ; 255 256systemd . services = mapAttrs' 257( name :conf : 258let 259serverDir = " ${ cfg . dataDir } / ${ name } " ; 260runner = " ${ getBin pkgs . abduco } /bin/abduco" ; 261socket = " ${ cfg . runDir } / ${ name } .sock" ; 262 263startScript = pkgs . writeScript "minecraft-start- ${ name } " '' 264#! ${ pkgs . runtimeShell } 265 266umask u=rwx,g=rwx,o=rx 267cd ${ serverDir } 268${ runner } -c ${ socket } ${ conf . package } /bin/minecraft-server ${ conf . jvmOpts } 269'' ; 270 271stopScript = pkgs . writeScript "minecraft-stop- ${ name } " '' 272#! ${ pkgs . runtimeShell } 273 274if ! [ -d "/proc/$1" ]; then 275exit 0 276fi 277 278echo "stop" | ${ runner } -p -a ${ socket } 279'' ; 280in 281{ 282name = "minecraft-server- ${ name } " ; 283value = { 284description = "Minecraft Server ${ name } " ; 285wantedBy = mkIf conf . autoStart [ "multi-user.target" ]; 286after = [ "network.target" ]; 287 288enable = conf . enable ; 289 290serviceConfig = { 291ExecStart = " ${ startScript } " ; 292ExecStop = " ${ stopScript } $MAINPID" ; 293Restart = conf . restart ; 294User = "minecraft" ; 295Type = "forking" ; 296GuessMainPID = true ; 297RuntimeDirectory = "minecraft" ; 298}; 299 300preStart = 301let 302eula = builtins . toFile "eula.txt" '' 303# eula.txt managed by NixOS Configuration 304eula=true 305'' ; 306 307whitelist = pkgs . writeText "whitelist.json" 308( builtins . toJSON 309( mapAttrsToList ( n :v :{ name = n ; uuid = v ; }) conf . whitelist )); 310 311serverProperties = 312let 313cfgToString = v :if builtins . isBool v then boolToString v else toString v ; 314in 315pkgs . writeText "server.properties" ( '' 316# server.properties managed by NixOS configuration 317'' + concatStringsSep "\n" ( mapAttrsToList 318( n :v :" ${ n } = ${ cfgToString v } " ) 319conf . serverProperties )); 320 321mkSymlinks = pkgs . writeShellScript "minecraft-server- ${ name } -symlinks" 322( concatStringsSep "\n" 323( mapAttrsToList 324( n :v :'' 325if [[ -L " ${ n } " ]]; then 326unlink ${ n } 327elif [[ -e " ${ n } " ]]; then 328echo " ${ n } already exists, moving" 329mv " ${ n } " " ${ n } .bak" 330fi 331ln -sf ${ v } ${ n } 332'' ) 333conf . symlinks )); 334in 335'' 336umask u=rwx,g=rwx,o=rx 337mkdir -p ${ serverDir } 338cd ${ serverDir } 339ln -sf ${ eula } eula.txt 340${ if conf . whitelist != {} then "ln -sf ${ whitelist } whitelist.json" else "" } 341${ if conf . serverProperties != {} then "cp -f ${ serverProperties } server.properties" else "" } 342${ mkSymlinks } 343'' ; 344 345postStart = '' 346${ pkgs . coreutils } /bin/chmod 660 ${ socket } 347'' ; 348}; 349}) 350servers ; 351} 352); 353}