nixos-config/modules/minecraft.nix

509 lines
15 KiB
Nix
Raw Normal View History

2022-02-19 14:34:26 +00:00
{ config, lib, options, pkgs, ... }:
with lib;
let
papermc = pkgs.callPackage ../packages/minecraft/paper.nix { };
cfg = config.services.minecraft;
opt = options.services.minecraft;
serverProperties = pkgs.writeText "server.properties" ''
${generators.toKeyValue {} cfg.properties.extraConfig}
2022-02-19 14:34:26 +00:00
'';
whitelistJson = pkgs.writeText "whitelist.json" ''
2022-02-19 14:36:39 +00:00
${builtins.toJSON cfg.whitelist}
2022-02-19 14:34:26 +00:00
'';
bukkitYaml = pkgs.writeText "bukkit.yml" ''
${generators.toYAML {} cfg.bukkit-yml}
'';
2022-02-19 14:34:26 +00:00
in
{
options.services.minecraft = {
enable = mkOption {
default = false;
type = types.bool;
description = "Enable minecraft server";
};
stateDir = mkOption {
default = "/var/lib/minecraft";
type = types.str;
2022-02-19 14:34:26 +00:00
description = "Path to the minecraft server state directory";
};
properties = {
allow-flight = mkOption {
default = false;
type = types.bool;
description = "Allow flight";
};
allow-nether = mkOption {
default = true;
type = types.bool;
description = "Allow nether";
};
broadcast-console-to-ops = mkOption {
default = true;
type = types.bool;
description = "Broadcast console to ops";
};
broadcast-rcon-to-ops = mkOption {
default = true;
type = types.bool;
description = "Broadcast rcon to ops";
};
difficulty = mkOption {
default = "normal";
type = types.enum [ "peacful" "easy" "normal" "hard" ];
description = "Difficulty";
};
enable-command-block = mkOption {
default = false;
type = types.bool;
description = "Enable command block";
};
enable-jmx-monitoring = mkOption {
default = false;
type = types.bool;
description = "Enable JMX monitoring";
};
enable-rcon = mkOption {
default = false;
type = types.bool;
description = "Enable rcon";
};
sync-chunk-writes = mkOption {
default = true;
type = types.bool;
description = "Sync chunk writes";
};
enable-status = mkOption {
default = true;
type = types.bool;
description = "Enable status";
};
enable-query = mkOption {
default = false;
type = types.bool;
description = "Enable query";
};
entity-broadcast-range-percentage = mkOption {
default = 100;
type = types.ints.between 10 1000;
description = "Entity broadcast range percentage";
};
force-gamemode = mkOption {
default = false;
type = types.bool;
description = "Force gamemode";
};
function-permission-level = mkOption {
default = 2;
type = types.ints.between 1 4;
description = "Function permission level";
};
gamemode = mkOption {
default = "survival";
type = types.enum [ "survival" "creative" "adventure" "spectator" ];
description = "Gamemode";
};
generate-structures = mkOption {
default = true;
type = types.bool;
description = "Generate structures";
};
generator-settings = mkOption {
default = "";
type = types.oneOf [ types.str (types.attrsOf types.anything) ];
description = "Generator settings";
apply = val: if (builtins.isString val) then val else builtins.toJSON val;
};
hardcore = mkOption {
default = false;
type = types.bool;
description = "Hardcore";
};
level-name = mkOption {
default = "world";
type = types.str;
description = "Level name";
};
level-seed = mkOption {
default = "";
type = types.str;
description = "Level seed";
};
level-type = mkOption {
2022-02-20 09:02:10 +00:00
default = "default";
type = types.enum [ "default" "flat" "largeBiomes" "amplified" ];
description = "Level type";
};
max-players = mkOption {
default = 20;
type = types.ints.unsigned;
description = "Max players";
};
max-tick-time = mkOption {
default = 60000;
type = types.int;
description = "Max tick time";
};
world-size = mkOption {
default = 29999984;
type = types.ints.between 1 29999984;
description = "World size";
};
motd = mkOption {
default = "A Minecraft server";
type = types.str;
description = "Message of the day";
};
network-compression-threshold = mkOption {
default = 256;
type = types.int;
description = "Network compression threshold";
};
online-mode = mkOption {
default = true;
type = types.bool;
description = "Online mode";
};
permission-level = mkOption {
default = 4;
type = types.ints.between 0 4;
description = "Permission level";
};
player-idle-timeout = mkOption {
default = 0;
type = types.ints.unsigned;
description = "Player idle timeout";
};
prevent-proxy-connections = mkOption {
default = false;
type = types.bool;
description = "Prevent proxy connections";
};
pvp = mkOption {
default = true;
type = types.bool;
description = "PvP";
};
query-port = mkOption {
default = 25565;
type = types.port;
description = "Query port";
};
rate-limit = mkOption {
default = 0;
type = types.ints.unsigned;
description = "Rate limit";
};
rcon-password-file = mkOption {
default = null;
type = types.nullOr types.str;
description = "Rcon password file";
};
rcon-port = mkOption {
default = 25575;
type = types.port;
description = "Rcon port";
};
resource-pack = mkOption {
default = "";
type = types.str;
description = "Resource pack";
};
resource-pack-prompt = mkOption {
default = "";
type = types.str;
description = "Resource pack prompt";
};
resource-pack-sha1 = mkOption {
2022-02-19 14:34:26 +00:00
default = "";
type = types.str;
description = "Resource pack sha1";
};
require-resource-pack = mkOption {
default = false;
type = types.bool;
description = "Require resource pack";
};
server-ip = mkOption {
default = "";
type = types.str;
description = "Server ip";
};
server-port = mkOption {
default = 25565;
type = types.port;
description = "Server port";
};
simulation-distance = mkOption {
default = 10;
type = types.ints.between 3 32;
description = "Simulation distance";
};
snooper-enabled = mkOption {
default = true;
type = types.bool;
description = "Snooper enabled";
};
spawn-animals = mkOption {
default = true;
type = types.bool;
description = "Enable animals";
};
spawn-monsters = mkOption {
default = true;
type = types.bool;
description = "Enable monsters";
};
spawn-npcs = mkOption {
default = true;
type = types.bool;
description = "Enable npcs";
};
spawn-protection = mkOption {
default = 16;
type = types.ints.between 0 256;
description = "Spawn protection";
};
use-native-transport = mkOption {
default = true;
type = types.bool;
description = "Use native transport";
};
view-distance = mkOption {
default = 10;
type = types.ints.between 3 32;
description = "View distance";
};
white-list = mkOption {
default = cfg.whitelist != [ ];
type = types.bool;
description = "White list";
};
enforce-whitelist = mkOption {
default = false;
type = types.bool;
description = "Enforce whitelist";
};
extraConfig = mkOption {
default = { };
2022-02-20 09:07:07 +00:00
type = types.attrsOf types.anything;
2022-02-19 14:34:26 +00:00
description = "Extra configuration to be added to the minecraft server properties file";
};
};
2022-02-20 09:17:57 +00:00
bukkit-yml = {
settings = {
allow-end = mkOption {
default = true;
type = types.bool;
description = "Allow end";
};
warn-on-overload = mkOption {
default = true;
type = types.bool;
description = "Warn on overload";
};
permissions-file = mkOption {
default = "permissions.yml";
type = types.str;
description = "Permissions file";
};
update-folder = mkOption {
default = "update";
type = types.str;
description = "Update folder";
};
ping-packet-limit = mkOption {
default = 100;
type = types.ints.between 0 1000;
description = "Ping packet limit";
};
use-exact-login-location = mkOption {
default = false;
type = types.bool;
description = "Use exact login location";
};
world-container = mkOption {
default = "world";
type = types.str;
description = "World container";
};
plugin-profiling = mkOption {
default = false;
type = types.bool;
description = "Plugin profiling";
};
connection-throttle = mkOption {
default = 0;
type = types.ints.unsigned;
description = "Connection throttle";
};
query-plugins = mkOption {
default = true;
type = types.bool;
description = "Query plugins";
};
deprecated-verbose = mkOption {
default = "default";
type = types.enum [ true false "default" ];
description = "Deprecated verbose";
};
shutdown-message = mkOption {
default = "Server closed";
type = types.str;
description = "Shutdown message";
};
};
spawn-limits = {
monsters = mkOption {
default = 70;
type = types.ints.unsigned;
description = "Max monsters";
};
animals = mkOption {
default = 15;
type = types.ints.unsigned;
description = "Max animals";
};
water-animals = mkOption {
default = 5;
type = types.ints.unsigned;
description = "Max water animals";
};
ambient = mkOption {
default = 15;
type = types.ints.unsigned;
description = "Max ambient";
};
};
chunk-gc = {
period-in-ticks = mkOption {
default = 600;
type = types.ints.unsigned;
description = "Period in ticks";
};
load-threshold = mkOption {
default = 0;
type = types.ints.unsigned;
description = "Load threshold";
};
};
ticks-per = {
animal-spawns = mkOption {
default = 400;
type = types.ints.unsigned;
description = "Animal spawns";
};
monster-spawns = mkOption {
default = 1;
type = types.ints.unsigned;
description = "Monster spawns";
};
autosave = mkOption {
default = 6000;
type = types.ints.unsigned;
description = "Autosave";
};
};
};
2022-02-19 14:34:26 +00:00
whitelist = mkOption {
default = [ ];
type = types.listOf (types.submodule {
options = {
uuid = mkOption {
type = types.str;
2022-02-19 14:34:26 +00:00
description = "UUID of the whitelist entry";
};
name = mkOption {
type = types.str;
2022-02-19 14:34:26 +00:00
description = "Name of the whitelist entry";
};
};
});
};
};
config = mkIf cfg.enable {
services.minecraft.properties.extraConfig = with cfg.properties; lib.mkDefault {
2022-02-20 08:59:53 +00:00
inherit allow-flight allow-nether broadcast-console-to-ops broadcast-rcon-to-ops;
inherit difficulty enable-command-block enable-jmx-monitoring enable-rcon sync-chunk-writes;
inherit enable-status enable-query entity-broadcast-range-percentage force-gamemode;
inherit function-permission-level gamemode generate-structures generator-settings hardcore;
2022-02-20 09:00:55 +00:00
inherit level-name level-seed level-type max-players max-tick-time motd;
inherit network-compression-threshold online-mode permission-level player-idle-timeout;
inherit prevent-proxy-connections pvp rate-limit resource-pack resource-pack-prompt;
inherit resource-pack-sha1 require-resource-pack server-ip server-port;
inherit simulation-distance snooper-enabled spawn-animals spawn-monsters spawn-npcs;
inherit spawn-protection use-native-transport view-distance white-list enforce-whitelist;
2022-02-20 09:03:01 +00:00
"query.port" = query-port;
"rcon.port" = rcon-port;
};
2022-02-19 14:34:26 +00:00
systemd.services.minecraft = {
description = "Minecraft Server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = [ papermc ];
preStart = ''
cd $HOME
2022-02-19 14:34:26 +00:00
# Agree to the EULA
echo "eula=true" > eula.txt
# Update the server properties
cat ${serverProperties} > server.properties
2022-02-20 09:12:00 +00:00
${if cfg.properties.rcon-password-file != null then ''
echo "rcon.password=$(cat ${builtins.toString cfg.properties.rcon-password-file})" >> server.properties
'' else "" }
2022-02-19 14:34:26 +00:00
# Update the whitelist
cat ${whitelistJson} > whitelist.json
# Update the bukkit yml
cat ${bukkitYaml} > bukkit.yml
2022-02-19 14:34:26 +00:00
'';
serviceConfig = {
Type = "simple";
User = "minecraft";
Group = "minecraft";
WorkingDirectory = cfg.stateDir;
2022-02-19 14:38:42 +00:00
ExecStart = "${papermc}/bin/minecraft-server";
2022-02-19 14:34:26 +00:00
Restart = "always";
RuntimeDirectory = "minecraft";
RuntimeDirectoryMode = "0755";
UMask = "0027";
ReadWritePaths = [ cfg.stateDir ]; # Grant access to the state directory
2022-02-19 14:34:26 +00:00
CapabilityBoundingSet = "";
# Security
NoNewPrivileges = true;
# Sandboxing
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
RestrictAddressFamilies = [ "AF_UNIX AF_INET AF_INET6" ];
LockPersonality = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
PrivateMounts = true;
};
environment = {
USER = "minecraft";
HOME = cfg.stateDir;
};
};
users.users.minecraft = {
description = "Minecraft Server";
home = cfg.stateDir;
useDefaultShell = true;
group = "minecraft";
isSystemUser = true;
};
users.groups.minecraft = { };
systemd.tmpfiles.rules = [
"d '${cfg.stateDir}' 0750 minecraft minecraft - -"
];
2022-02-19 14:34:26 +00:00
};
}