@@ -4,14 +4,17 @@ with lib;
44
55let
66 cfg = config.services.factorio;
7+ factorio = pkgs.factorio-headless;
78 name = "Factorio";
89 stateDir = "/var/lib/factorio";
10+ mkSavePath = name: "${stateDir}/saves/${name}.zip";
911 configFile = pkgs.writeText "factorio.conf" ''
1012 use-system-read-write-data-directories=true
1113 [path]
12− read-data=${pkgs.factorio-headless}/share/factorio/data
14+ read-data=${factorio}/share/factorio/data
1315 write-data=${stateDir}
1416 '';
17+ modDir = pkgs.factorio-mkModDirDrv cfg.mods;
1518in
1619{
1720 options = {
@@ -32,7 +35,8 @@ in
3235 description = ''
3336 The name of the savegame that will be used by the server.
3437
35− When not present in ${stateDir}/saves, it will be generated before starting the service.
38+ When not present in ${stateDir}/saves, a new map with default
39+ settings will be generated before starting the service.
3640 '';
3741 };
3842 # TODO Add more individual settings as nixos-options?
@@ -51,6 +55,26 @@ in
5155 customizations.
5256 '';
5357 };
58+ mods = mkOption {
59+ type = types.listOf types.package;
60+ default = [];
61+ description = ''
62+ Mods the server should install and activate.
63+
64+ The derivations in this list must "build" the mod by simply copying
65+ the .zip, named correctly, into the output directory. Eventually,
66+ there will be a way to pull in the most up-to-date list of
67+ derivations via nixos-channel. Until then, this is for experts only.
68+ '';
69+ };
70+ autosave-interval = mkOption {
71+ type = types.nullOr types.int;
72+ default = null;
73+ example = 2;
74+ description = ''
75+ The time, in minutes, between autosaves.
76+ '';
77+ };
5478 };
5579 };
5680
@@ -74,12 +98,14 @@ in
7498 wantedBy = [ "multi-user.target" ];
7599 after = [ "network.target" ];
76100
77− preStart = ''
78− test -e ${stateDir}/saves/${cfg.saveName}.zip || \
79− ${pkgs.factorio-headless}/bin/factorio \
80− --config=${cfg.configFile} \
81− --create=${stateDir}/saves/${cfg.saveName}.zip
82− '';
101+ preStart = toString [
102+ "test -e ${stateDir}/saves/${cfg.saveName}.zip"
103+ "||"
104+ "${factorio}/bin/factorio"
105+ "--config=${cfg.configFile}"
106+ "--create=${mkSavePath cfg.saveName}"
107+ (optionalString (cfg.mods != []) "--mod-directory=${modDir}")
108+ ];
83109
84110 serviceConfig = {
85111 User = "factorio";
@@ -90,10 +116,12 @@ in
90116 PrivateTmp = true;
91117 UMask = "0007";
92118 ExecStart = toString [
93− "${pkgs.factorio-headless}/bin/factorio"
119+ "${factorio}/bin/factorio"
94120 "--config=${cfg.configFile}"
95121 "--port=${toString cfg.port}"
96− "--start-server=${stateDir}/saves/${cfg.saveName}.zip"
122+ "--start-server=${mkSavePath cfg.saveName}"
123+ (optionalString (cfg.mods != []) "--mod-directory=${modDir}")
124+ (optionalString (cfg.autosave-interval != null) "--autosave-interval ${toString cfg.autosave-interval}")
97125 ];
98126 };
99127 };