summary refs log tree commit diff
path: root/nixos/modules/services/games/deliantra-server.nix
blob: b7011f4c3542880ca1446db0350f1ea5b6dfc9b5 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.deliantra-server;
  serverPort = 13327;
in {
  options.services.deliantra-server = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        If enabled, the Deliantra game server will be started at boot.
      '';
    };

    package = mkOption {
      type = types.package;
      default = pkgs.deliantra-server;
      defaultText = literalExpression "pkgs.deliantra-server";
      description = ''
        The package to use for the Deliantra server (and map/arch data, if you
        don't change dataDir).
      '';
    };

    dataDir = mkOption {
      type = types.str;
      default = "${pkgs.deliantra-data}";
      defaultText = literalExpression ''"''${pkgs.deliantra-data}"'';
      description = ''
        Where to store readonly data (maps, archetypes, sprites, etc).
        Note that if you plan to use the live map editor (rather than editing
        the maps offline and then nixos-rebuilding), THIS MUST BE WRITEABLE --
        copy the deliantra-data someplace writeable (say,
        /var/lib/deliantra/data) and update this option accordingly.
      '';
    };

    stateDir = mkOption {
      type = types.str;
      default = "/var/lib/deliantra";
      description = ''
        Where to store runtime data (save files, persistent items, etc).

        If left at the default, this will be automatically created on server
        startup if it does not already exist. If changed, it is the admin's
        responsibility to make sure that the directory exists and is writeable
        by the `crossfire` user.
      '';
    };

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to open ports in the firewall for the server.
      '';
    };

    configFiles = mkOption {
      type = types.attrsOf types.str;
      description = ''
        Contents of the server configuration files. These will be appended to
        the example configurations the server comes with and overwrite any
        default settings defined therein.

        The example here is not comprehensive. See the files in
        /etc/deliantra-server after enabling this module for full documentation.
      '';
      example = literalExpression ''
        {
          dm_file = '''
            admin:secret_password:localhost
            jane:xyzzy:*
          ''';
          motd = "Welcome to Deliantra!";
          settings = '''
            # Settings for game mechanics.
            stat_loss_on_death true
            armor_max_enchant 7
          ''';
          config = '''
            # Settings for the server daemon.
            hiscore_url https://deliantra.example.net/scores/
            max_map_reset 86400
          ''';
        }
      '';
      default = {
        motd = "";
      };
    };
  };

  config = mkIf cfg.enable {
    users.users.deliantra = {
      description     = "Deliantra server daemon user";
      home            = cfg.stateDir;
      createHome      = false;
      isSystemUser    = true;
      group           = "deliantra";
    };
    users.groups.deliantra = {};

    # Merge the cfg.configFiles setting with the default files shipped with
    # Deliantra.
    # For most files this consists of reading
    # ${deliantra}/etc/deliantra-server/${name} and appending the user setting
    # to it.
    environment.etc = lib.attrsets.mapAttrs'
      (name: value: lib.attrsets.nameValuePair "deliantra-server/${name}" {
        mode = "0644";
        text =
          # Deliantra doesn't come with a motd file, but respects it if present
          # in /etc.
          (optionalString (name != "motd")
            (fileContents "${cfg.package}/etc/deliantra-server/${name}"))
          + "\n${value}";
      }) ({
        motd = "";
        settings = "";
        config = "";
        dm_file = "";
      } // cfg.configFiles);

    systemd.services.deliantra-server = {
      description   = "Deliantra Server Daemon";
      wantedBy      = [ "multi-user.target" ];
      after         = [ "network.target" ];

      environment = {
        DELIANTRA_DATADIR="${cfg.dataDir}";
        DELIANTRA_LOCALDIR="${cfg.stateDir}";
        DELIANTRA_CONFDIR="/etc/deliantra-server";
      };

      serviceConfig = mkMerge [
        {
          ExecStart = "${cfg.package}/bin/deliantra-server";
          Restart = "always";
          User = "deliantra";
          Group = "deliantra";
          WorkingDirectory = cfg.stateDir;
        }
        (mkIf (cfg.stateDir == "/var/lib/deliantra") {
          StateDirectory = "deliantra";
        })
      ];

      # The deliantra server needs access to a bunch of files at runtime that
      # are not created automatically at server startup; they're meant to be
      # installed in $PREFIX/var/deliantra-server by `make install`. And those
      # files need to be writeable, so we can't just point at the ones in the
      # nix store. Instead we take the approach of copying them out of the store
      # on first run. If `bookarch` already exists, we assume the rest of the
      # files do as well, and copy nothing -- otherwise we risk ovewriting
      # server state information every time the server is upgraded.
      preStart = ''
        if [ ! -e "${cfg.stateDir}"/bookarch ]; then
          ${pkgs.rsync}/bin/rsync -a --chmod=u=rwX,go=rX \
            "${cfg.package}/var/deliantra-server/" "${cfg.stateDir}/"
        fi
      '';
    };

    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = [ serverPort ];
    };
  };
}