-
-
Save datakurre/cfdf627fb23ed8ff62bb7b3520b92674 to your computer and use it in GitHub Desktop.
{ 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 ]; | |
}; | |
}; | |
} |
@B3vis Thanks! I've noticed that bedrock-server is a moving target and updates on devices force to keep the server up to date 😬 And if you ever have time to figure out a declarative multi-world setup, that would be awesome.
@datakurre - I've been thinking about this for a while and I couldn't find a way to reliably modify the bedrock-server.nix to make it multi-world so I took another approach using NixOS containers. https://nixos.org/manual/nixos/stable/index.html#sec-declarative-containers
Here is a working configuration for the second bedrock-server instance I run on a VPS. 'user' is someone's name to make it easy to see which instance is for who
containers.minecraft-user = {
autoStart = true;
ephemeral = true;
bindMounts.minecraft-user = {
hostPath = "/var/lib/minecraft/user";
mountPoint = "/var/lib/minecraft-bedrock";
isReadOnly = false;
};
config = { config, pkgs, ... }: {
boot.isContainer = true;
networking.hostName = "minecraft-user";
imports = [ ./modules/minecraft/bedrock-server.nix ];
services.minecraft-bedrock-server = {
enable = true;
serverProperties = {
server-name = "user's Minecraft Server";
gamemode = "creative";
difficulty = "easy";
allow-cheats = true;
white-list = true;
server-port = 19134;
server-portv6 = 19135;
};
};
};
};
Only annoyance that I can't solve because bedrock edition doesn't support _srv records is having different ports for users. Not the end of the world but not as clean as I'd like
@B3vis That's clever approach, and would allow simpler modules. Thanks for sharing!
I've updated this to work with 1.17 - https://gist.github.com/b3vis/ab4a27ab548ea285262ed8580fb87df8
Microsoft removed the libCrypto.so library from the zip and it's also no longer required to run the binary.
Thank you for your work @datakurre