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 phpSession = mkOption {
39 description = "Handle phpsession files separately in vardir";
41 phpListen = mkOption {
44 description = "Name of the socket to listen to. Defaults to app name if null";
49 description = "Pool configuration to append";
51 phpOptions = mkOption {
54 description = "php configuration to append";
56 phpOpenbasedir = mkOption {
60 paths to add to php open_basedir configuration in addition to app and vardir
63 phpWatchFiles = mkOption {
67 Path to other files to watch to trigger preStart scripts
70 websiteEnv = mkOption {
73 website instance name to use
76 httpdUser = mkOption {
78 default = config.services.httpd.user;
80 httpd user to run the prestart scripts as.
83 httpdGroup = mkOption {
85 default = config.services.httpd.group;
87 httpd group to run the prestart scripts as.
90 httpdWatchFiles = mkOption {
94 Path to other files to watch to trigger httpd reload
100 Path to application root
103 webappName = mkOption {
107 Alias name for the app, to be used in services.websites.webappDirs
113 Path to the web root path of the application. May differ from the application itself (usually a subdirectory)
116 preStartActions = mkOption {
120 List of actions to run as apache user at preStart when
121 whatchFiles or app dir changed.
124 serviceDeps = mkOption {
128 List of systemd services this application depends on
134 # Read-only variables
135 services.phpApplication.phpListenPaths = mkOption {
137 default = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
138 name "/run/phpfpm/${if icfg.phpListen == null then name else icfg.phpListen}.sock"
142 Full paths to listen for php
145 services.phpApplication.webappDirs = mkOption {
147 default = attrsets.filterAttrs (n: v: builtins.hasAttr n cfg.apps) config.services.websites.webappDirsPaths;
150 Stable name webapp dirs for httpd
156 services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair
158 modules = [ "proxy_fcgi" ];
159 watchPaths = builtins.concatLists (map (c: c.httpdWatchFiles) cfgs);
163 services.phpfpm.pools = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
165 listen = cfg.phpListenPaths."${name}";
167 user = ${icfg.httpdUser}
168 group = ${icfg.httpdGroup}
169 listen.owner = ${icfg.httpdUser}
170 listen.group = ${icfg.httpdGroup}
171 ${optionalString (icfg.phpSession) ''
172 php_admin_value[session.save_path] = "${icfg.varDir}/phpSessions"''}
173 php_admin_value[open_basedir] = "${builtins.concatStringsSep ":" ([icfg.app icfg.varDir] ++ icfg.phpWatchFiles ++ icfg.phpOpenbasedir)}"
175 phpOptions = config.services.phpfpm.phpOptions + icfg.phpOptions;
179 services.websites.webappDirs = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
180 (if icfg.webappName == null then name else icfg.webappName) icfg.webRoot
181 ) (attrsets.filterAttrs (n: v: !isNull v.webRoot) cfg.apps);
183 services.filesWatcher = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
186 paths = icfg.phpWatchFiles;
188 ) (attrsets.filterAttrs (n: v: builtins.length v.phpWatchFiles > 0) cfg.apps);
190 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
192 after = lib.mkAfter icfg.serviceDeps;
193 wants = icfg.serviceDeps;
194 preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) ''
195 watchFilesChanged() {
196 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 1"}
197 [ ! -f "${icfg.varDir}"/watchedFiles ] \
198 || ! sha512sum -c --status ${icfg.varDir}/watchedFiles
201 [ ! -f "${icfg.varDir}/currentWebappDir" -o \
202 "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ]
205 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 0"}
206 sha512sum ${builtins.concatStringsSep " " icfg.phpWatchFiles} > ${icfg.varDir}/watchedFiles
209 if watchFilesChanged || appDirChanged; then
210 pushd ${icfg.app} > /dev/null
211 ${builtins.concatStringsSep "\n " (map (c: "/run/wrappers/bin/sudo -u ${icfg.httpdUser} ${c}") icfg.preStartActions) }
213 echo -n "${icfg.app}" > ${icfg.varDir}/currentWebappDir
220 system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
223 text = optionalString (!isNull icfg.varDir) ''
224 install -m ${icfg.mode} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}
225 '' + optionalString (icfg.phpSession) ''
226 install -m 0700 -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/phpSessions
227 '' + builtins.concatStringsSep "\n" (attrsets.mapAttrsToList (n: v: ''
228 install -m ${v} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/${n}
229 '') icfg.varDirPaths);