1 { config, lib, pkgs, ... }:
5 cfg = config.services.freshrss;
9 extension-env = pkgs.buildEnv {
10 name = "freshrss-extensions";
11 paths = cfg.extensions;
14 DATA_PATH = cfg.dataDir;
15 THIRDPARTY_EXTENSIONS_PATH = "${extension-env}/share/freshrss/";
19 meta.maintainers = with maintainers; [ etu stunkymonkey mattchrist ];
21 options.services.freshrss = {
22 enable = mkEnableOption "FreshRSS RSS aggregator and reader with php-fpm backend";
24 package = mkPackageOption pkgs "freshrss" { };
26 extensions = mkOption {
27 type = types.listOf types.package;
29 defaultText = literalExpression "[]";
30 example = literalExpression ''
31 with freshrss-extensions; [
34 (freshrss-extensions.buildFreshRssExtension {
35 FreshRssExtUniqueId = "ReadingTime";
36 pname = "reading-time";
38 src = pkgs.fetchFromGitLab {
39 domain = "framagit.org";
41 repo = "FreshRSS_Extension-ReadingTime";
42 rev = "fb6e9e944ef6c5299fa56ffddbe04c41e5a34ebf";
43 hash = "sha256-C5cRfaphx4Qz2xg2z+v5qRji8WVSIpvzMbethTdSqsk=";
48 description = "Additional extensions to be used.";
51 defaultUser = mkOption {
54 description = "Default username for FreshRSS.";
58 passwordFile = mkOption {
59 type = types.nullOr types.path;
61 description = "Password for the defaultUser for FreshRSS.";
62 example = "/run/secrets/freshrss";
67 description = "Default URL for FreshRSS.";
68 example = "https://freshrss.example.com";
74 description = "Default language for FreshRSS.";
80 type = types.enum [ "sqlite" "pgsql" "mysql" ];
82 description = "Database type.";
87 type = types.nullOr types.str;
88 default = "localhost";
89 description = "Database host for FreshRSS.";
93 type = types.nullOr types.port;
95 description = "Database port for FreshRSS.";
100 type = types.nullOr types.str;
101 default = "freshrss";
102 description = "Database user for FreshRSS.";
105 passFile = mkOption {
106 type = types.nullOr types.path;
108 description = "Database password file for FreshRSS.";
109 example = "/run/secrets/freshrss";
113 type = types.nullOr types.str;
114 default = "freshrss";
115 description = "Database name for FreshRSS.";
118 tableprefix = mkOption {
119 type = types.nullOr types.str;
121 description = "Database table prefix for FreshRSS.";
122 example = "freshrss";
128 default = "/var/lib/freshrss";
129 description = "Default data folder for FreshRSS.";
130 example = "/mnt/freshrss";
133 virtualHost = mkOption {
134 type = types.nullOr types.str;
135 default = "freshrss";
137 Name of the nginx virtualhost to use and setup. If null, do not setup any virtualhost.
138 You may need to configure the virtualhost further through services.nginx.virtualHosts.<virtualhost>,
139 for example to enable SSL.
147 Name of the php-fpm pool to use and setup. If not specified, a pool will be created
154 default = "freshrss";
155 description = "User under which FreshRSS runs.";
158 authType = mkOption {
159 type = types.enum [ "form" "http_auth" "none" ];
161 description = "Authentication type for FreshRSS.";
167 defaultServiceConfig = {
168 ReadWritePaths = "${cfg.dataDir}";
169 CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
171 LockPersonality = true;
172 NoNewPrivileges = true;
173 PrivateDevices = true;
178 ProtectControlGroups = true;
180 ProtectHostname = true;
181 ProtectKernelLogs = true;
182 ProtectKernelModules = true;
183 ProtectKernelTunables = true;
184 ProtectProc = "invisible";
185 ProtectSystem = "strict";
187 RestrictNamespaces = true;
188 RestrictRealtime = true;
189 RestrictSUIDSGID = true;
190 SystemCallArchitectures = "native";
191 SystemCallFilter = [ "@system-service" "~@resources" "~@privileged" ];
195 Group = config.users.users.${cfg.user}.group;
196 StateDirectory = "freshrss";
197 WorkingDirectory = cfg.package;
201 assertions = mkIf (cfg.authType == "form") [
203 assertion = cfg.passwordFile != null;
205 `passwordFile` must be supplied when using "form" authentication!
209 # Set up a Nginx virtual host.
210 services.nginx = mkIf (cfg.virtualHost != null) {
212 virtualHosts.${cfg.virtualHost} = {
213 root = "${cfg.package}/p";
216 # this regex is mandatory because of the API
217 locations."~ ^.+?\.php(/.*)?$".extraConfig = ''
218 fastcgi_pass unix:${config.services.phpfpm.pools.${cfg.pool}.socket};
219 fastcgi_split_path_info ^(.+\.php)(/.*)$;
220 # By default, the variable PATH_INFO is not set under PHP-FPM
221 # But FreshRSS API greader.php need it. If you have a “Bad Request” error, double check this var!
222 # NOTE: the separate $path_info variable is required. For more details, see:
223 # https://trac.nginx.org/nginx/ticket/321
224 set $path_info $fastcgi_path_info;
225 fastcgi_param PATH_INFO $path_info;
226 include ${pkgs.nginx}/conf/fastcgi_params;
227 include ${pkgs.nginx}/conf/fastcgi.conf;
231 tryFiles = "$uri $uri/ index.php";
232 index = "index.php index.html index.htm";
238 services.phpfpm.pools = mkIf (cfg.pool == poolName) {
242 "listen.owner" = "nginx";
243 "listen.group" = "nginx";
244 "listen.mode" = "0600";
246 "pm.max_children" = 32;
247 "pm.max_requests" = 500;
248 "pm.start_servers" = 2;
249 "pm.min_spare_servers" = 2;
250 "pm.max_spare_servers" = 5;
251 "catch_workers_output" = true;
257 users.users."${cfg.user}" = {
258 description = "FreshRSS service user";
260 group = "${cfg.user}";
263 users.groups."${cfg.user}" = { };
265 systemd.tmpfiles.settings."10-freshrss".${cfg.dataDir}.d = {
267 group = config.users.users.${cfg.user}.group;
270 systemd.services.freshrss-config =
272 settingsFlags = concatStringsSep " \\\n "
273 (mapAttrsToList (k: v: "${k} ${toString v}") {
274 "--default_user" = ''"${cfg.defaultUser}"'';
275 "--auth_type" = ''"${cfg.authType}"'';
276 "--base_url" = ''"${cfg.baseUrl}"'';
277 "--language" = ''"${cfg.language}"'';
278 "--db-type" = ''"${cfg.database.type}"'';
279 # The following attributes are optional depending on the type of
280 # database. Those that evaluate to null on the left hand side
282 ${if cfg.database.name != null then "--db-base" else null} = ''"${cfg.database.name}"'';
283 ${if cfg.database.passFile != null then "--db-password" else null} = ''"$(cat ${cfg.database.passFile})"'';
284 ${if cfg.database.user != null then "--db-user" else null} = ''"${cfg.database.user}"'';
285 ${if cfg.database.tableprefix != null then "--db-prefix" else null} = ''"${cfg.database.tableprefix}"'';
286 # hostname:port e.g. "localhost:5432"
287 ${if cfg.database.host != null && cfg.database.port != null then "--db-host" else null} = ''"${cfg.database.host}:${toString cfg.database.port}"'';
288 # socket path e.g. "/run/postgresql"
289 ${if cfg.database.host != null && cfg.database.port == null then "--db-host" else null} = ''"${cfg.database.host}"'';
293 description = "Set up the state directory for FreshRSS before use";
294 wantedBy = [ "multi-user.target" ];
295 serviceConfig = defaultServiceConfig // {
296 RemainAfterExit = true;
298 restartIfChanged = true;
299 environment = env-vars;
303 userScriptArgs = ''--user ${cfg.defaultUser} ${optionalString (cfg.authType == "form") ''--password "$(cat ${cfg.passwordFile})"''}'';
304 updateUserScript = optionalString (cfg.authType == "form" || cfg.authType == "none") ''
305 ./cli/update-user.php ${userScriptArgs}
307 createUserScript = optionalString (cfg.authType == "form" || cfg.authType == "none") ''
308 ./cli/create-user.php ${userScriptArgs}
312 # do installation or reconfigure
313 if test -f ${cfg.dataDir}/config.php; then
314 # reconfigure with settings
315 ./cli/reconfigure.php ${settingsFlags}
318 # check correct folders in data folder
320 # install with settings
321 ./cli/do-install.php ${settingsFlags}
327 systemd.services.freshrss-updater = {
328 description = "FreshRSS feed updater";
329 after = [ "freshrss-config.service" ];
331 environment = env-vars;
332 serviceConfig = defaultServiceConfig // {
333 ExecStart = "${cfg.package}/app/actualize_script.php";