about summary refs log tree commit diff
path: root/modules/services/starbound.nix
blob: 991ed2beb7010b6b80baaa72503bb50d4f202049 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.vuizvui.services.starbound;

  mkListenerOptions = what: defaultPort: {
    bind = mkOption {
      type = types.str;
      default = "::";
      description = ''
        Host/IP address to listen for incoming connections to the ${what}.
      '';
    };

    port = mkOption {
      type = types.int;
      default = defaultPort;
      description = ''
        Port to listen for incoming connections to the ${what}.
      '';
    };
  };

  serverConfig = {
    allowAdminCommands = cfg.adminCommands.allow;
    allowAdminCommandsFromAnyone = cfg.adminCommands.allowFromAnyone;

    allowAnonymousConnections = cfg.anonymousConnections.allow;
    anonymousConnectionsAreAdmin = cfg.anonymousConnections.adminPrivileges;

    serverUsers = mapAttrs (user: attrs: {
      inherit (attrs) admin password;
    }) cfg.users;

    inherit (cfg)
      allowAssetsMismatch maxPlayers maxTeamSize serverName serverFidelity;

    clearPlayerFiles = false;
    clearUniverseFiles = false;

    safeScripts = cfg.safeScripts.enable;
    scriptInstructionLimit = cfg.safeScripts.instructionLimit;
    scriptInstructionMeasureInterval =
      cfg.safeScripts.instructionMeasureInterval;
    scriptProfilingEnabled = cfg.safeScripts.profiling.enable;
    scriptRecursionLimit = cfg.safeScripts.recursionLimit;

    gameServerBind = cfg.bind;
    gameServerPort = cfg.port;

    bannedIPs = cfg.bannedIPs;
    bannedUuids = cfg.bannedUUIDs;

    runRconServer = cfg.rconServer.enable;
    rconServerBind = cfg.rconServer.bind;
    rconServerPort = cfg.rconServer.port;
    rconServerPassword = cfg.rconServer.password;
    rconServerTimeout = cfg.rconServer.timeout;

    runQueryServer = cfg.queryServer.enable;
    queryServerBind = cfg.queryServer.bind;
    queryServerPort = cfg.queryServer.port;
  } // cfg.extraConfig;

  bootConfig = pkgs.writeText "sbinit.config" (builtins.toJSON {
    logFileBackups = 0;
    storageDirectory = cfg.dataDir;
    assetDirectories = singleton (cfg.package.assets);
    defaultConfiguration = serverConfig;
  });

  # Traverse a given path with ../ until we get to the root directory (/).
  gotoRoot = p: concatStringsSep "/" (map (const "..") (splitString "/" p));

in {
  options.vuizvui.services.starbound = {
    enable = mkEnableOption "Starbound game server";

    adminCommands = {
      allow = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Whether to allow admin commands in general.
        '';
        # XXX: Make this dependant on whether an account is defined with enabled
        # admin.
      };

      allowFromAnyone = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Allow anyone, even anonymous users to use admin commands.
        '';
        # XXX: Check whether this is true!
      };
    };

    anonymousConnections = {
      allow = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Whether to allow anonymous connections to the server.

          Set this to <literal>false</literal> and use
          <option>serverUsers</option> to only allow specific accounts to
          connect.
        '';
      };

      adminPrivileges = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether all anonymous connections have administrative privileges.
        '';
      };
    };

    users = mkOption {
      type = types.attrsOf (types.submodule {
        options.admin = mkOption {
          type = types.bool;
          default = false;
          description = ''
            Whether this user has admin privileges.
          '';
        };
        options.password = mkOption {
          type = types.str;
          example = "supersecure";
          description = ''
            The password for the user.
          '';
        };
      });
      default = {};
      description = ''
        User accounts to allow connection to the Starbound server.
      '';
    };

    allowAssetsMismatch = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Check whether the assets on the client match the ones from the server
        and deny connection if they don't match.
      '';
    };

    bannedIPs = mkOption {
      type = types.listOf types.str;
      default = [];
      description = ''
        IP addresses disallowed for connection to the server.
      '';
    };

    bannedUUIDs = mkOption {
      type = types.listOf types.str;
      default = [];
      description = ''
        User IDs disallowed for connection to the server.
      '';
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/starbound";
      description = ''
        The directory where Starbound stores its universe/player files.
      '';
    };

    package = mkOption {
      type = types.package;
      default = pkgs.vuizvui.games.humblebundle.starbound;
      defaultText = "pkgs.vuizvui.games.humblebundle.starbound";
      description = ''
        The starbound package to use for running this game server.
      '';
    };

    extraConfig = mkOption {
      type = types.attrs;
      default = {};
      description = ''
        Extra configuration options to add to the server config.
      '';
    };

    rconServer = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to run an RCON server which allows to run administrative
          commands on this game server instance.

          See the <link xlink:href="${
            "https://developer.valvesoftware.com/wiki/Source_RCON_Protocol"
          }">RCON protocol documentation</link> for more information about this.
        '';
      };

      password = mkOption {
        type = types.str;
        default = "";
        description = ''
          The password needed to authorize with the RCON server.
        '';
      };

      timeout = mkOption {
        type = types.int;
        default = 1000;
        # XXX: Find out what this timeout is for and whether it's in seconds.
        description = ''
          After how many seconds the RCON server drops the connection.
        '';
      };
    } // mkListenerOptions "RCON server" 21026;

    queryServer = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to run a query server that shows information such as currently
          connected players.
        '';
      };
    } // mkListenerOptions "query server" 21025;

    safeScripts = {
      enable = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Enable certain limitations of LUA scripts.
        '';
      };

      instructionLimit = mkOption {
        type = types.int;
        default = 10000000;
        description = ''
          The maximum amount of instructions a LUA function can have.
        '';
      };

      instructionMeasureInterval = mkOption {
        type = types.int;
        default = 10000;
        description = ''
          The amount of milliseconds to wait between consecutive checks of the
          <option>instructionLimit</option> on LUA scripts.
        '';
      };

      recursionLimit = mkOption {
        type = types.int;
        default = 100;
        description = ''
          Maximum depth of recursion for LUA scripts.
        '';
      };

      profiling.enable = mkEnableOption "LUA script profiling";
    };

    serverName = mkOption {
      type = types.str;
      default = "A Starbound Server";
      example = "My shiny Starbound Server";
      description = ''
        A short description or name of the Starbound server to run.
      '';
    };

    serverFidelity = mkOption {
      type = types.enum [ "automatic" "minimum" "low" "medium" "high" ];
      default = "automatic";
      example = "high";
      description = ''
        The fidelity profile to use for this server as defined in
        <filename>worldserver.config</filename> inside the packed assets.

        If this is set to <literal>automatic</literal> the server will
        automatically switch between these profiles.
      '';
    };

    maxPlayers = mkOption {
      type = types.int;
      default = 8;
      description = ''
        Maximum amount of players to allow concurrently.
      '';
    };

    maxTeamSize = mkOption {
      type = types.int;
      default = 4;
      description = ''
        Maximum amount of players to allow within a party.
      '';
    };
  } // mkListenerOptions "game server" 21025;

  config = mkIf cfg.enable {
    users.groups.starbound = {
      gid = config.ids.gids.starbound;
    };

    users.users.starbound = {
      uid = config.ids.uids.starbound;
      description = "Starbound Game Server User";
      group = "starbound";
      home = cfg.dataDir;
      createHome = true;
    };

    systemd.services.starbound = {
      description = "Starbound Server";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "fs.target" ];

      serviceConfig = {
        User = "starbound";
        Group = "starbound";
        PrivateTmp = true;

        KillSignal = "SIGINT";

        ExecStart = toString [
          "${cfg.package}/bin/starbound-server"
          "-bootconfig \"${bootConfig}\""
          # Workaround to disable logging to file
          "-logfile \"${gotoRoot cfg.dataDir}/dev/null\""
          "-verbose"
        ];
      };
    };
  };
}