]>
Commit | Line | Data |
---|---|---|
dcac3ec7 | 1 | { lib, config, pkgs, ... }: |
2e48907d IB |
2 | with lib; |
3 | let | |
4 | cfg = config.services.phpApplication; | |
717ccfd9 | 5 | cfgByEnv = lists.groupBy (x: x.websiteEnv) (builtins.attrValues cfg.apps); |
2e48907d IB |
6 | in |
7 | { | |
717ccfd9 IB |
8 | options = with types; { |
9 | services.phpApplication.apps = mkOption { | |
2e48907d IB |
10 | default = {}; |
11 | description = '' | |
12 | php applications to define | |
13 | ''; | |
14 | type = attrsOf (submodule { | |
15 | options = { | |
16 | varDir = mkOption { | |
17 | type = nullOr path; | |
18 | description = '' | |
19 | Path to application’s vardir. | |
20 | ''; | |
21 | }; | |
1594c8da IB |
22 | varDirPaths = mkOption { |
23 | type = attrsOf str; | |
24 | default = {}; | |
25 | description = '' | |
26 | Map of additional folders => mode to create under varDir | |
27 | ''; | |
28 | }; | |
2e48907d IB |
29 | mode = mkOption { |
30 | type = str; | |
31 | default = "0700"; | |
32 | description = '' | |
33 | Mode to apply to the vardir | |
34 | ''; | |
35 | }; | |
717ccfd9 IB |
36 | phpListen = mkOption { |
37 | type = nullOr str; | |
38 | default = null; | |
39 | description = "Name of the socket to listen to. Defaults to app name if null"; | |
40 | }; | |
41 | phpPool = mkOption { | |
5400b9b6 IB |
42 | type = attrsOf str; |
43 | default = {}; | |
717ccfd9 IB |
44 | description = "Pool configuration to append"; |
45 | }; | |
5400b9b6 IB |
46 | phpEnv = mkOption { |
47 | type = attrsOf str; | |
48 | default = {}; | |
49 | description = "Pool environment to append"; | |
50 | }; | |
dcac3ec7 IB |
51 | phpPackage = mkOption { |
52 | type = attrsOf str; | |
53 | default = pkgs.php; | |
54 | description = "Php package to use"; | |
55 | }; | |
717ccfd9 IB |
56 | phpOptions = mkOption { |
57 | type = lines; | |
58 | default = ""; | |
59 | description = "php configuration to append"; | |
60 | }; | |
61 | phpOpenbasedir = mkOption { | |
62 | type = listOf path; | |
63 | default = []; | |
64 | description = '' | |
65 | paths to add to php open_basedir configuration in addition to app and vardir | |
66 | ''; | |
67 | }; | |
68 | phpWatchFiles = mkOption { | |
69 | type = listOf path; | |
70 | default = []; | |
71 | description = '' | |
72 | Path to other files to watch to trigger preStart scripts | |
73 | ''; | |
74 | }; | |
2e48907d IB |
75 | websiteEnv = mkOption { |
76 | type = str; | |
77 | description = '' | |
78 | website instance name to use | |
79 | ''; | |
80 | }; | |
81 | httpdUser = mkOption { | |
82 | type = str; | |
83 | default = config.services.httpd.user; | |
84 | description = '' | |
85 | httpd user to run the prestart scripts as. | |
86 | ''; | |
87 | }; | |
88 | httpdGroup = mkOption { | |
89 | type = str; | |
90 | default = config.services.httpd.group; | |
91 | description = '' | |
92 | httpd group to run the prestart scripts as. | |
93 | ''; | |
94 | }; | |
717ccfd9 IB |
95 | httpdWatchFiles = mkOption { |
96 | type = listOf path; | |
97 | default = []; | |
98 | description = '' | |
99 | Path to other files to watch to trigger httpd reload | |
100 | ''; | |
101 | }; | |
2e48907d IB |
102 | app = mkOption { |
103 | type = path; | |
104 | description = '' | |
105 | Path to application root | |
106 | ''; | |
2e48907d IB |
107 | }; |
108 | preStartActions = mkOption { | |
109 | type = listOf str; | |
110 | default = []; | |
111 | description = '' | |
112 | List of actions to run as apache user at preStart when | |
113 | whatchFiles or app dir changed. | |
114 | ''; | |
115 | }; | |
116 | serviceDeps = mkOption { | |
117 | type = listOf str; | |
118 | default = []; | |
119 | description = '' | |
120 | List of systemd services this application depends on | |
121 | ''; | |
122 | }; | |
2e48907d IB |
123 | }; |
124 | }); | |
125 | }; | |
717ccfd9 IB |
126 | # Read-only variables |
127 | services.phpApplication.phpListenPaths = mkOption { | |
128 | type = attrsOf path; | |
129 | default = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair | |
5400b9b6 | 130 | name config.services.phpfpm.pools."${name}".socket |
717ccfd9 IB |
131 | ) cfg.apps; |
132 | readOnly = true; | |
133 | description = '' | |
134 | Full paths to listen for php | |
135 | ''; | |
136 | }; | |
2e48907d IB |
137 | }; |
138 | ||
139 | config = { | |
29f8cb85 | 140 | services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair |
2e48907d IB |
141 | name { |
142 | modules = [ "proxy_fcgi" ]; | |
717ccfd9 | 143 | watchPaths = builtins.concatLists (map (c: c.httpdWatchFiles) cfgs); |
2e48907d IB |
144 | } |
145 | ) cfgByEnv; | |
146 | ||
e34b3079 | 147 | services.phpfpm.phpPackage = pkgs.php74; |
717ccfd9 IB |
148 | services.phpfpm.pools = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair |
149 | name { | |
5400b9b6 IB |
150 | user = icfg.httpdUser; |
151 | group = icfg.httpdUser; | |
152 | settings = { | |
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); | |
156 | } | |
5400b9b6 | 157 | // icfg.phpPool; |
717ccfd9 | 158 | phpOptions = config.services.phpfpm.phpOptions + icfg.phpOptions; |
dcac3ec7 | 159 | inherit (icfg) phpEnv phpPackage; |
717ccfd9 IB |
160 | } |
161 | ) cfg.apps; | |
162 | ||
1594c8da IB |
163 | services.filesWatcher = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair |
164 | "phpfpm-${name}" { | |
165 | restart = true; | |
166 | paths = icfg.phpWatchFiles; | |
167 | } | |
168 | ) (attrsets.filterAttrs (n: v: builtins.length v.phpWatchFiles > 0) cfg.apps); | |
169 | ||
2e48907d IB |
170 | systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair |
171 | "phpfpm-${name}" { | |
172 | after = lib.mkAfter icfg.serviceDeps; | |
173 | wants = icfg.serviceDeps; | |
174 | preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) '' | |
175 | watchFilesChanged() { | |
717ccfd9 | 176 | ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 1"} |
2e48907d IB |
177 | [ ! -f "${icfg.varDir}"/watchedFiles ] \ |
178 | || ! sha512sum -c --status ${icfg.varDir}/watchedFiles | |
179 | } | |
180 | appDirChanged() { | |
181 | [ ! -f "${icfg.varDir}/currentWebappDir" -o \ | |
182 | "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ] | |
183 | } | |
184 | updateWatchFiles() { | |
717ccfd9 IB |
185 | ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 0"} |
186 | sha512sum ${builtins.concatStringsSep " " icfg.phpWatchFiles} > ${icfg.varDir}/watchedFiles | |
2e48907d IB |
187 | } |
188 | ||
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) } | |
192 | popd > /dev/null | |
193 | echo -n "${icfg.app}" > ${icfg.varDir}/currentWebappDir | |
194 | updateWatchFiles | |
195 | fi | |
196 | ''); | |
197 | } | |
717ccfd9 | 198 | ) cfg.apps; |
2e48907d IB |
199 | |
200 | system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair | |
201 | name { | |
202 | deps = []; | |
203 | text = optionalString (!isNull icfg.varDir) '' | |
204 | install -m ${icfg.mode} -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir} | |
1594c8da IB |
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); | |
2e48907d | 208 | } |
717ccfd9 | 209 | ) cfg.apps; |
2e48907d IB |
210 | }; |
211 | } |