nixos/rss-bridge: refactor

This commit is contained in:
Zitrone
2025-02-11 22:41:07 +01:00
committed by Felix Buehler
parent 1783f05a0c
commit 052d936cfe
+50 -31
View File
@@ -4,33 +4,53 @@
pkgs,
...
}:
with lib;
let
inherit (lib)
literalExpression
mkDefault
mkEnableOption
mkIf
mkOption
mkRenamedOptionModule
types
;
cfg = config.services.rss-bridge;
poolName = "rss-bridge";
cfgHalf = lib.mapAttrsRecursive (
path: value:
let
envName = lib.toUpper ("RSSBRIDGE_" + lib.concatStringsSep "_" path);
envValue =
if lib.isList value then
lib.concatStringsSep "," value
else if lib.isBool value then
lib.boolToString value
else
toString value;
in
if (value != null) then "fastcgi_param \"${envName}\" \"${envValue}\";" else null
) cfg.config;
cfgEnv = lib.concatStringsSep "\n" (lib.collect lib.isString cfgHalf);
cfgEnv = lib.pipe cfg.config [
(lib.mapAttrsRecursive (
path: value:
lib.optionalAttrs (value != null) {
name = lib.toUpper "RSSBRIDGE_${lib.concatStringsSep "_" path}";
value =
if lib.isList value then
lib.concatStringsSep "," value
else if lib.isBool value then
lib.boolToString value
else
toString value;
}
))
(lib.collect (x: lib.isString x.name or false && lib.isString x.value or false))
lib.listToAttrs
];
in
{
imports = [
(mkRenamedOptionModule
[ "services" "rss-bridge" "whitelist" ]
[ "services" "rss-bridge" "config" "system" "enabled_bridges" ]
[
"services"
"rss-bridge"
"whitelist"
]
[
"services"
"rss-bridge"
"config"
"system"
"enabled_bridges"
]
)
];
@@ -55,12 +75,11 @@ in
};
pool = mkOption {
type = types.str;
default = poolName;
type = types.nullOr types.str;
default = "rss-bridge";
description = ''
Name of existing phpfpm pool that is used to run web-application.
If not specified a pool will be created automatically with
default values.
Name of phpfpm pool that is used to run web-application.
If `null` specified none will be created, otherwise automatically created with default values.
'';
};
@@ -97,12 +116,12 @@ in
type = types.str;
description = "Directory where to store cache files (if cache.type = \"file\").";
default = "${cfg.dataDir}/cache/";
defaultText = options.literalExpression "\${config.services.rss-bridge.dataDir}/cache/";
defaultText = literalExpression "\${config.services.rss-bridge.dataDir}/cache/";
};
};
};
};
example = options.literalExpression ''
example = literalExpression ''
{
system.enabled_bridges = [ "*" ];
error = {
@@ -124,12 +143,12 @@ in
};
config = mkIf cfg.enable {
services.phpfpm.pools = mkIf (cfg.pool == poolName) {
${poolName} = {
services.phpfpm.pools = mkIf (cfg.pool != null) {
${cfg.pool} = {
user = cfg.user;
settings = mapAttrs (name: mkDefault) {
settings = lib.mapAttrs (name: mkDefault) {
"listen.owner" = cfg.user;
"listen.group" = cfg.user;
"listen.group" = cfg.group;
"listen.mode" = "0600";
"pm" = "dynamic";
"pm.max_children" = 75;
@@ -166,7 +185,7 @@ in
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:${config.services.phpfpm.pools.${cfg.pool}.socket};
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
${cfgEnv}
${lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "fastcgi_param \"${n}\" \"${v}\";") cfgEnv)}
'';
};
};