From 877a1f4da7beb726d558acdb9edfc1ef675727e5 Mon Sep 17 00:00:00 2001 From: niten Date: Mon, 5 Sep 2022 17:30:30 -0700 Subject: [PATCH] Add memory flags to minecraft server --- lib/fudo/default.nix | 99 ++++++++++++++--------------- lib/fudo/minecraft-clj.nix | 113 ++++++++++++++++++++++++++++++++++ lib/fudo/minecraft-server.nix | 34 ++++++++++ 3 files changed, 197 insertions(+), 49 deletions(-) create mode 100644 lib/fudo/minecraft-clj.nix diff --git a/lib/fudo/default.nix b/lib/fudo/default.nix index 0042492..978397c 100644 --- a/lib/fudo/default.nix +++ b/lib/fudo/default.nix @@ -1,54 +1,55 @@ { config, lib, pkgs, ... }: with lib; { - imports = [ - ./backplane-service/dns.nix + imports = [ + ./backplane-service/dns.nix - ./acme-certs.nix - ./adguard-dns-proxy.nix - ./authentication.nix - ./backplane.nix - ./chat.nix - ./client/dns.nix - ./deploy.nix - ./distributed-builds.nix - ./dns.nix - ./domains.nix - ./garbage-collector.nix - ./git.nix - ./global.nix - ./grafana.nix - ./hosts.nix - ./host-filesystems.nix - ./initrd-network.nix - ./ipfs.nix - ./jabber.nix - ./kdc.nix - ./ldap.nix - ./local-network.nix - ./mail.nix - ./mail-container.nix - ./minecraft-server.nix - ./netinfo-email.nix - ./node-exporter.nix - ./nsd.nix - ./password.nix - ./postgres.nix - ./powerdns.nix - ./prometheus.nix - ./secrets.nix - ./secure-dns-proxy.nix - ./sites.nix - ./slynk.nix - ./ssh.nix - ./system.nix - ./system-networking.nix - ./users.nix - ./vpn.nix - ./webmail.nix - # ./wireguard.nix - # ./wireguard-client.nix - ./wireless-networks.nix - ./zones.nix - ]; + ./acme-certs.nix + ./adguard-dns-proxy.nix + ./authentication.nix + ./backplane.nix + ./chat.nix + ./client/dns.nix + ./deploy.nix + ./distributed-builds.nix + ./dns.nix + ./domains.nix + ./garbage-collector.nix + ./git.nix + ./global.nix + ./grafana.nix + ./hosts.nix + ./host-filesystems.nix + ./initrd-network.nix + ./ipfs.nix + ./jabber.nix + ./kdc.nix + ./ldap.nix + ./local-network.nix + ./mail.nix + ./mail-container.nix + ./minecraft-clj.nix + ./minecraft-server.nix + ./netinfo-email.nix + ./node-exporter.nix + ./nsd.nix + ./password.nix + ./postgres.nix + ./powerdns.nix + ./prometheus.nix + ./secrets.nix + ./secure-dns-proxy.nix + ./sites.nix + ./slynk.nix + ./ssh.nix + ./system.nix + ./system-networking.nix + ./users.nix + ./vpn.nix + ./webmail.nix + # ./wireguard.nix + # ./wireguard-client.nix + ./wireless-networks.nix + ./zones.nix + ]; } diff --git a/lib/fudo/minecraft-clj.nix b/lib/fudo/minecraft-clj.nix new file mode 100644 index 0000000..c938ca3 --- /dev/null +++ b/lib/fudo/minecraft-clj.nix @@ -0,0 +1,113 @@ +{ config, lib, pkgs, ... }: + +with lib; +let + cfg = config.fudo.minecraft-clj; + + papermcWithPlugins = pkgs.buildEnv { + name = "papermcWithPlugins"; + paths = with pkgs; [ papermc witchcraft-plugin-current ]; + }; + + highMemFlags = [ + "-XX:G1NewSizePercent=40" + "-XX:G1MaxNewSizePercent=50" + "-XX:G1HeapRegionSize=16M" + "-XX:G1ReservePercent=15" + "-XX:InitiatingHeapOccupancyPercent=20" + ]; + + commonFlags = [ + "-XX:+UseG1GC" + "-XX:+ParallelRefProcEnabled" + "-XX:MaxGCPauseMillis=200" + "-XX:+UnlockExperimentalVMOptions" + "-XX:+DisableExplicitGC" + "-XX:+AlwaysPreTouch" + "-XX:G1NewSizePercent=30" + "-XX:G1MaxNewSizePercent=40" + "-XX:G1HeapRegionSize=8M" + "-XX:G1ReservePercent=20" + "-XX:G1HeapWastePercent=5" + "-XX:G1MixedGCCountTarget=4" + "-XX:InitiatingHeapOccupancyPercent=15" + "-XX:G1MixedGCLiveThresholdPercent=90" + "-XX:G1RSetUpdatingPauseTimePercent=5" + "-XX:SurvivorRatio=32" + "-XX:+PerfDisableSharedMem" + "-XX:MaxTenuringThreshold=1" + "-Dusing.aikars.flags=https://mcflags.emc.gs" + "-Daikars.new.flags=true" + ]; + +in { + options.fudo.minecraft-clj = with types; { + enable = mkEnableOption "Enable Minecraft server with Clojure repl."; + + data-dir = mkOption { + type = str; + description = "Path at which to store Minecraft data."; + }; + + allocated-memory = mkOption { + type = int; + description = "Memory to allocate to Minecraft, in GB."; + default = 4; + }; + + user = mkOption { + type = str; + description = "User as which to run the minecraft server."; + default = "minecraft-clj"; + }; + + group = mkOption { + type = str; + description = "Group as which to run the minecraft server."; + default = "minecraft-clj"; + }; + }; + + config = { + users = { + users."${cfg.user}" = { + isNormalUser = false; + home = cfg.data-dir; + group = cfg.group; + createHome = true; + }; + groups."${cfg.group}" = { members = [ cfg.user ]; }; + }; + + systemd.services.minecraft-clj = { + description = "Minecraft server with Clojure REPL."; + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + serviceConfig = { + User = cfg.user; + Group = cfg.group; + ExecStart = let + mem = "${cfg.allocated-memory}G"; + memFlags = [ "-Xms${mem}" "-Xmx${mem}" ]; + flags = commonFlags ++ memFlags + ++ (optionals (cfg.allocated-memory >= 12) highMemFlags); + flagStr = concatStringsSep " " flags; + in "${pkgs.papermc}/bin/minecraft-server ${flagStr}"; + + Restart = "always"; + NoNewPrivileges = true; + PrivateTmp = true; + PrivateDevices = true; + ProtectSystem = "strict"; + ProtectHome = true; + ProtectControlGroups = true; + ProtectKernelModules = true; + ProtectKernalTunables = true; + RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ]; + RestrictRealtime = true; + RestrictNamespaces = true; + MemoryDenyWriteExecute = true; + }; + }; + }; +} diff --git a/lib/fudo/minecraft-server.nix b/lib/fudo/minecraft-server.nix index ed7a56e..f8e03c1 100644 --- a/lib/fudo/minecraft-server.nix +++ b/lib/fudo/minecraft-server.nix @@ -45,6 +45,12 @@ in { type = types.bool; default = false; }; + + allocated-memory = mkOption { + type = types.int; + description = "Memory (in GB) to allocate to the Minecraft server."; + default = 2; + }; }; config = mkIf cfg.enable { @@ -63,6 +69,34 @@ in { gamemode = cfg.game-mode; allow-cheats = true; }; + jvmOpts = [ + "-Xms${toString cfg.allocated-memory}G" + "-Xmx${toString cfg.allocated-memory}G" + "-XX:+UseG1GC" + "-XX:+ParallelRefProcEnabled" + "-XX:MaxGCPauseMillis=200" + "-XX:+UnlockExperimentalVMOptions" + "-XX:+DisableExplicitGC" + "-XX:+AlwaysPreTouch" + "-XX:G1NewSizePercent=30" + "-XX:G1MaxNewSizePercent=40" + "-XX:G1HeapRegionSize=8M" + "-XX:G1ReservePercent=20" + "-XX:G1HeapWastePercent=5" + "-XX:G1MixedGCCountTarget=4" + "-XX:InitiatingHeapOccupancyPercent=15" + "-XX:G1MixedGCLiveThresholdPercent=90" + "-XX:G1RSetUpdatingPauseTimePercent=5" + "-XX:SurvivorRatio=32" + "-XX:+PerfDisableSharedMem" + "-XX:MaxTenuringThreshold=1" + ] ++ (optionals (cfg.allocated-memory >= 12) [ + "-XX:G1NewSizePercent=40" + "-XX:G1MaxNewSizePercent=50" + "-XX:G1HeapRegionSize=16M" + "-XX:G1ReservePercent=15" + "-XX:InitiatingHeapOccupancyPercent=20" + ]); }; }; }