2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2014-03-29 09:48:33 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2014-03-29 09:48:33 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.minecraft-server;
|
2018-08-21 00:35:22 +01:00
|
|
|
|
|
|
|
# We don't allow eula=false anyways
|
|
|
|
eulaFile = builtins.toFile "eula.txt" ''
|
|
|
|
# eula.txt managed by NixOS Configuration
|
|
|
|
eula=true
|
|
|
|
'';
|
|
|
|
|
|
|
|
whitelistFile = pkgs.writeText "whitelist.json"
|
|
|
|
(builtins.toJSON
|
|
|
|
(mapAttrsToList (n: v: { name = n; uuid = v; }) cfg.whitelist));
|
|
|
|
|
|
|
|
cfgToString = v: if builtins.isBool v then boolToString v else toString v;
|
|
|
|
|
|
|
|
serverPropertiesFile = pkgs.writeText "server.properties" (''
|
|
|
|
# server.properties managed by NixOS configuration
|
|
|
|
'' + concatStringsSep "\n" (mapAttrsToList
|
|
|
|
(n: v: "${n}=${cfgToString v}") cfg.serverProperties));
|
|
|
|
|
2022-07-21 20:05:43 +01:00
|
|
|
stopScript = pkgs.writeShellScript "minecraft-server-stop" ''
|
|
|
|
echo stop > ${config.systemd.sockets.minecraft-server.socketConfig.ListenFIFO}
|
|
|
|
|
|
|
|
# Wait for the PID of the minecraft server to disappear before
|
|
|
|
# returning, so systemd doesn't attempt to SIGKILL it.
|
|
|
|
while kill -0 "$1" 2> /dev/null; do
|
|
|
|
sleep 1s
|
|
|
|
done
|
|
|
|
'';
|
2018-08-21 00:35:22 +01:00
|
|
|
|
|
|
|
# To be able to open the firewall, we need to read out port values in the
|
|
|
|
# server properties, but fall back to the defaults when those don't exist.
|
|
|
|
# These defaults are from https://minecraft.gamepedia.com/Server.properties#Java_Edition_3
|
|
|
|
defaultServerPort = 25565;
|
|
|
|
|
|
|
|
serverPort = cfg.serverProperties.server-port or defaultServerPort;
|
|
|
|
|
|
|
|
rconPort = if cfg.serverProperties.enable-rcon or false
|
|
|
|
then cfg.serverProperties."rcon.port" or 25575
|
|
|
|
else null;
|
|
|
|
|
|
|
|
queryPort = if cfg.serverProperties.enable-query or false
|
|
|
|
then cfg.serverProperties."query.port" or 25565
|
|
|
|
else null;
|
|
|
|
|
|
|
|
in {
|
2014-03-29 09:48:33 +00:00
|
|
|
options = {
|
|
|
|
services.minecraft-server = {
|
2015-04-07 13:05:03 +01:00
|
|
|
|
2014-03-29 09:48:33 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
If enabled, start a Minecraft Server. The server
|
2014-03-29 09:48:33 +00:00
|
|
|
data will be loaded from and saved to
|
2022-07-28 22:19:15 +01:00
|
|
|
{option}`services.minecraft-server.dataDir`.
|
2018-08-21 00:35:22 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
declarative = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Whether to use a declarative Minecraft server configuration.
|
2022-07-28 22:19:15 +01:00
|
|
|
Only if set to `true`, the options
|
|
|
|
{option}`services.minecraft-server.whitelist` and
|
|
|
|
{option}`services.minecraft-server.serverProperties` will be
|
2018-08-21 00:35:22 +01:00
|
|
|
applied.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
eula = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Whether you agree to
|
2022-07-28 22:19:15 +01:00
|
|
|
[
|
|
|
|
Mojangs EULA](https://account.mojang.com/documents/minecraft_eula). This option must be set to
|
|
|
|
`true` to run Minecraft server.
|
2015-04-07 13:05:03 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/minecraft";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Directory to store Minecraft database and other state/data files.
|
2015-04-07 13:05:03 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Whether to open ports in the firewall for the server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
whitelist = mkOption {
|
|
|
|
type = let
|
|
|
|
minecraftUUID = types.strMatching
|
|
|
|
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" // {
|
|
|
|
description = "Minecraft UUID";
|
|
|
|
};
|
|
|
|
in types.attrsOf minecraftUUID;
|
|
|
|
default = {};
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Whitelisted players, only has an effect when
|
2022-07-28 22:19:15 +01:00
|
|
|
{option}`services.minecraft-server.declarative` is
|
|
|
|
`true` and the whitelist is enabled
|
|
|
|
via {option}`services.minecraft-server.serverProperties` by
|
|
|
|
setting `white-list` to `true`.
|
2018-08-21 00:35:22 +01:00
|
|
|
This is a mapping from Minecraft usernames to UUIDs.
|
2022-07-28 22:19:15 +01:00
|
|
|
You can use <https://mcuuid.net/> to get a
|
2018-08-21 00:35:22 +01:00
|
|
|
Minecraft UUID for a username.
|
|
|
|
'';
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2018-08-21 00:35:22 +01:00
|
|
|
{
|
|
|
|
username1 = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
|
|
|
|
username2 = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy";
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
serverProperties = mkOption {
|
2019-08-08 22:35:52 +01:00
|
|
|
type = with types; attrsOf (oneOf [ bool int str ]);
|
2018-08-21 00:35:22 +01:00
|
|
|
default = {};
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2018-08-21 00:35:22 +01:00
|
|
|
{
|
|
|
|
server-port = 43000;
|
|
|
|
difficulty = 3;
|
|
|
|
gamemode = 1;
|
|
|
|
max-players = 5;
|
|
|
|
motd = "NixOS Minecraft server!";
|
|
|
|
white-list = true;
|
|
|
|
enable-rcon = true;
|
|
|
|
"rcon.password" = "hunter2";
|
|
|
|
}
|
|
|
|
'';
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2018-08-21 00:35:22 +01:00
|
|
|
Minecraft server properties for the server.properties file. Only has
|
2022-07-28 22:19:15 +01:00
|
|
|
an effect when {option}`services.minecraft-server.declarative`
|
|
|
|
is set to `true`. See
|
|
|
|
<https://minecraft.gamepedia.com/Server.properties#Java_Edition_3>
|
2018-08-21 00:35:22 +01:00
|
|
|
for documentation on these values.
|
2014-03-29 09:48:33 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-08-21 00:35:22 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.minecraft-server;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.minecraft-server";
|
|
|
|
example = literalExpression "pkgs.minecraft-server_1_12_2";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Version of minecraft-server to run.";
|
2018-08-21 00:35:22 +01:00
|
|
|
};
|
|
|
|
|
2014-03-29 09:48:33 +00:00
|
|
|
jvmOpts = mkOption {
|
2018-08-21 00:35:22 +01:00
|
|
|
type = types.separatedString " ";
|
2014-03-29 09:48:33 +00:00
|
|
|
default = "-Xmx2048M -Xms2048M";
|
2018-08-21 00:35:22 +01:00
|
|
|
# Example options from https://minecraft.gamepedia.com/Tutorials/Server_startup_script
|
2022-03-27 14:09:46 +01:00
|
|
|
example = "-Xms4092M -Xmx4092M -XX:+UseG1GC -XX:+CMSIncrementalPacing "
|
2018-08-21 00:35:22 +01:00
|
|
|
+ "-XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 "
|
|
|
|
+ "-XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "JVM options for the Minecraft server.";
|
2014-03-29 09:48:33 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2018-08-21 00:35:22 +01:00
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users.minecraft = {
|
2018-08-21 00:35:22 +01:00
|
|
|
description = "Minecraft server service user";
|
2015-04-07 13:05:03 +01:00
|
|
|
home = cfg.dataDir;
|
2014-03-29 09:48:33 +00:00
|
|
|
createHome = true;
|
2021-08-08 13:00:00 +01:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "minecraft";
|
2014-03-29 09:48:33 +00:00
|
|
|
};
|
2021-08-08 13:00:00 +01:00
|
|
|
users.groups.minecraft = {};
|
2014-03-29 09:48:33 +00:00
|
|
|
|
2022-07-21 20:05:43 +01:00
|
|
|
systemd.sockets.minecraft-server = {
|
|
|
|
bindsTo = [ "minecraft-server.service" ];
|
|
|
|
socketConfig = {
|
|
|
|
ListenFIFO = "/run/minecraft-server.stdin";
|
|
|
|
SocketMode = "0660";
|
|
|
|
SocketUser = "minecraft";
|
|
|
|
SocketGroup = "minecraft";
|
|
|
|
RemoveOnStop = true;
|
|
|
|
FlushPending = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-03-29 09:48:33 +00:00
|
|
|
systemd.services.minecraft-server = {
|
|
|
|
description = "Minecraft Server Service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-07-21 20:05:43 +01:00
|
|
|
requires = [ "minecraft-server.socket" ];
|
|
|
|
after = [ "network.target" "minecraft-server.socket" ];
|
2014-03-29 09:48:33 +00:00
|
|
|
|
2018-08-21 00:35:22 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/minecraft-server ${cfg.jvmOpts}";
|
2022-07-21 20:05:43 +01:00
|
|
|
ExecStop = "${stopScript} $MAINPID";
|
2018-08-21 00:35:22 +01:00
|
|
|
Restart = "always";
|
|
|
|
User = "minecraft";
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
2022-07-21 20:05:43 +01:00
|
|
|
|
|
|
|
StandardInput = "socket";
|
|
|
|
StandardOutput = "journal";
|
|
|
|
StandardError = "journal";
|
|
|
|
|
2021-12-28 12:49:14 +00:00
|
|
|
# Hardening
|
|
|
|
CapabilityBoundingSet = [ "" ];
|
|
|
|
DeviceAllow = [ "" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
UMask = "0077";
|
2018-08-21 00:35:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
ln -sf ${eulaFile} eula.txt
|
|
|
|
'' + (if cfg.declarative then ''
|
|
|
|
|
|
|
|
if [ -e .declarative ]; then
|
|
|
|
|
|
|
|
# Was declarative before, no need to back up anything
|
|
|
|
ln -sf ${whitelistFile} whitelist.json
|
|
|
|
cp -f ${serverPropertiesFile} server.properties
|
|
|
|
|
|
|
|
else
|
2015-04-07 13:05:03 +01:00
|
|
|
|
2018-08-21 00:35:22 +01:00
|
|
|
# Declarative for the first time, backup stateful files
|
|
|
|
ln -sb --suffix=.stateful ${whitelistFile} whitelist.json
|
|
|
|
cp -b --suffix=.stateful ${serverPropertiesFile} server.properties
|
|
|
|
|
|
|
|
# server.properties must have write permissions, because every time
|
|
|
|
# the server starts it first parses the file and then regenerates it..
|
|
|
|
chmod +w server.properties
|
|
|
|
echo "Autogenerated file that signifies that this server configuration is managed declaratively by NixOS" \
|
|
|
|
> .declarative
|
|
|
|
|
|
|
|
fi
|
|
|
|
'' else ''
|
|
|
|
if [ -e .declarative ]; then
|
|
|
|
rm .declarative
|
|
|
|
fi
|
|
|
|
'');
|
2015-04-07 13:05:03 +01:00
|
|
|
};
|
2018-08-21 00:35:22 +01:00
|
|
|
|
|
|
|
networking.firewall = mkIf cfg.openFirewall (if cfg.declarative then {
|
|
|
|
allowedUDPPorts = [ serverPort ];
|
|
|
|
allowedTCPPorts = [ serverPort ]
|
2019-04-24 04:48:22 +01:00
|
|
|
++ optional (queryPort != null) queryPort
|
|
|
|
++ optional (rconPort != null) rconPort;
|
2018-08-21 00:35:22 +01:00
|
|
|
} else {
|
|
|
|
allowedUDPPorts = [ defaultServerPort ];
|
|
|
|
allowedTCPPorts = [ defaultServerPort ];
|
|
|
|
});
|
|
|
|
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.eula;
|
|
|
|
message = "You must agree to Mojangs EULA to run minecraft-server."
|
|
|
|
+ " Read https://account.mojang.com/documents/minecraft_eula and"
|
|
|
|
+ " set `services.minecraft-server.eula` to `true` if you agree.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2014-03-29 09:48:33 +00:00
|
|
|
};
|
|
|
|
}
|