aboutsummaryrefslogtreecommitdiff
path: root/modules/websites/php-application.nix
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2019-06-02 09:48:05 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2019-06-02 11:23:54 +0200
commit717ccfd957e686d773480df817387aebbe79aa48 (patch)
treeb2f59c57da32dcafefee9ccc5d7e6d32e498721e /modules/websites/php-application.nix
parent29f8cb850d74b456d6481a456311bbf5361d328c (diff)
downloadNix-717ccfd957e686d773480df817387aebbe79aa48.tar.gz
Nix-717ccfd957e686d773480df817387aebbe79aa48.tar.zst
Nix-717ccfd957e686d773480df817387aebbe79aa48.zip
Finish moving aten php configuration to dedicated module
Diffstat (limited to 'modules/websites/php-application.nix')
-rw-r--r--modules/websites/php-application.nix101
1 files changed, 83 insertions, 18 deletions
diff --git a/modules/websites/php-application.nix b/modules/websites/php-application.nix
index 765d406..1bc4872 100644
--- a/modules/websites/php-application.nix
+++ b/modules/websites/php-application.nix
@@ -2,11 +2,11 @@
2with lib; 2with lib;
3let 3let
4 cfg = config.services.phpApplication; 4 cfg = config.services.phpApplication;
5 cfgByEnv = lists.groupBy (x: x.websiteEnv) (builtins.attrValues cfg); 5 cfgByEnv = lists.groupBy (x: x.websiteEnv) (builtins.attrValues cfg.apps);
6in 6in
7{ 7{
8 options = { 8 options = with types; {
9 services.phpApplication = with types; mkOption { 9 services.phpApplication.apps = mkOption {
10 default = {}; 10 default = {};
11 description = '' 11 description = ''
12 php applications to define 12 php applications to define
@@ -31,6 +31,35 @@ in
31 default = true; 31 default = true;
32 description = "Handle phpsession files separately in vardir"; 32 description = "Handle phpsession files separately in vardir";
33 }; 33 };
34 phpListen = mkOption {
35 type = nullOr str;
36 default = null;
37 description = "Name of the socket to listen to. Defaults to app name if null";
38 };
39 phpPool = mkOption {
40 type = lines;
41 default = "";
42 description = "Pool configuration to append";
43 };
44 phpOptions = mkOption {
45 type = lines;
46 default = "";
47 description = "php configuration to append";
48 };
49 phpOpenbasedir = mkOption {
50 type = listOf path;
51 default = [];
52 description = ''
53 paths to add to php open_basedir configuration in addition to app and vardir
54 '';
55 };
56 phpWatchFiles = mkOption {
57 type = listOf path;
58 default = [];
59 description = ''
60 Path to other files to watch to trigger preStart scripts
61 '';
62 };
34 websiteEnv = mkOption { 63 websiteEnv = mkOption {
35 type = str; 64 type = str;
36 description = '' 65 description = ''
@@ -51,6 +80,13 @@ in
51 httpd group to run the prestart scripts as. 80 httpd group to run the prestart scripts as.
52 ''; 81 '';
53 }; 82 };
83 httpdWatchFiles = mkOption {
84 type = listOf path;
85 default = [];
86 description = ''
87 Path to other files to watch to trigger httpd reload
88 '';
89 };
54 app = mkOption { 90 app = mkOption {
55 type = path; 91 type = path;
56 description = '' 92 description = ''
@@ -59,6 +95,7 @@ in
59 }; 95 };
60 webappName = mkOption { 96 webappName = mkOption {
61 type = nullOr str; 97 type = nullOr str;
98 default = null;
62 description = '' 99 description = ''
63 Alias name for the app, to be used in services.websites.webappDirs 100 Alias name for the app, to be used in services.websites.webappDirs
64 ''; 101 '';
@@ -84,29 +121,57 @@ in
84 List of systemd services this application depends on 121 List of systemd services this application depends on
85 ''; 122 '';
86 }; 123 };
87 watchFiles = mkOption {
88 type = listOf path;
89 default = [];
90 description = ''
91 Path to other files to watch to trigger preStart scripts
92 '';
93 };
94 }; 124 };
95 }); 125 });
96 }; 126 };
127 # Read-only variables
128 services.phpApplication.phpListenPaths = mkOption {
129 type = attrsOf path;
130 default = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
131 name "/run/phpfpm/${if icfg.phpListen == null then name else icfg.phpListen}.sock"
132 ) cfg.apps;
133 readOnly = true;
134 description = ''
135 Full paths to listen for php
136 '';
137 };
138 services.phpApplication.webappDirs = mkOption {
139 type = attrsOf path;
140 default = attrsets.filterAttrs (n: v: builtins.hasAttr n cfg.apps) config.services.websites.webappDirsPaths;
141 readOnly = true;
142 description = ''
143 Stable name webapp dirs for httpd
144 '';
145 };
97 }; 146 };
98 147
99 config = { 148 config = {
100 services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair 149 services.websites.env = attrsets.mapAttrs' (name: cfgs: attrsets.nameValuePair
101 name { 150 name {
102 modules = [ "proxy_fcgi" ]; 151 modules = [ "proxy_fcgi" ];
103 watchPaths = builtins.concatLists (map (c: c.watchFiles) cfgs); 152 watchPaths = builtins.concatLists (map (c: c.httpdWatchFiles) cfgs);
104 } 153 }
105 ) cfgByEnv; 154 ) cfgByEnv;
106 155
156 services.phpfpm.pools = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
157 name {
158 listen = cfg.phpListenPaths."${name}";
159 extraConfig = ''
160 user = ${icfg.httpdUser}
161 group = ${icfg.httpdGroup}
162 listen.owner = ${icfg.httpdUser}
163 listen.group = ${icfg.httpdGroup}
164 ${optionalString (icfg.phpSession) ''
165 php_admin_value[session.save_path] = "${icfg.varDir}/phpSessions"''}
166 php_admin_value[open_basedir] = "${builtins.concatStringsSep ":" ([icfg.app icfg.varDir] ++ icfg.phpOpenbasedir)}"
167 '' + icfg.phpPool;
168 phpOptions = config.services.phpfpm.phpOptions + icfg.phpOptions;
169 }
170 ) cfg.apps;
171
107 services.websites.webappDirs = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair 172 services.websites.webappDirs = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
108 icfg.webappName icfg.webRoot 173 (if icfg.webappName == null then name else icfg.webappName) icfg.webRoot
109 ) (attrsets.filterAttrs (n: v: !isNull v.webappName && !isNull v.webRoot) cfg); 174 ) (attrsets.filterAttrs (n: v: !isNull v.webRoot) cfg.apps);
110 175
111 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair 176 systemd.services = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
112 "phpfpm-${name}" { 177 "phpfpm-${name}" {
@@ -114,7 +179,7 @@ in
114 wants = icfg.serviceDeps; 179 wants = icfg.serviceDeps;
115 preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) '' 180 preStart = lib.mkAfter (optionalString (!isNull icfg.varDir) ''
116 watchFilesChanged() { 181 watchFilesChanged() {
117 ${optionalString (builtins.length icfg.watchFiles == 0) "return 0"} 182 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 1"}
118 [ ! -f "${icfg.varDir}"/watchedFiles ] \ 183 [ ! -f "${icfg.varDir}"/watchedFiles ] \
119 || ! sha512sum -c --status ${icfg.varDir}/watchedFiles 184 || ! sha512sum -c --status ${icfg.varDir}/watchedFiles
120 } 185 }
@@ -123,8 +188,8 @@ in
123 "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ] 188 "${icfg.app}" != "$(cat ${icfg.varDir}/currentWebappDir 2>/dev/null)" ]
124 } 189 }
125 updateWatchFiles() { 190 updateWatchFiles() {
126 ${optionalString (builtins.length icfg.watchFiles == 0) "return 0"} 191 ${optionalString (builtins.length icfg.phpWatchFiles == 0) "return 0"}
127 sha512sum ${builtins.concatStringsSep " " icfg.watchFiles} > ${icfg.varDir}/watchedFiles 192 sha512sum ${builtins.concatStringsSep " " icfg.phpWatchFiles} > ${icfg.varDir}/watchedFiles
128 } 193 }
129 194
130 if watchFilesChanged || appDirChanged; then 195 if watchFilesChanged || appDirChanged; then
@@ -136,7 +201,7 @@ in
136 fi 201 fi
137 ''); 202 '');
138 } 203 }
139 ) cfg; 204 ) cfg.apps;
140 205
141 system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair 206 system.activationScripts = attrsets.mapAttrs' (name: icfg: attrsets.nameValuePair
142 name { 207 name {
@@ -147,6 +212,6 @@ in
147 install -m 0700 -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/phpSessions 212 install -m 0700 -o ${icfg.httpdUser} -g ${icfg.httpdGroup} -d ${icfg.varDir}/phpSessions
148 ''; 213 '';
149 } 214 }
150 ) cfg; 215 ) cfg.apps;
151 }; 216 };
152} 217}