ctucx.git: nixfiles

ctucx' nixfiles

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
{ 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
      '';
    };
  };
}