2016-04-12 04:17:05 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.factorio;
|
|
|
|
name = "Factorio";
|
2019-05-01 19:56:58 +00:00
|
|
|
stateDir = "/var/lib/${cfg.stateDirName}";
|
2016-08-03 13:44:15 +00:00
|
|
|
mkSavePath = name: "${stateDir}/saves/${name}.zip";
|
2016-04-12 04:17:05 +00:00
|
|
|
configFile = pkgs.writeText "factorio.conf" ''
|
|
|
|
use-system-read-write-data-directories=true
|
|
|
|
[path]
|
2020-01-09 15:07:50 +00:00
|
|
|
read-data=${cfg.package}/share/factorio/data
|
2016-04-12 04:17:05 +00:00
|
|
|
write-data=${stateDir}
|
|
|
|
'';
|
2017-01-22 15:51:11 +00:00
|
|
|
serverSettings = {
|
|
|
|
name = cfg.game-name;
|
|
|
|
description = cfg.description;
|
|
|
|
visibility = {
|
|
|
|
public = cfg.public;
|
|
|
|
lan = cfg.lan;
|
|
|
|
};
|
|
|
|
username = cfg.username;
|
|
|
|
password = cfg.password;
|
|
|
|
token = cfg.token;
|
|
|
|
game_password = cfg.game-password;
|
2017-11-23 12:50:19 +00:00
|
|
|
require_user_verification = cfg.requireUserVerification;
|
2017-01-22 15:51:11 +00:00
|
|
|
max_upload_in_kilobytes_per_second = 0;
|
|
|
|
minimum_latency_in_ticks = 0;
|
|
|
|
ignore_player_limit_for_returning_players = false;
|
|
|
|
allow_commands = "admins-only";
|
|
|
|
autosave_interval = cfg.autosave-interval;
|
|
|
|
autosave_slots = 5;
|
|
|
|
afk_autokick_interval = 0;
|
|
|
|
auto_pause = true;
|
|
|
|
only_admins_can_pause_the_game = true;
|
|
|
|
autosave_only_on_server = true;
|
|
|
|
admins = [];
|
2020-01-09 15:07:50 +00:00
|
|
|
} // cfg.extraSettings;
|
2017-01-22 15:51:11 +00:00
|
|
|
serverSettingsFile = pkgs.writeText "server-settings.json" (builtins.toJSON (filterAttrsRecursive (n: v: v != null) serverSettings));
|
2017-07-19 21:09:18 +00:00
|
|
|
modDir = pkgs.factorio-utils.mkModDirDrv cfg.mods;
|
2016-04-12 04:17:05 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.factorio = {
|
|
|
|
enable = mkEnableOption name;
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 34197;
|
|
|
|
description = ''
|
|
|
|
The port to which the service should bind.
|
2020-11-14 16:21:56 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
2020-11-24 20:17:34 +00:00
|
|
|
default = false;
|
2020-11-14 16:21:56 +00:00
|
|
|
description = ''
|
|
|
|
Whether to automatically open the specified UDP port in the firewall.
|
2016-04-12 04:17:05 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
saveName = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2016-04-12 04:17:05 +00:00
|
|
|
default = "default";
|
|
|
|
description = ''
|
|
|
|
The name of the savegame that will be used by the server.
|
|
|
|
|
2016-08-03 13:44:15 +00:00
|
|
|
When not present in ${stateDir}/saves, a new map with default
|
|
|
|
settings will be generated before starting the service.
|
2016-04-12 04:17:05 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
# TODO Add more individual settings as nixos-options?
|
|
|
|
# TODO XXX The server tries to copy a newly created config file over the old one
|
|
|
|
# on shutdown, but fails, because it's in the nix store. When is this needed?
|
|
|
|
# Can an admin set options in-game and expect to have them persisted?
|
|
|
|
configFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = configFile;
|
|
|
|
defaultText = "configFile";
|
|
|
|
description = ''
|
|
|
|
The server's configuration file.
|
|
|
|
|
|
|
|
The default file generated by this module contains lines essential to
|
|
|
|
the server's operation. Use its contents as a basis for any
|
|
|
|
customizations.
|
|
|
|
'';
|
|
|
|
};
|
2019-05-01 19:56:58 +00:00
|
|
|
stateDirName = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2019-05-01 19:56:58 +00:00
|
|
|
default = "factorio";
|
2017-11-23 12:50:19 +00:00
|
|
|
description = ''
|
2019-05-01 19:56:58 +00:00
|
|
|
Name of the directory under /var/lib holding the server's data.
|
2017-11-23 12:50:19 +00:00
|
|
|
|
|
|
|
The configuration and map will be stored here.
|
|
|
|
'';
|
|
|
|
};
|
2016-08-03 13:44:15 +00:00
|
|
|
mods = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Mods the server should install and activate.
|
|
|
|
|
|
|
|
The derivations in this list must "build" the mod by simply copying
|
|
|
|
the .zip, named correctly, into the output directory. Eventually,
|
|
|
|
there will be a way to pull in the most up-to-date list of
|
|
|
|
derivations via nixos-channel. Until then, this is for experts only.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-22 15:51:11 +00:00
|
|
|
game-name = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = "Factorio Game";
|
|
|
|
description = ''
|
|
|
|
Name of the game as it will appear in the game listing.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
description = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Description of the game that will appear in the listing.
|
|
|
|
'';
|
|
|
|
};
|
2020-01-09 15:07:50 +00:00
|
|
|
extraSettings = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = { admins = [ "username" ];};
|
|
|
|
description = ''
|
|
|
|
Extra game configuration that will go into server-settings.json
|
|
|
|
'';
|
|
|
|
};
|
2017-01-22 15:51:11 +00:00
|
|
|
public = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Game will be published on the official Factorio matching server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
lan = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Game will be broadcast on LAN.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
username = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Your factorio.com login credentials. Required for games with visibility public.
|
|
|
|
'';
|
|
|
|
};
|
2020-01-09 15:07:50 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.factorio-headless;
|
|
|
|
defaultText = "pkgs.factorio-headless";
|
|
|
|
example = "pkgs.factorio-headless-experimental";
|
|
|
|
description = ''
|
|
|
|
Factorio version to use. This defaults to the stable channel.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-22 15:51:11 +00:00
|
|
|
password = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Your factorio.com login credentials. Required for games with visibility public.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
token = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Authentication token. May be used instead of 'password' above.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
game-password = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.nullOr types.str;
|
2017-01-22 15:51:11 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Game password.
|
|
|
|
'';
|
|
|
|
};
|
2017-11-23 12:50:19 +00:00
|
|
|
requireUserVerification = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
When set to true, the server will only allow clients that have a valid factorio.com account.
|
|
|
|
'';
|
|
|
|
};
|
2016-08-03 13:44:15 +00:00
|
|
|
autosave-interval = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2017-01-22 15:51:11 +00:00
|
|
|
example = 10;
|
2016-08-03 13:44:15 +00:00
|
|
|
description = ''
|
2017-01-22 15:51:11 +00:00
|
|
|
Autosave interval in minutes.
|
2016-08-03 13:44:15 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-04-12 04:17:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.factorio = {
|
|
|
|
description = "Factorio headless server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
2016-08-03 13:44:15 +00:00
|
|
|
preStart = toString [
|
|
|
|
"test -e ${stateDir}/saves/${cfg.saveName}.zip"
|
|
|
|
"||"
|
2020-01-09 15:07:50 +00:00
|
|
|
"${cfg.package}/bin/factorio"
|
2016-08-03 13:44:15 +00:00
|
|
|
"--config=${cfg.configFile}"
|
|
|
|
"--create=${mkSavePath cfg.saveName}"
|
|
|
|
(optionalString (cfg.mods != []) "--mod-directory=${modDir}")
|
|
|
|
];
|
2016-04-12 04:17:05 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
|
|
|
KillSignal = "SIGINT";
|
2019-05-01 19:56:58 +00:00
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = cfg.stateDirName;
|
2016-04-12 04:17:05 +00:00
|
|
|
UMask = "0007";
|
|
|
|
ExecStart = toString [
|
2020-01-09 15:07:50 +00:00
|
|
|
"${cfg.package}/bin/factorio"
|
2016-04-12 04:17:05 +00:00
|
|
|
"--config=${cfg.configFile}"
|
|
|
|
"--port=${toString cfg.port}"
|
2016-08-03 13:44:15 +00:00
|
|
|
"--start-server=${mkSavePath cfg.saveName}"
|
2017-01-22 15:51:11 +00:00
|
|
|
"--server-settings=${serverSettingsFile}"
|
2016-08-03 13:44:15 +00:00
|
|
|
(optionalString (cfg.mods != []) "--mod-directory=${modDir}")
|
2016-04-12 04:17:05 +00:00
|
|
|
];
|
2019-05-01 19:56:58 +00:00
|
|
|
|
|
|
|
# Sandboxing
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
2016-04-12 04:17:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-14 16:21:56 +00:00
|
|
|
networking.firewall.allowedUDPPorts = if cfg.openFirewall then [ cfg.port ] else [];
|
2016-04-12 04:17:05 +00:00
|
|
|
};
|
|
|
|
}
|