1 { lib, config, pkgs, ... }:
4 cfg = config.services.phpApplication;
5 cfgByEnv = lists.groupBy (x: x.websiteEnv) (builtins.attrValues cfg.apps);
8 options = with types; {
9 services.phpApplication.apps = mkOption {
12 php applications to define
14 type = attrsOf (submodule {
19 Path to application’s vardir.
22 varDirPaths = mkOption {
26 Map of additional folders => mode to create under varDir
33 Mode to apply to the vardir
36 phpListen = mkOption {
39 description = "Name of the socket to listen to. Defaults to app name if null";
44 description = "Pool configuration to append";
49 description = "Pool environment to append";
51 phpPackage = mkOption {
54 description = "Php package to use";
56 phpOptions = mkOption {
59 description = "php configuration to append";
61 phpOpenbasedir = mkOption {
65 paths to add to php open_basedir configuration in addition to app and vardir
68 phpWatchFiles = mkOption {
72 Path to other files to watch to trigger preStart scripts
75 websiteEnv = mkOption {
78 website instance name to use
81 httpdUser = mkOption {
83 default = config.services.httpd.user;
85 httpd user to run the prestart scripts as.
88 httpdGroup = mkOption {
90 default = config.services.httpd.group;
92 httpd group to run the prestart scripts as.
95 httpdWatchFiles = mkOption {
99 Path to other files to watch to trigger httpd reload
105 Path to application root
108 preStartActions = mkOption {
112 List of actions to run as apache user at preStart when
113 whatchFiles or app dir changed.
116 serviceDeps = mkOption {
120 List of systemd services this application depends on
126 # Read-only variables
127 services.phpApplication.phpListenPaths = mkOption {
129 default = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
130 name config.services.phpfpm.pools."${name}".socket
134 Full paths to listen for php
140 services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair
142 modules = [ "proxy_fcgi" ];
143 watchPaths = builtins.concatLists (map (c: c.httpdWatchFiles) cfgs);
147 services.phpfpm.phpPackage = pkgs.php74;
148 services.phpfpm.pools = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
150 user = icfg.httpdUser;
151 group = icfg.httpdUser;
153 "listen.owner" = icfg.httpdUser;
154 "listen.group" = icfg.httpdGroup;
155 "php_admin_value[open_basedir]" = builtins.concatStringsSep ":" ([icfg.app icfg.varDir] ++ icfg.phpWatchFiles ++ icfg.phpOpenbasedir);
158 phpOptions = config.services.phpfpm.phpOptions + icfg.phpOptions;
159 inherit (icfg) phpEnv phpPackage;
163 services.filesWatcher = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
166 paths = icfg.phpWatchFiles;
168 ) (attrsets.filterAttrs (n: v: builtins.length v.phpWatchFiles > 0) cfg.apps);
170 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
172 after = lib.mkAfter icfg.serviceDeps;
173 wants = icfg.serviceDeps;
174 preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) ''
175 watchFilesChanged() {
176 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 1"}
177 [ ! -f "${icfg.varDir}"/watchedFiles ] \
178 || ! sha512sum -c --status ${icfg.varDir}/watchedFiles
181 [ ! -f "${icfg.varDir}/currentWebappDir" -o \
182 "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ]
185 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 0"}
186 sha512sum ${builtins.concatStringsSep " " icfg.phpWatchFiles} > ${icfg.varDir}/watchedFiles
189 if watchFilesChanged || appDirChanged; then
190 pushd ${icfg.app} > /dev/null
191 ${builtins.concatStringsSep "\n " (map (c: "/run/wrappers/bin/sudo -u ${icfg.httpdUser} ${c}") icfg.preStartActions) }
193 echo -n "${icfg.app}" > ${icfg.varDir}/currentWebappDir
200 system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
203 text = optionalString (!isNull icfg.varDir) ''
204 install -m ${icfg.mode} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}
205 '' + builtins.concatStringsSep "\n" (attrsets.mapAttrsToList (n: v: ''
206 install -m ${v} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/${n}
207 '') icfg.varDirPaths);