]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/phpfpm/default.nix
Move shaarli passwords to secure location
[perso/Immae/Config/Nix.git] / nixops / modules / websites / phpfpm / default.nix
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 serviceDependencies = mkOption {
76 default = {};
77 type = types.attrsOf (types.listOf types.string);
78 example = literalExample ''
79 { mypool = ["postgresql.service"]; }
80 '';
81 description = ''
82 Extra service dependencies specific to pool.
83 '';
84 };
85
86 envFile = mkOption {
87 default = {};
88 type = types.attrsOf types.string;
89 example = literalExample ''
90 { mypool = "path/to/file";
91 }
92 '';
93 description = ''
94 Extra environment file go into the service script.
95 '';
96 };
97
98 poolPhpConfigs = mkOption {
99 default = {};
100 type = types.attrsOf types.lines;
101 example = literalExample ''
102 { mypool = '''
103 extension = some_extension.so
104 ''';
105 }
106 '';
107 description = ''
108 Extra lines that go into the php configuration specific to pool.
109 '';
110 };
111
112 poolConfigs = mkOption {
113 default = {};
114 type = types.attrsOf types.lines;
115 example = literalExample ''
116 { mypool = '''
117 listen = /run/phpfpm/mypool
118 user = nobody
119 pm = dynamic
120 pm.max_children = 75
121 pm.start_servers = 10
122 pm.min_spare_servers = 5
123 pm.max_spare_servers = 20
124 pm.max_requests = 500
125 ''';
126 }
127 '';
128 description = ''
129 A mapping between PHP-FPM pool names and their configurations.
130 See the documentation on <literal>php-fpm.conf</literal> for
131 details on configuration directives. If no pools are defined,
132 the phpfpm service is disabled.
133 '';
134 };
135
136 pools = mkOption {
137 type = types.attrsOf (types.submodule (import ./pool-options.nix {
138 inherit lib;
139 }));
140 default = {};
141 example = literalExample ''
142 {
143 mypool = {
144 listen = "/path/to/unix/socket";
145 extraConfig = '''
146 user = nobody
147 pm = dynamic
148 pm.max_children = 75
149 pm.start_servers = 10
150 pm.min_spare_servers = 5
151 pm.max_spare_servers = 20
152 pm.max_requests = 500
153 ''';
154 }
155 }'';
156 description = ''
157 PHP-FPM pools. If no pools or poolConfigs are defined, the PHP-FPM
158 service is disabled.
159 '';
160 };
161 };
162 };
163
164 config = mkIf enabled {
165
166 systemd.slices.phpfpm = {
167 description = "PHP FastCGI Process manager pools slice";
168 };
169
170 systemd.targets.phpfpm = {
171 description = "PHP FastCGI Process manager pools target";
172 wantedBy = [ "multi-user.target" ];
173 };
174
175 systemd.services = flip mapAttrs' poolConfigs (pool: poolConfig:
176 nameValuePair "phpfpm-${pool}" {
177 description = "PHP FastCGI Process Manager service for pool ${pool}";
178 after = [ "network.target" ] ++ (cfg.serviceDependencies.${pool} or []);
179 wants = cfg.serviceDependencies.${pool} or [];
180 wantedBy = [ "phpfpm.target" ];
181 partOf = [ "phpfpm.target" ];
182 preStart = ''
183 mkdir -p ${stateDir}
184 '';
185 serviceConfig = let
186 cfgFile = fpmCfgFile pool poolConfig;
187 poolPhpIni = cfg.poolPhpConfigs.${pool} or "";
188 in {
189 EnvironmentFile = if builtins.hasAttr pool cfg.envFile then [cfg.envFile.${pool}] else [];
190 Slice = "phpfpm.slice";
191 PrivateDevices = true;
192 ProtectSystem = "full";
193 ProtectHome = true;
194 # XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work
195 RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
196 Type = "notify";
197 ExecStart = "${cfg.phpPackage}/bin/php-fpm -y ${cfgFile} -c ${phpIni poolPhpIni}";
198 ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
199 };
200 }
201 );
202 };
203 }