diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2019-01-25 23:15:08 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2019-01-25 23:15:08 +0100 |
commit | 01f21083a897b86bf148f1d2bb9c8edca4d3786a (patch) | |
tree | 784f04e9b6ef99a49e572c84e4b7ab40b5eb5fde /nixops/modules/websites/phpfpm | |
parent | bfe3c9c9df0c5112bc8806483292b55ed0f7e02d (diff) | |
download | Nix-01f21083a897b86bf148f1d2bb9c8edca4d3786a.tar.gz Nix-01f21083a897b86bf148f1d2bb9c8edca4d3786a.tar.zst Nix-01f21083a897b86bf148f1d2bb9c8edca4d3786a.zip |
Rename virtual folder to nixops
Fixes https://git.immae.eu/mantisbt/view.php?id=82
Diffstat (limited to 'nixops/modules/websites/phpfpm')
-rw-r--r-- | nixops/modules/websites/phpfpm/default.nix | 178 | ||||
-rw-r--r-- | nixops/modules/websites/phpfpm/pool-options.nix | 35 |
2 files changed, 213 insertions, 0 deletions
diff --git a/nixops/modules/websites/phpfpm/default.nix b/nixops/modules/websites/phpfpm/default.nix new file mode 100644 index 0000000..3c6f027 --- /dev/null +++ b/nixops/modules/websites/phpfpm/default.nix | |||
@@ -0,0 +1,178 @@ | |||
1 | { config, lib, pkgs, ... }: | ||
2 | |||
3 | with lib; | ||
4 | |||
5 | let | ||
6 | cfg = config.services.myPhpfpm; | ||
7 | enabled = cfg.poolConfigs != {} || cfg.pools != {}; | ||
8 | |||
9 | stateDir = "/run/phpfpm"; | ||
10 | |||
11 | poolConfigs = cfg.poolConfigs // mapAttrs mkPool cfg.pools; | ||
12 | |||
13 | mkPool = n: p: '' | ||
14 | listen = ${p.listen} | ||
15 | ${p.extraConfig} | ||
16 | ''; | ||
17 | |||
18 | fpmCfgFile = pool: poolConfig: pkgs.writeText "phpfpm-${pool}.conf" '' | ||
19 | [global] | ||
20 | error_log = syslog | ||
21 | daemonize = no | ||
22 | ${cfg.extraConfig} | ||
23 | |||
24 | [${pool}] | ||
25 | ${poolConfig} | ||
26 | ''; | ||
27 | |||
28 | phpIni = poolPhpOptions: (pkgs.runCommand "php.ini" { | ||
29 | inherit (cfg) phpPackage phpOptions; | ||
30 | inherit poolPhpOptions; | ||
31 | nixDefaults = '' | ||
32 | sendmail_path = "/run/wrappers/bin/sendmail -t -i" | ||
33 | ''; | ||
34 | passAsFile = [ "nixDefaults" "phpOptions" "poolPhpOptions" ]; | ||
35 | } '' | ||
36 | cat $phpPackage/etc/php.ini $nixDefaultsPath $phpOptionsPath $poolPhpOptionsPath > $out | ||
37 | ''); | ||
38 | |||
39 | in { | ||
40 | |||
41 | options = { | ||
42 | services.myPhpfpm = { | ||
43 | extraConfig = mkOption { | ||
44 | type = types.lines; | ||
45 | default = ""; | ||
46 | description = '' | ||
47 | Extra configuration that should be put in the global section of | ||
48 | the PHP-FPM configuration file. Do not specify the options | ||
49 | <literal>error_log</literal> or | ||
50 | <literal>daemonize</literal> here, since they are generated by | ||
51 | NixOS. | ||
52 | ''; | ||
53 | }; | ||
54 | |||
55 | phpPackage = mkOption { | ||
56 | type = types.package; | ||
57 | default = pkgs.php; | ||
58 | defaultText = "pkgs.php"; | ||
59 | description = '' | ||
60 | The PHP package to use for running the PHP-FPM service. | ||
61 | ''; | ||
62 | }; | ||
63 | |||
64 | phpOptions = mkOption { | ||
65 | type = types.lines; | ||
66 | default = ""; | ||
67 | example = | ||
68 | '' | ||
69 | date.timezone = "CET" | ||
70 | ''; | ||
71 | description = | ||
72 | "Options appended to the PHP configuration file <filename>php.ini</filename>."; | ||
73 | }; | ||
74 | |||
75 | poolPhpConfigs = mkOption { | ||
76 | default = {}; | ||
77 | type = types.attrsOf types.lines; | ||
78 | example = literalExample '' | ||
79 | { mypool = ''' | ||
80 | extension = some_extension.so | ||
81 | '''; | ||
82 | } | ||
83 | ''; | ||
84 | description = '' | ||
85 | Extra lines that go into the php configuration specific to pool. | ||
86 | ''; | ||
87 | }; | ||
88 | |||
89 | poolConfigs = mkOption { | ||
90 | default = {}; | ||
91 | type = types.attrsOf types.lines; | ||
92 | example = literalExample '' | ||
93 | { mypool = ''' | ||
94 | listen = /run/phpfpm/mypool | ||
95 | user = nobody | ||
96 | pm = dynamic | ||
97 | pm.max_children = 75 | ||
98 | pm.start_servers = 10 | ||
99 | pm.min_spare_servers = 5 | ||
100 | pm.max_spare_servers = 20 | ||
101 | pm.max_requests = 500 | ||
102 | '''; | ||
103 | } | ||
104 | ''; | ||
105 | description = '' | ||
106 | A mapping between PHP-FPM pool names and their configurations. | ||
107 | See the documentation on <literal>php-fpm.conf</literal> for | ||
108 | details on configuration directives. If no pools are defined, | ||
109 | the phpfpm service is disabled. | ||
110 | ''; | ||
111 | }; | ||
112 | |||
113 | pools = mkOption { | ||
114 | type = types.attrsOf (types.submodule (import ./pool-options.nix { | ||
115 | inherit lib; | ||
116 | })); | ||
117 | default = {}; | ||
118 | example = literalExample '' | ||
119 | { | ||
120 | mypool = { | ||
121 | listen = "/path/to/unix/socket"; | ||
122 | extraConfig = ''' | ||
123 | user = nobody | ||
124 | pm = dynamic | ||
125 | pm.max_children = 75 | ||
126 | pm.start_servers = 10 | ||
127 | pm.min_spare_servers = 5 | ||
128 | pm.max_spare_servers = 20 | ||
129 | pm.max_requests = 500 | ||
130 | '''; | ||
131 | } | ||
132 | }''; | ||
133 | description = '' | ||
134 | PHP-FPM pools. If no pools or poolConfigs are defined, the PHP-FPM | ||
135 | service is disabled. | ||
136 | ''; | ||
137 | }; | ||
138 | }; | ||
139 | }; | ||
140 | |||
141 | config = mkIf enabled { | ||
142 | |||
143 | systemd.slices.phpfpm = { | ||
144 | description = "PHP FastCGI Process manager pools slice"; | ||
145 | }; | ||
146 | |||
147 | systemd.targets.phpfpm = { | ||
148 | description = "PHP FastCGI Process manager pools target"; | ||
149 | wantedBy = [ "multi-user.target" ]; | ||
150 | }; | ||
151 | |||
152 | systemd.services = flip mapAttrs' poolConfigs (pool: poolConfig: | ||
153 | nameValuePair "phpfpm-${pool}" { | ||
154 | description = "PHP FastCGI Process Manager service for pool ${pool}"; | ||
155 | after = [ "network.target" ]; | ||
156 | wantedBy = [ "phpfpm.target" ]; | ||
157 | partOf = [ "phpfpm.target" ]; | ||
158 | preStart = '' | ||
159 | mkdir -p ${stateDir} | ||
160 | ''; | ||
161 | serviceConfig = let | ||
162 | cfgFile = fpmCfgFile pool poolConfig; | ||
163 | poolPhpIni = cfg.poolPhpConfigs.${pool} or ""; | ||
164 | in { | ||
165 | Slice = "phpfpm.slice"; | ||
166 | PrivateDevices = true; | ||
167 | ProtectSystem = "full"; | ||
168 | ProtectHome = true; | ||
169 | # XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work | ||
170 | RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK"; | ||
171 | Type = "notify"; | ||
172 | ExecStart = "${cfg.phpPackage}/bin/php-fpm -y ${cfgFile} -c ${phpIni poolPhpIni}"; | ||
173 | ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID"; | ||
174 | }; | ||
175 | } | ||
176 | ); | ||
177 | }; | ||
178 | } | ||
diff --git a/nixops/modules/websites/phpfpm/pool-options.nix b/nixops/modules/websites/phpfpm/pool-options.nix new file mode 100644 index 0000000..cc688c2 --- /dev/null +++ b/nixops/modules/websites/phpfpm/pool-options.nix | |||
@@ -0,0 +1,35 @@ | |||
1 | { lib }: | ||
2 | |||
3 | with lib; { | ||
4 | |||
5 | options = { | ||
6 | |||
7 | listen = mkOption { | ||
8 | type = types.str; | ||
9 | example = "/path/to/unix/socket"; | ||
10 | description = '' | ||
11 | The address on which to accept FastCGI requests. | ||
12 | ''; | ||
13 | }; | ||
14 | |||
15 | extraConfig = mkOption { | ||
16 | type = types.lines; | ||
17 | example = '' | ||
18 | user = nobody | ||
19 | pm = dynamic | ||
20 | pm.max_children = 75 | ||
21 | pm.start_servers = 10 | ||
22 | pm.min_spare_servers = 5 | ||
23 | pm.max_spare_servers = 20 | ||
24 | pm.max_requests = 500 | ||
25 | ''; | ||
26 | |||
27 | description = '' | ||
28 | Extra lines that go into the pool configuration. | ||
29 | See the documentation on <literal>php-fpm.conf</literal> for | ||
30 | details on configuration directives. | ||
31 | ''; | ||
32 | }; | ||
33 | }; | ||
34 | } | ||
35 | |||