Forked from datakurre/minecraft-bedrock-server.nix
Created
November 19, 2023 23:11
-
-
Save olebedev/9945cf0af7accd55c2d9ba135435aed9 to your computer and use it in GitHub Desktop.
Minecraft Bedrock Server NixOS module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, lib, pkgs, ... }: | |
let | |
libCrypto = with pkgs; stdenv.mkDerivation rec { | |
name = "${pname}-${version}"; | |
pname = "minecraft-bedrock-server-libcrypto"; | |
version = "1.16-201.02"; | |
src = fetchurl { | |
url = "https://minecraft.azureedge.net/bin-linux/bedrock-server-1.16.201.02.zip"; | |
sha256 = "7213784b7d321044fee0ae89c781a3a3cd189dbfcb4911a959f11ee8a876b0cb"; | |
}; | |
sourceRoot = "."; | |
nativeBuildInputs = [ | |
autoPatchelfHook | |
curl | |
gcc-unwrapped | |
openssl | |
unzip | |
]; | |
installPhase = '' | |
install -m755 -D libCrypto.so $out/lib/libCrypto.so | |
''; | |
fixupPhase = '' | |
autoPatchelf $out/lib/libCrypto.so | |
''; | |
}; | |
minecraft-bedrock-server = with pkgs; stdenv.mkDerivation rec { | |
name = "${pname}-${version}"; | |
pname = "minecraft-bedrock-server"; | |
version = "1.16-201.02"; | |
src = fetchurl { | |
url = "https://minecraft.azureedge.net/bin-linux/bedrock-server-1.16.201.02.zip"; | |
sha256 = "7213784b7d321044fee0ae89c781a3a3cd189dbfcb4911a959f11ee8a876b0cb"; | |
}; | |
sourceRoot = "."; | |
nativeBuildInputs = [ | |
(patchelf.overrideDerivation(old: { | |
postPatch = '' | |
substituteInPlace src/patchelf.cc \ | |
--replace "32 * 1024 * 1024" "512 * 1024 * 1024" | |
''; | |
})) | |
autoPatchelfHook | |
curl | |
gcc-unwrapped | |
libCrypto | |
openssl | |
unzip | |
]; | |
installPhase = '' | |
install -m755 -D bedrock_server $out/bin/bedrock_server | |
rm bedrock_server | |
rm libCrypto.so | |
rm server.properties | |
mkdir -p $out/var | |
cp -a . $out/var/lib | |
''; | |
fixupPhase = '' | |
autoPatchelf $out/bin/bedrock_server | |
''; | |
}; | |
in | |
with lib; | |
let | |
cfg = config.services.minecraft-bedrock-server; | |
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)); | |
defaultServerPort = 19132; | |
serverPort = cfg.serverProperties.server-port or defaultServerPort; | |
in { | |
options = { | |
services.minecraft-bedrock-server = { | |
enable = mkOption { | |
type = types.bool; | |
default = false; | |
description = '' | |
If enabled, start a Minecraft Bedrock Server. The server | |
data will be loaded from and saved to | |
<option>services.minecraft-bedrock-server.dataDir</option>. | |
''; | |
}; | |
dataDir = mkOption { | |
type = types.path; | |
default = "/var/lib/minecraft-bedrock"; | |
description = '' | |
Directory to store Minecraft Bedrock database and other state/data files. | |
''; | |
}; | |
serverProperties = mkOption { | |
type = with types; attrsOf (oneOf [ bool int str ]); | |
default = { | |
server-name = "Dedicated Server"; | |
gamemode = "survival"; | |
difficulty = "easy"; | |
allow-cheats = false; | |
max-players = 10; | |
online-mode = false; | |
white-list = false; | |
server-port = 19132; | |
server-portv6 = 19133; | |
view-distance = 32; | |
tick-distance = 4; | |
player-idle-timeout = 30; | |
max-threads = 8; | |
level-name = "Bedrock level"; | |
level-seed = ""; | |
default-player-permission-level = "member"; | |
texturepack-required = false; | |
content-log-file-enabled = false; | |
compression-threshold = 1; | |
server-authoritative-movement = "server-auth"; | |
player-movement-score-threshold = 20; | |
player-movement-distance-threshold = 0.3; | |
player-movement-duration-threshold-in-ms = 500; | |
correct-player-movement = false; | |
}; | |
example = literalExample '' | |
{ | |
server-name = "Dedicated Server"; | |
gamemode = "survival"; | |
difficulty = "easy"; | |
allow-cheats = false; | |
max-players = 10; | |
online-mode = false; | |
white-list = false; | |
server-port = 19132; | |
server-portv6 = 19133; | |
view-distance = 32; | |
tick-distance = 4; | |
player-idle-timeout = 30; | |
max-threads = 8; | |
level-name = "Bedrock level"; | |
level-seed = ""; | |
default-player-permission-level = "member"; | |
texturepack-required = false; | |
content-log-file-enabled = false; | |
compression-threshold = 1; | |
server-authoritative-movement = "server-auth"; | |
player-movement-score-threshold = 20; | |
player-movement-distance-threshold = 0.3; | |
player-movement-duration-threshold-in-ms = 500; | |
correct-player-movement = false; | |
} | |
''; | |
description = '' | |
Minecraft Bedrock server properties for the server.properties file. | |
''; | |
}; | |
package = mkOption { | |
type = types.package; | |
default = minecraft-bedrock-server; | |
defaultText = "pkgs.minecraft-bedrock-server"; | |
example = literalExample "pkgs.minecraft-bedrock-server-1_16"; | |
description = "Version of minecraft-bedrock-server to run."; | |
}; | |
}; | |
}; | |
config = mkIf cfg.enable { | |
users.users.minecraft = { | |
description = "Minecraft server service user"; | |
home = cfg.dataDir; | |
createHome = true; | |
uid = config.ids.uids.minecraft; | |
}; | |
systemd.services.minecraft-bedrock-server = { | |
description = "Minecraft Bedrock Server Service"; | |
wantedBy = [ "multi-user.target" ]; | |
after = [ "network.target" ]; | |
serviceConfig = { | |
ExecStart = "${cfg.package}/bin/bedrock_server"; | |
Restart = "always"; | |
User = "minecraft"; | |
WorkingDirectory = cfg.dataDir; | |
}; | |
preStart = '' | |
cp -a -n ${cfg.package}/var/lib/* . | |
cp -f ${serverPropertiesFile} server.properties | |
chmod +w server.properties | |
''; | |
}; | |
networking.firewall = { | |
allowedUDPPorts = [ serverPort ]; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment