1 { config, lib, pkgs, ... }:
3 cfg = config.services.crossfire-server;
6 options.services.crossfire-server = {
7 enable = lib.mkOption {
11 If enabled, the Crossfire game server will be started at boot.
15 package = lib.mkPackageOption pkgs "crossfire-server" {
18 This will also be used for map/arch data, if you don't change {option}`dataDir`
23 dataDir = lib.mkOption {
25 default = "${cfg.package}/share/crossfire";
26 defaultText = lib.literalExpression ''"''${config.services.crossfire.package}/share/crossfire"'';
28 Where to load readonly data from -- maps, archetypes, treasure tables,
29 and the like. If you plan to edit the data on the live server (rather
30 than overlaying the crossfire-maps and crossfire-arch packages and
31 nixos-rebuilding), point this somewhere read-write and copy the data
32 there before starting the server.
36 stateDir = lib.mkOption {
38 default = "/var/lib/crossfire";
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 Text to append to the corresponding configuration files. Note that the
61 files given in the example are *not* the complete set of files available
62 to customize; look in /etc/crossfire after enabling the server to see
63 the available files, and read the comments in each file for detailed
64 documentation on the format and what settings are available.
66 Note that the motd, rules, and news files, if configured here, will
67 overwrite the example files that come with the server, rather than being
68 appended to them as the other configuration files are.
70 example = lib.literalExpression ''
73 admin:secret_password:localhost
79 # So is everyone on 192.168.86.255/24
83 metaserver2_notification on
84 localhostname crossfire.example.net
86 motd = "Welcome to CrossFire!";
87 news = "No news yet.";
88 rules = "Don't be a jerk.";
90 # be nicer to newbies and harsher to experienced players
91 balanced_stat_loss true
92 # don't let players pick up and use admin-created items
101 config = lib.mkIf cfg.enable {
102 users.users.crossfire = {
103 description = "Crossfire server daemon user";
109 users.groups.crossfire = {};
111 # Merge the cfg.configFiles setting with the default files shipped with
113 # For most files this consists of reading ${crossfire}/etc/crossfire/${name}
114 # and appending the user setting to it; the motd, news, and rules are handled
115 # specially, with user-provided values completely replacing the original.
116 environment.etc = lib.attrsets.mapAttrs'
117 (name: value: lib.attrsets.nameValuePair "crossfire/${name}" {
120 (lib.optionalString (!lib.elem name ["motd" "news" "rules"])
121 (lib.fileContents "${cfg.package}/etc/crossfire/${name}"))
129 motd = lib.fileContents "${cfg.package}/etc/crossfire/motd";
130 news = lib.fileContents "${cfg.package}/etc/crossfire/news";
131 rules = lib.fileContents "${cfg.package}/etc/crossfire/rules";
134 } // cfg.configFiles);
136 systemd.services.crossfire-server = {
137 description = "Crossfire Server Daemon";
138 wantedBy = [ "multi-user.target" ];
139 after = [ "network.target" ];
141 serviceConfig = lib.mkMerge [
143 ExecStart = "${cfg.package}/bin/crossfire-server -conf /etc/crossfire -local '${cfg.stateDir}' -data '${cfg.dataDir}'";
147 WorkingDirectory = cfg.stateDir;
149 (lib.mkIf (cfg.stateDir == "/var/lib/crossfire") {
150 StateDirectory = "crossfire";
154 # The crossfire server needs access to a bunch of files at runtime that
155 # are not created automatically at server startup; they're meant to be
156 # installed in $PREFIX/var/crossfire by `make install`. And those files
157 # need to be writeable, so we can't just point at the ones in the nix
158 # store. Instead we take the approach of copying them out of the store
159 # on first run. If `bookarch` already exists, we assume the rest of the
160 # files do as well, and copy nothing -- otherwise we risk ovewriting
161 # server state information every time the server is upgraded.
163 if [ ! -e "${cfg.stateDir}"/bookarch ]; then
164 ${pkgs.rsync}/bin/rsync -a --chmod=u=rwX,go=rX \
165 "${cfg.package}/var/crossfire/" "${cfg.stateDir}/"
170 networking.firewall = lib.mkIf cfg.openFirewall {
171 allowedTCPPorts = [ serverPort ];