1 { config, lib, pkgs, ... }:
3 cfg = config.services.deliantra-server;
6 options.services.deliantra-server = {
7 enable = lib.mkOption {
11 If enabled, the Deliantra game server will be started at boot.
15 package = lib.mkPackageOption pkgs "deliantra-server" {
18 This will also be used for map/arch data, if you don't change {option}`dataDir`
23 dataDir = lib.mkOption {
25 default = "${pkgs.deliantra-data}";
26 defaultText = lib.literalExpression ''"''${pkgs.deliantra-data}"'';
28 Where to store readonly data (maps, archetypes, sprites, etc).
29 Note that if you plan to use the live map editor (rather than editing
30 the maps offline and then nixos-rebuilding), THIS MUST BE WRITEABLE --
31 copy the deliantra-data someplace writeable (say,
32 /var/lib/deliantra/data) and update this option accordingly.
36 stateDir = lib.mkOption {
38 default = "/var/lib/deliantra";
40 Where to store runtime data (save files, persistent items, etc).
42 If left at the default, this will be automatically created on server
43 startup if it does not already exist. If changed, it is the admin's
44 responsibility to make sure that the directory exists and is writeable
45 by the `crossfire` user.
49 openFirewall = lib.mkOption {
50 type = lib.types.bool;
53 Whether to open ports in the firewall for the server.
57 configFiles = lib.mkOption {
58 type = lib.types.attrsOf lib.types.str;
60 Contents of the server configuration files. These will be appended to
61 the example configurations the server comes with and overwrite any
62 default settings defined therein.
64 The example here is not comprehensive. See the files in
65 /etc/deliantra-server after enabling this module for full documentation.
67 example = lib.literalExpression ''
70 admin:secret_password:localhost
73 motd = "Welcome to Deliantra!";
75 # Settings for game mechanics.
76 stat_loss_on_death true
80 # Settings for the server daemon.
81 hiscore_url https://deliantra.example.net/scores/
92 config = lib.mkIf cfg.enable {
93 users.users.deliantra = {
94 description = "Deliantra server daemon user";
100 users.groups.deliantra = {};
102 # Merge the cfg.configFiles setting with the default files shipped with
104 # For most files this consists of reading
105 # ${deliantra}/etc/deliantra-server/${name} and appending the user setting
107 environment.etc = lib.attrsets.mapAttrs'
108 (name: value: lib.attrsets.nameValuePair "deliantra-server/${name}" {
111 # Deliantra doesn't come with a motd file, but respects it if present
113 (lib.optionalString (name != "motd")
114 (lib.fileContents "${cfg.package}/etc/deliantra-server/${name}"))
121 } // cfg.configFiles);
123 systemd.services.deliantra-server = {
124 description = "Deliantra Server Daemon";
125 wantedBy = [ "multi-user.target" ];
126 after = [ "network.target" ];
129 DELIANTRA_DATADIR="${cfg.dataDir}";
130 DELIANTRA_LOCALDIR="${cfg.stateDir}";
131 DELIANTRA_CONFDIR="/etc/deliantra-server";
134 serviceConfig = lib.mkMerge [
136 ExecStart = "${cfg.package}/bin/deliantra-server";
140 WorkingDirectory = cfg.stateDir;
142 (lib.mkIf (cfg.stateDir == "/var/lib/deliantra") {
143 StateDirectory = "deliantra";
147 # The deliantra server needs access to a bunch of files at runtime that
148 # are not created automatically at server startup; they're meant to be
149 # installed in $PREFIX/var/deliantra-server by `make install`. And those
150 # files need to be writeable, so we can't just point at the ones in the
151 # nix store. Instead we take the approach of copying them out of the store
152 # on first run. If `bookarch` already exists, we assume the rest of the
153 # files do as well, and copy nothing -- otherwise we risk ovewriting
154 # server state information every time the server is upgraded.
156 if [ ! -e "${cfg.stateDir}"/bookarch ]; then
157 ${pkgs.rsync}/bin/rsync -a --chmod=u=rwX,go=rX \
158 "${cfg.package}/var/deliantra-server/" "${cfg.stateDir}/"
163 networking.firewall = lib.mkIf cfg.openFirewall {
164 allowedTCPPorts = [ serverPort ];