{ config, lib, pkgs, ... }: with lib; let cfg = config.services.minecraft-bedrock-server; cfgToString = v: if builtins.isBool v then boolToString v else toString v; allowListFile = pkgs.writeText "allowlist.json" (builtins.toJSON cfg.allowList); permissionsFile = pkgs.writeText "permissions.json" (builtins.toJSON cfg.permissions); serverPropertiesFile = pkgs.writeText "server.properties" ('' # server.properties managed by NixOS configuration '' + concatStringsSep "\n" (mapAttrsToList (n: v: "${n}=${cfgToString v}") cfg.settings)); in { options = { services.minecraft-bedrock-server = { enable = mkOption { type = types.bool; default = false; }; dataDir = mkOption { type = types.path; default = "/var/lib/minecraft-bedrock"; }; allowList = mkOption { type = with types; listOf (attrs); default = []; }; permissions = mkOption { type = with types; listOf (attrs); default = []; }; settings = mkOption { type = with types; attrsOf (oneOf [ bool int str ]); }; package = mkOption { type = types.package; default = pkgs.minecraft-bedrock-server; }; }; }; config = mkIf cfg.enable { services.minecraft-bedrock-server.settings = { server-name = lib.mkDefault "Bedrock Server"; gamemode = lib.mkDefault "survival"; difficulty = lib.mkDefault "easy"; allow-list = lib.mkDefault false; allow-cheats = lib.mkDefault false; max-players = lib.mkDefault 10; online-mode = lib.mkDefault false; server-port = lib.mkDefault 19132; server-portv6 = lib.mkDefault 19133; view-distance = lib.mkDefault 32; tick-distance = lib.mkDefault 4; player-idle-timeout = lib.mkDefault 30; max-threads = lib.mkDefault 4; level-name = lib.mkDefault "Bedrock level"; level-seed = lib.mkDefault ""; default-player-permission-level = lib.mkDefault "member"; texturepack-required = lib.mkDefault false; content-log-file-enabled = lib.mkDefault false; compression-threshold = lib.mkDefault 1; server-authoritative-movement = lib.mkDefault "server-auth"; correct-player-movement = lib.mkDefault false; }; users.groups.minecraft = {}; users.users.minecraft = { isSystemUser = true; home = cfg.dataDir; createHome = true; group = "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 cp -f ${permissionsFile} permissions.json cp -f ${allowListFile} allowlist.json chmod +w server.properties ''; }; }; }