From 052d936cfec2fe741b06754a310ffe7087c17f46 Mon Sep 17 00:00:00 2001 From: Zitrone Date: Sat, 16 Nov 2024 18:03:12 +0100 Subject: [PATCH] nixos/rss-bridge: refactor --- .../modules/services/web-apps/rss-bridge.nix | 81 ++++++++++++------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/nixos/modules/services/web-apps/rss-bridge.nix b/nixos/modules/services/web-apps/rss-bridge.nix index f375635c94b6..0f91c6d6c4b0 100644 --- a/nixos/modules/services/web-apps/rss-bridge.nix +++ b/nixos/modules/services/web-apps/rss-bridge.nix @@ -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)} ''; }; };