1 { config, lib, pkgs, ... }:
6 cfg = config.services.phpfpm;
8 runtimeDir = "/run/phpfpm";
11 if true == value then "yes"
12 else if false == value then "no"
15 fpmCfgFile = pool: poolOpts: pkgs.writeText "phpfpm-${pool}.conf" ''
17 ${concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") cfg.settings)}
18 ${optionalString (cfg.extraConfig != null) cfg.extraConfig}
21 ${concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") poolOpts.settings)}
22 ${concatStringsSep "\n" (mapAttrsToList (n: v: "env[${n}] = ${toStr v}") poolOpts.phpEnv)}
23 ${optionalString (poolOpts.extraConfig != null) poolOpts.extraConfig}
26 phpIni = poolOpts: pkgs.runCommand "php.ini" {
27 inherit (poolOpts) phpPackage phpOptions;
28 preferLocalBuild = true;
29 passAsFile = [ "phpOptions" ];
31 cat ${poolOpts.phpPackage}/etc/php.ini $phpOptionsPath > $out
34 poolOpts = { name, ... }:
36 poolOpts = cfg.pools.${name};
44 Path to the unix socket file on which to accept FastCGI requests.
47 This option is read-only and managed by NixOS.
50 example = "${runtimeDir}/<name>.sock";
56 example = "/path/to/unix/socket";
58 The address on which to accept FastCGI requests.
62 phpPackage = mkOption {
64 default = cfg.phpPackage;
65 defaultText = literalExpression "config.services.phpfpm.phpPackage";
67 The PHP package to use for running this PHP-FPM pool.
71 phpOptions = mkOption {
74 "Options appended to the PHP configuration file {file}`php.ini` used for this PHP-FPM pool."
78 phpEnv = lib.mkOption {
79 type = with types; attrsOf str;
82 Environment variables used for this PHP-FPM pool.
84 example = literalExpression ''
86 HOSTNAME = "$HOSTNAME";
96 description = "User account under which this pool runs.";
101 description = "Group account under which this pool runs.";
104 settings = mkOption {
105 type = with types; attrsOf (oneOf [ str int bool ]);
108 PHP-FPM pool directives. Refer to the "List of pool directives" section of
109 <https://www.php.net/manual/en/install.fpm.configuration.php>
110 for details. Note that settings names must be enclosed in quotes (e.g.
111 `"pm.max_children"` instead of `pm.max_children`).
113 example = literalExpression ''
116 "pm.max_children" = 75;
117 "pm.start_servers" = 10;
118 "pm.min_spare_servers" = 5;
119 "pm.max_spare_servers" = 20;
120 "pm.max_requests" = 500;
125 extraConfig = mkOption {
126 type = with types; nullOr lines;
129 Extra lines that go into the pool configuration.
130 See the documentation on `php-fpm.conf` for
131 details on configuration directives.
137 socket = if poolOpts.listen == "" then "${runtimeDir}/${name}.sock" else poolOpts.listen;
138 group = mkDefault poolOpts.user;
139 phpOptions = mkBefore cfg.phpOptions;
141 settings = mapAttrs (name: mkDefault){
142 listen = poolOpts.socket;
143 user = poolOpts.user;
144 group = poolOpts.group;
151 (mkRemovedOptionModule [ "services" "phpfpm" "poolConfigs" ] "Use services.phpfpm.pools instead.")
152 (mkRemovedOptionModule [ "services" "phpfpm" "phpIni" ] "")
157 settings = mkOption {
158 type = with types; attrsOf (oneOf [ str int bool ]);
161 PHP-FPM global directives. Refer to the "List of global php-fpm.conf directives" section of
162 <https://www.php.net/manual/en/install.fpm.configuration.php>
163 for details. Note that settings names must be enclosed in quotes (e.g.
164 `"pm.max_children"` instead of `pm.max_children`).
165 You need not specify the options `error_log` or
166 `daemonize` here, since they are generated by NixOS.
170 extraConfig = mkOption {
171 type = with types; nullOr lines;
174 Extra configuration that should be put in the global section of
175 the PHP-FPM configuration file. Do not specify the options
177 `daemonize` here, since they are generated by
182 phpPackage = mkPackageOption pkgs "php" { };
184 phpOptions = mkOption {
189 date.timezone = "CET"
192 Options appended to the PHP configuration file {file}`php.ini`.
197 type = types.attrsOf (types.submodule poolOpts);
199 example = literalExpression ''
204 phpPackage = pkgs.php;
207 "pm.max_children" = 75;
208 "pm.start_servers" = 10;
209 "pm.min_spare_servers" = 5;
210 "pm.max_spare_servers" = 20;
211 "pm.max_requests" = 500;
216 PHP-FPM pools. If no pools are defined, the PHP-FPM
223 config = mkIf (cfg.pools != {}) {
226 mapAttrsToList (pool: poolOpts: ''
227 Using config.services.phpfpm.pools.${pool}.listen is deprecated and will become unsupported in a future release. Please reference the read-only option config.services.phpfpm.pools.${pool}.socket to access the path of your socket.
228 '') (filterAttrs (pool: poolOpts: poolOpts.listen != "") cfg.pools) ++
229 mapAttrsToList (pool: poolOpts: ''
230 Using config.services.phpfpm.pools.${pool}.extraConfig is deprecated and will become unsupported in a future release. Please migrate your configuration to config.services.phpfpm.pools.${pool}.settings.
231 '') (filterAttrs (pool: poolOpts: poolOpts.extraConfig != null) cfg.pools) ++
232 optional (cfg.extraConfig != null) ''
233 Using config.services.phpfpm.extraConfig is deprecated and will become unsupported in a future release. Please migrate your configuration to config.services.phpfpm.settings.
237 services.phpfpm.settings = {
238 error_log = "syslog";
242 systemd.slices.phpfpm = {
243 description = "PHP FastCGI Process manager pools slice";
246 systemd.targets.phpfpm = {
247 description = "PHP FastCGI Process manager pools target";
248 wantedBy = [ "multi-user.target" ];
251 systemd.services = mapAttrs' (pool: poolOpts:
252 nameValuePair "phpfpm-${pool}" {
253 description = "PHP FastCGI Process Manager service for pool ${pool}";
254 after = [ "network.target" ];
255 wantedBy = [ "phpfpm.target" ];
256 partOf = [ "phpfpm.target" ];
258 cfgFile = fpmCfgFile pool poolOpts;
259 iniFile = phpIni poolOpts;
261 Slice = "phpfpm.slice";
262 PrivateDevices = true;
264 ProtectSystem = "full";
266 # XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work
267 RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
269 ExecStart = "${poolOpts.phpPackage}/bin/php-fpm -y ${cfgFile} -c ${iniFile}";
270 ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
271 RuntimeDirectory = "phpfpm";
272 RuntimeDirectoryPreserve = true; # Relevant when multiple processes are running