2021-02-05 17:12:51 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.freeciv;
|
|
|
|
inherit (config.users) groups;
|
|
|
|
rootDir = "/run/freeciv";
|
|
|
|
argsFormat = {
|
|
|
|
type = with lib.types; let
|
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool int float str
|
|
|
|
(listOf valueType)
|
|
|
|
]) // {
|
|
|
|
description = "freeciv-server params";
|
|
|
|
};
|
|
|
|
in valueType;
|
|
|
|
generate = name: value:
|
|
|
|
let mkParam = k: v:
|
|
|
|
if v == null then []
|
2023-07-15 17:15:38 +00:00
|
|
|
else if isBool v then optional v ("--"+k)
|
2021-02-05 17:12:51 +00:00
|
|
|
else [("--"+k) v];
|
|
|
|
mkParams = k: v: map (mkParam k) (if isList v then v else [v]);
|
|
|
|
in escapeShellArgs (concatLists (concatLists (mapAttrsToList mkParams value)));
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.freeciv = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption ''freeciv'';
|
2021-02-05 17:12:51 +00:00
|
|
|
settings = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Parameters of freeciv-server.
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = argsFormat.type;
|
|
|
|
options.Announce = mkOption {
|
|
|
|
type = types.enum ["IPv4" "IPv6" "none"];
|
|
|
|
default = "none";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Announce game in LAN using given protocol.";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
options.auth = mkEnableOption "server authentication";
|
2021-02-05 17:12:51 +00:00
|
|
|
options.Database = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
apply = pkgs.writeText "auth.conf";
|
|
|
|
default = ''
|
|
|
|
[fcdb]
|
|
|
|
backend="sqlite"
|
|
|
|
database="/var/lib/freeciv/auth.sqlite"
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Enable database connection with given configuration.";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
|
|
|
options.debug = mkOption {
|
|
|
|
type = types.ints.between 0 3;
|
|
|
|
default = 0;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Set debug log level.";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
options.exit-on-end = mkEnableOption "exit instead of restarting when a game ends";
|
|
|
|
options.Guests = mkEnableOption "guests to login if auth is enabled";
|
|
|
|
options.Newusers = mkEnableOption "new users to login if auth is enabled";
|
2021-02-05 17:12:51 +00:00
|
|
|
options.port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 5556;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Listen for clients on given port";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
|
|
|
options.quitidle = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Quit if no players for given time in seconds.";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
|
|
|
options.read = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
apply = v: pkgs.writeTextDir "read.serv" v + "/read";
|
|
|
|
default = ''
|
|
|
|
/fcdb lua sqlite_createdb()
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Startup script.";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
|
|
|
options.saves = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "/var/lib/freeciv/saves/";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Save games to given directory,
|
|
|
|
a sub-directory named after the starting date of the service
|
|
|
|
will me inserted to preserve older saves.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
openFirewall = mkEnableOption "opening the firewall for the port listening for clients";
|
2021-02-05 17:12:51 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
users.groups.freeciv = {};
|
|
|
|
# Use with:
|
|
|
|
# journalctl -u freeciv.service -f -o cat &
|
|
|
|
# cat >/run/freeciv.stdin
|
|
|
|
# load saves/2020-11-14_05-22-27/freeciv-T0005-Y-3750-interrupted.sav.bz2
|
|
|
|
systemd.sockets.freeciv = {
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig = {
|
|
|
|
ListenFIFO = "/run/freeciv.stdin";
|
|
|
|
SocketGroup = groups.freeciv.name;
|
|
|
|
SocketMode = "660";
|
|
|
|
RemoveOnStop = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.services.freeciv = {
|
|
|
|
description = "Freeciv Service";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
environment.HOME = "/var/lib/freeciv";
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "5s";
|
|
|
|
StandardInput = "fd:freeciv.socket";
|
|
|
|
StandardOutput = "journal";
|
|
|
|
StandardError = "journal";
|
|
|
|
ExecStart = pkgs.writeShellScript "freeciv-server" (''
|
|
|
|
set -eux
|
|
|
|
savedir=$(date +%Y-%m-%d_%H-%M-%S)
|
|
|
|
'' + "${pkgs.freeciv}/bin/freeciv-server"
|
|
|
|
+ " " + optionalString (cfg.settings.saves != null)
|
|
|
|
(concatStringsSep " " [ "--saves" "${escapeShellArg cfg.settings.saves}/$savedir" ])
|
|
|
|
+ " " + argsFormat.generate "freeciv-server" (cfg.settings // { saves = null; }));
|
|
|
|
DynamicUser = true;
|
|
|
|
# Create rootDir in the host's mount namespace.
|
|
|
|
RuntimeDirectory = [(baseNameOf rootDir)];
|
|
|
|
RuntimeDirectoryMode = "755";
|
|
|
|
StateDirectory = [ "freeciv" ];
|
|
|
|
WorkingDirectory = "/var/lib/freeciv";
|
|
|
|
# Avoid mounting rootDir in the own rootDir of ExecStart='s mount namespace.
|
|
|
|
InaccessiblePaths = ["-+${rootDir}"];
|
|
|
|
# This is for BindPaths= and BindReadOnlyPaths=
|
|
|
|
# to allow traversal of directories they create in RootDirectory=.
|
|
|
|
UMask = "0066";
|
|
|
|
RootDirectory = rootDir;
|
|
|
|
RootDirectoryStartOnly = true;
|
|
|
|
MountAPIVFS = true;
|
|
|
|
BindReadOnlyPaths = [
|
|
|
|
builtins.storeDir
|
|
|
|
"/etc"
|
|
|
|
"/run"
|
|
|
|
];
|
|
|
|
# The following options are only for optimizing:
|
|
|
|
# systemd-analyze security freeciv
|
|
|
|
AmbientCapabilities = "";
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# ProtectClock= adds DeviceAllow=char-rtc r
|
|
|
|
DeviceAllow = "";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateNetwork = mkDefault false;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
RemoveIPC = true;
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
# Groups in @system-service which do not contain a syscall listed by:
|
|
|
|
# perf stat -x, 2>perf.log -e 'syscalls:sys_enter_*' freeciv-server
|
|
|
|
# in tests, and seem likely not necessary for freeciv-server.
|
|
|
|
"~@aio" "~@chown" "~@ipc" "~@keyring" "~@memlock"
|
|
|
|
"~@resources" "~@setuid" "~@sync" "~@timer"
|
|
|
|
];
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallErrorNumber = "EPERM";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
networking.firewall = mkIf cfg.openFirewall
|
|
|
|
{ allowedTCPPorts = [ cfg.settings.port ]; };
|
|
|
|
};
|
|
|
|
meta.maintainers = with lib.maintainers; [ julm ];
|
|
|
|
}
|