Merge pull request #111951 from f4814/add-quake3-module

nixos/quake3-server: Init

authored by

Aaron Andersen and committed by
GitHub
3041a858 2d5ea251

+112
+1
nixos/modules/module-list.nix
··· 366 366 ./services/games/minecraft-server.nix 367 367 ./services/games/minetest-server.nix 368 368 ./services/games/openarena.nix 369 + ./services/games/quake3-server.nix 369 370 ./services/games/teeworlds.nix 370 371 ./services/games/terraria.nix 371 372 ./services/hardware/acpid.nix
+111
nixos/modules/services/games/quake3-server.nix
··· 1 + { config, pkgs, lib, ... }: 2 + with lib; 3 + 4 + let 5 + cfg = config.services.quake3-server; 6 + configFile = pkgs.writeText "q3ds-extra.cfg" '' 7 + set net_port ${builtins.toString cfg.port} 8 + 9 + ${cfg.extraConfig} 10 + ''; 11 + defaultBaseq3 = pkgs.requireFile rec { 12 + name = "baseq3"; 13 + hashMode = "recursive"; 14 + sha256 = "5dd8ee09eabd45e80450f31d7a8b69b846f59738726929298d8a813ce5725ed3"; 15 + message = '' 16 + Unfortunately, we cannot download ${name} automatically. 17 + Please purchase a legitimate copy of Quake 3 and change into the installation directory. 18 + 19 + You can either add all relevant files to the nix-store like this: 20 + mkdir /tmp/baseq3 21 + cp baseq3/pak*.pk3 /tmp/baseq3 22 + nix-store --add-fixed sha256 --recursive /tmp/baseq3 23 + 24 + Alternatively you can set services.quake3-server.baseq3 to a path and copy the baseq3 directory into 25 + $services.quake3-server.baseq3/.q3a/ 26 + ''; 27 + }; 28 + home = pkgs.runCommand "quake3-home" {} '' 29 + mkdir -p $out/.q3a/baseq3 30 + 31 + for file in ${cfg.baseq3}/*; do 32 + ln -s $file $out/.q3a/baseq3/$(basename $file) 33 + done 34 + 35 + ln -s ${configFile} $out/.q3a/baseq3/nix.cfg 36 + ''; 37 + in { 38 + options = { 39 + services.quake3-server = { 40 + enable = mkEnableOption "Quake 3 dedicated server"; 41 + 42 + port = mkOption { 43 + type = types.port; 44 + default = 27960; 45 + description = '' 46 + UDP Port the server should listen on. 47 + ''; 48 + }; 49 + 50 + openFirewall = mkOption { 51 + type = types.bool; 52 + default = false; 53 + description = '' 54 + Open the firewall. 55 + ''; 56 + }; 57 + 58 + extraConfig = mkOption { 59 + type = types.lines; 60 + default = ""; 61 + example = '' 62 + seta rconPassword "superSecret" // sets RCON password for remote console 63 + seta sv_hostname "My Quake 3 server" // name that appears in server list 64 + ''; 65 + description = '' 66 + Extra configuration options. Note that options changed via RCON will not be persisted. To list all possible 67 + options, use "cvarlist 1" via RCON. 68 + ''; 69 + }; 70 + 71 + baseq3 = mkOption { 72 + type = types.either types.package types.path; 73 + default = defaultBaseq3; 74 + example = "/var/lib/q3ds"; 75 + description = '' 76 + Path to the baseq3 files (pak*.pk3). If this is on the nix store (type = package) all .pk3 files should be saved 77 + in the top-level directory. If this is on another filesystem (e.g /var/lib/baseq3) the .pk3 files are searched in 78 + $baseq3/.q3a/baseq3/ 79 + ''; 80 + }; 81 + }; 82 + }; 83 + 84 + config = let 85 + baseq3InStore = builtins.typeOf cfg.baseq3 == "set"; 86 + in mkIf cfg.enable { 87 + networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ cfg.port ]; 88 + 89 + systemd.services.q3ds = { 90 + description = "Quake 3 dedicated server"; 91 + wantedBy = [ "multi-user.target" ]; 92 + after = [ "networking.target" ]; 93 + 94 + environment.HOME = if baseq3InStore then home else cfg.baseq3; 95 + 96 + serviceConfig = with lib; { 97 + Restart = "always"; 98 + DynamicUser = true; 99 + WorkingDirectory = home; 100 + 101 + # It is possible to alter configuration files via RCON. To ensure reproducibility we have to prevent this 102 + ReadOnlyPaths = if baseq3InStore then home else cfg.baseq3; 103 + ExecStartPre = optionalString (!baseq3InStore) "+${pkgs.coreutils}/bin/cp ${configFile} ${cfg.baseq3}/.q3a/baseq3/nix.cfg"; 104 + 105 + ExecStart = "${pkgs.ioquake3}/ioq3ded.x86_64 +exec nix.cfg"; 106 + }; 107 + }; 108 + }; 109 + 110 + meta.maintainers = with maintainers; [ f4814n ]; 111 + }