From 765eaf332d23877c70fc2ad8aed725c9be567f80 Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 10 Feb 2021 19:10:04 +0100 Subject: [PATCH] New minecraft-server module --- modules/default.nix | 1 + modules/minecraft-server/default.nix | 84 ++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 modules/minecraft-server/default.nix diff --git a/modules/default.nix b/modules/default.nix index c813962..a9e415d 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -6,6 +6,7 @@ ./anycast_healthchecker ./gitea ./gre-tunnel + ./minecraft-server ./nginx-port-forward ]; } diff --git a/modules/minecraft-server/default.nix b/modules/minecraft-server/default.nix new file mode 100644 index 0000000..1aab14e --- /dev/null +++ b/modules/minecraft-server/default.nix @@ -0,0 +1,84 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.clerie.minecraft-server; + + # We don't allow eula=false anyways + eulaFile = builtins.toFile "eula.txt" '' + # eula.txt managed by NixOS Configuration + eula=true + ''; + + generateServerUnit = name: server: ( + nameValuePair "minecraft-server-${name}" (mkIf server.enable { + + description = "Minecraft Server ${name}"; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + + serviceConfig = { + ExecStart = "${server.package}/bin/minecraft-server ${server.jvmOpts}"; + Restart = "always"; + WorkingDirectory = if server.dataDir != null then server.dataDir else "/var/lib/minecraft-server-${name}"; + StateDirectory = "minecraft-server-${name}"; + DynamicUser = true; + }; + + preStart = '' + ln -sf ${eulaFile} eula.txt + ''; + }) + ); + + serverOpts = { config, ... }@moduleAttrs: { + options = { + + enable = mkEnableOption "A shared minecraft hosting service."; + + dataDir = mkOption { + type = with types; nullOr path; + default = null; + description = '' + Directory to store Minecraft database and other state/data files. + When null defaulting to /var/lib/minecraft-server-${name} + ''; + }; + + package = mkOption { + type = types.package; + default = pkgs.papermc; + defaultText = "pkgs.papermc"; + example = literalExample "pkgs.minecraft-server_1_12_2"; + description = "Version of minecraft-server to run."; + }; + + jvmOpts = mkOption { + type = types.separatedString " "; + default = "-Xmx2G -Xms1G"; + # Example options from https://minecraft.gamepedia.com/Tutorials/Server_startup_script + example = "-Xmx2048M -Xms4092M -XX:+UseG1GC -XX:+CMSIncrementalPacing " + + "-XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 " + + "-XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10"; + description = "JVM options for the Minecraft server."; + }; + }; + }; + +in { + options = { + clerie.minecraft-server = { + + servers = mkOption { + type = with types; attrsOf (submodule serverOpts); + default = {}; + }; + + }; + }; + + config = { + systemd.services = (mapAttrs' generateServerUnit cfg.servers); + }; +}