about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/games/xonotic.nix
blob: c84347ddc9813445c9fc6c48d7e8123096ba3f38 (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{ config
, pkgs
, lib
, ...
}:

let
  cfg = config.services.xonotic;

  serverCfg = pkgs.writeText "xonotic-server.cfg" (
    toString cfg.prependConfig
      + "\n"
      + builtins.concatStringsSep "\n" (
        lib.mapAttrsToList (key: option:
          let
            escape = s: lib.escape [ "\"" ] s;
            quote = s: "\"${s}\"";

            toValue = x: quote (escape (toString x));

            value = (if lib.isList option then
              builtins.concatStringsSep
                " "
                (builtins.map (x: toValue x) option)
            else
              toValue option
            );
          in
          "${key} ${value}"
        ) cfg.settings
      )
      + "\n"
      + toString cfg.appendConfig
  );
in

{
  options.services.xonotic = {
    enable = lib.mkEnableOption (lib.mdDoc "Xonotic dedicated server");

    package = lib.mkPackageOption pkgs "xonotic-dedicated" {};

    openFirewall = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = lib.mdDoc ''
        Open the firewall for TCP and UDP on the specified port.
      '';
    };

    dataDir = lib.mkOption {
      type = lib.types.path;
      readOnly = true;
      default = "/var/lib/xonotic";
      description = lib.mdDoc ''
        Data directory.
      '';
    };

    settings = lib.mkOption {
      description = lib.mdDoc ''
        Generates the `server.cfg` file. Refer to [upstream's example][0] for
        details.

        [0]: https://gitlab.com/xonotic/xonotic/-/blob/master/server/server.cfg
      '';
      default = {};
      type = lib.types.submodule {
        freeformType = with lib.types; let
          scalars = oneOf [ singleLineStr int float ];
        in
        attrsOf (oneOf [ scalars (nonEmptyListOf scalars) ]);

        options.sv_public = lib.mkOption {
          type = lib.types.int;
          default = 0;
          example = [ (-1) 1 ];
          description = lib.mdDoc ''
            Controls whether the server will be publicly listed.
          '';
        };

        options.hostname = lib.mkOption {
          type = lib.types.singleLineStr;
          default = "Xonotic $g_xonoticversion Server";
          description = lib.mdDoc ''
            The name that will appear in the server list. `$g_xonoticversion`
            gets replaced with the current version.
          '';
        };

        options.sv_motd = lib.mkOption {
          type = lib.types.singleLineStr;
          default = "";
          description = lib.mdDoc ''
            Text displayed when players join the server.
          '';
        };

        options.sv_termsofservice_url = lib.mkOption {
          type = lib.types.singleLineStr;
          default = "";
          description = lib.mdDoc ''
            URL for the Terms of Service for playing on your server.
          '';
        };

        options.maxplayers = lib.mkOption {
          type = lib.types.int;
          default = 16;
          description = lib.mdDoc ''
            Number of player slots on the server, including spectators.
          '';
        };

        options.net_address = lib.mkOption {
          type = lib.types.singleLineStr;
          default = "0.0.0.0";
          description = lib.mdDoc ''
            The address Xonotic will listen on.
          '';
        };

        options.port = lib.mkOption {
          type = lib.types.port;
          default = 26000;
          description = lib.mdDoc ''
            The port Xonotic will listen on.
          '';
        };
      };
    };

    # Still useful even though we're using RFC 42 settings because *some* keys
    # can be repeated.
    appendConfig = lib.mkOption {
      type = with lib.types; nullOr lines;
      default = null;
      description = lib.mdDoc ''
        Literal text to insert at the end of `server.cfg`.
      '';
    };

    # Certain changes need to happen at the beginning of the file.
    prependConfig = lib.mkOption {
      type = with lib.types; nullOr lines;
      default = null;
      description = lib.mdDoc ''
        Literal text to insert at the start of `server.cfg`.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.xonotic = {
      description = "Xonotic server";
      wantedBy = [ "multi-user.target" ];

      environment = {
        # Required or else it tries to write the lock file into the nix store
        HOME = cfg.dataDir;
      };

      serviceConfig = {
        DynamicUser = true;
        User = "xonotic";
        StateDirectory = "xonotic";
        ExecStart = "${cfg.package}/bin/xonotic-dedicated";

        # Symlink the configuration from the nix store to where Xonotic actually
        # looks for it
        ExecStartPre = [
          "${pkgs.coreutils}/bin/mkdir -p ${cfg.dataDir}/.xonotic/data"
          ''
            ${pkgs.coreutils}/bin/ln -sf ${serverCfg} \
              ${cfg.dataDir}/.xonotic/data/server.cfg
          ''
        ];

        # Cargo-culted from search results about writing Xonotic systemd units
        ExecReload = "${pkgs.util-linux}/bin/kill -HUP $MAINPID";

        Restart = "on-failure";
        RestartSec = 10;
        StartLimitBurst = 5;
      };
    };

    networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
      cfg.settings.port
    ];
    networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [
      cfg.settings.port
    ];
  };

  meta.maintainers = with lib.maintainers; [ CobaltCause ];
}