2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2014-03-11 22:46:57 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2014-03-11 22:46:57 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.phpfpm;
|
|
|
|
|
2019-08-02 02:48:53 +01:00
|
|
|
runtimeDir = "/run/phpfpm";
|
2019-06-29 02:47:43 +01:00
|
|
|
|
2019-07-03 17:11:38 +01:00
|
|
|
fpmCfgFile = pool: poolOpts: pkgs.writeText "phpfpm-${pool}.conf" ''
|
2014-03-11 22:46:57 +00:00
|
|
|
[global]
|
|
|
|
error_log = syslog
|
2016-10-05 16:30:31 +01:00
|
|
|
daemonize = no
|
2019-06-29 02:47:43 +01:00
|
|
|
${cfg.extraConfig}
|
2014-03-11 22:46:57 +00:00
|
|
|
|
2017-01-25 22:21:33 +00:00
|
|
|
[${pool}]
|
2019-07-03 22:34:17 +01:00
|
|
|
listen = ${poolOpts.socket}
|
2019-08-03 01:56:13 +01:00
|
|
|
user = ${poolOpts.user}
|
|
|
|
group = ${poolOpts.group}
|
2019-07-03 17:11:38 +01:00
|
|
|
${poolOpts.extraConfig}
|
2014-03-11 22:46:57 +00:00
|
|
|
'';
|
|
|
|
|
2019-07-03 17:11:38 +01:00
|
|
|
phpIni = poolOpts: pkgs.runCommand "php.ini" {
|
|
|
|
inherit (poolOpts) phpPackage phpOptions;
|
2018-11-08 10:59:03 +00:00
|
|
|
preferLocalBuild = true;
|
2017-03-09 19:19:28 +00:00
|
|
|
nixDefaults = ''
|
|
|
|
sendmail_path = "/run/wrappers/bin/sendmail -t -i"
|
|
|
|
'';
|
|
|
|
passAsFile = [ "nixDefaults" "phpOptions" ];
|
2017-02-26 12:29:46 +00:00
|
|
|
} ''
|
2017-03-09 19:19:28 +00:00
|
|
|
cat $phpPackage/etc/php.ini $nixDefaultsPath $phpOptionsPath > $out
|
2016-04-29 07:26:20 +01:00
|
|
|
'';
|
|
|
|
|
2019-07-03 22:34:17 +01:00
|
|
|
poolOpts = { lib, name, ... }:
|
|
|
|
let
|
|
|
|
poolOpts = cfg.pools."${name}";
|
|
|
|
in
|
2019-07-03 17:18:27 +01:00
|
|
|
{
|
|
|
|
options = {
|
2019-07-03 22:34:17 +01:00
|
|
|
socket = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
readOnly = true;
|
|
|
|
description = ''
|
|
|
|
Path to the unix socket file on which to accept FastCGI requests.
|
|
|
|
<note><para>This option is read-only and managed by NixOS.</para></note>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-07-03 17:18:27 +01:00
|
|
|
listen = mkOption {
|
|
|
|
type = types.str;
|
2019-07-03 22:34:17 +01:00
|
|
|
default = "";
|
2019-07-03 17:18:27 +01:00
|
|
|
example = "/path/to/unix/socket";
|
|
|
|
description = ''
|
|
|
|
The address on which to accept FastCGI requests.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
phpPackage = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = cfg.phpPackage;
|
|
|
|
defaultText = "config.services.phpfpm.phpPackage";
|
|
|
|
description = ''
|
|
|
|
The PHP package to use for running this PHP-FPM pool.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
phpOptions = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = cfg.phpOptions;
|
|
|
|
defaultText = "config.services.phpfpm.phpOptions";
|
|
|
|
description = ''
|
|
|
|
"Options appended to the PHP configuration file <filename>php.ini</filename> used for this PHP-FPM pool."
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-08-03 01:56:13 +01:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "User account under which this pool runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Group account under which this pool runs.";
|
|
|
|
};
|
|
|
|
|
2019-07-03 17:18:27 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
example = ''
|
|
|
|
pm = dynamic
|
|
|
|
pm.max_children = 75
|
|
|
|
pm.start_servers = 10
|
|
|
|
pm.min_spare_servers = 5
|
|
|
|
pm.max_spare_servers = 20
|
|
|
|
pm.max_requests = 500
|
|
|
|
'';
|
|
|
|
|
|
|
|
description = ''
|
|
|
|
Extra lines that go into the pool configuration.
|
|
|
|
See the documentation on <literal>php-fpm.conf</literal> for
|
|
|
|
details on configuration directives.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2019-07-03 22:34:17 +01:00
|
|
|
|
|
|
|
config = {
|
2019-08-02 02:48:53 +01:00
|
|
|
socket = if poolOpts.listen == "" then "${runtimeDir}/${name}.sock" else poolOpts.listen;
|
2019-08-03 01:56:13 +01:00
|
|
|
group = mkDefault poolOpts.user;
|
2019-07-03 22:34:17 +01:00
|
|
|
};
|
2019-07-03 17:18:27 +01:00
|
|
|
};
|
|
|
|
|
2014-03-11 22:46:57 +00:00
|
|
|
in {
|
2019-06-29 02:47:43 +01:00
|
|
|
|
2014-03-11 22:46:57 +00:00
|
|
|
options = {
|
|
|
|
services.phpfpm = {
|
2019-06-29 02:47:43 +01:00
|
|
|
extraConfig = mkOption {
|
2014-03-12 10:45:31 +00:00
|
|
|
type = types.lines;
|
2014-03-11 22:46:57 +00:00
|
|
|
default = "";
|
|
|
|
description = ''
|
2019-06-29 02:47:43 +01:00
|
|
|
Extra configuration that should be put in the global section of
|
2016-09-27 02:20:22 +01:00
|
|
|
the PHP-FPM configuration file. Do not specify the options
|
2016-10-05 16:30:31 +01:00
|
|
|
<literal>error_log</literal> or
|
2019-06-29 02:47:43 +01:00
|
|
|
<literal>daemonize</literal> here, since they are generated by
|
|
|
|
NixOS.
|
2014-03-11 22:46:57 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-06-29 02:47:43 +01:00
|
|
|
phpPackage = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.php;
|
|
|
|
defaultText = "pkgs.php";
|
|
|
|
description = ''
|
|
|
|
The PHP package to use for running the PHP-FPM service.
|
|
|
|
'';
|
|
|
|
};
|
2016-06-19 22:42:26 +01:00
|
|
|
|
2019-06-29 02:47:43 +01:00
|
|
|
phpOptions = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example =
|
|
|
|
''
|
|
|
|
date.timezone = "CET"
|
|
|
|
'';
|
|
|
|
description =
|
|
|
|
"Options appended to the PHP configuration file <filename>php.ini</filename>.";
|
|
|
|
};
|
|
|
|
|
|
|
|
pools = mkOption {
|
2019-07-03 17:18:27 +01:00
|
|
|
type = types.attrsOf (types.submodule poolOpts);
|
2019-06-29 02:47:43 +01:00
|
|
|
default = {};
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
mypool = {
|
2019-08-03 01:56:13 +01:00
|
|
|
user = "php";
|
|
|
|
group = "php";
|
2019-06-29 02:47:43 +01:00
|
|
|
phpPackage = pkgs.php;
|
|
|
|
extraConfig = '''
|
|
|
|
pm = dynamic
|
|
|
|
pm.max_children = 75
|
|
|
|
pm.start_servers = 10
|
|
|
|
pm.min_spare_servers = 5
|
|
|
|
pm.max_spare_servers = 20
|
|
|
|
pm.max_requests = 500
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
}'';
|
2016-06-19 22:42:26 +01:00
|
|
|
description = ''
|
2019-07-03 17:11:38 +01:00
|
|
|
PHP-FPM pools. If no pools are defined, the PHP-FPM
|
2016-09-27 02:20:22 +01:00
|
|
|
service is disabled.
|
2016-06-19 22:42:26 +01:00
|
|
|
'';
|
|
|
|
};
|
2014-03-11 22:46:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-07-03 17:11:38 +01:00
|
|
|
config = mkIf (cfg.pools != {}) {
|
2017-02-27 23:00:57 +00:00
|
|
|
|
2019-07-03 22:34:17 +01:00
|
|
|
warnings =
|
|
|
|
mapAttrsToList (pool: poolOpts: ''
|
|
|
|
Using config.services.phpfpm.pools.${pool}.listen is deprecated and will become unsupported. Please reference the read-only option config.services.phpfpm.pools.${pool}.socket to access the path of your socket.
|
|
|
|
'') (filterAttrs (pool: poolOpts: poolOpts.listen != "") cfg.pools)
|
|
|
|
;
|
|
|
|
|
2017-02-27 23:00:57 +00:00
|
|
|
systemd.slices.phpfpm = {
|
|
|
|
description = "PHP FastCGI Process manager pools slice";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.targets.phpfpm = {
|
|
|
|
description = "PHP FastCGI Process manager pools target";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
|
2019-07-03 17:11:38 +01:00
|
|
|
systemd.services = mapAttrs' (pool: poolOpts:
|
2017-01-25 22:21:33 +00:00
|
|
|
nameValuePair "phpfpm-${pool}" {
|
2017-02-27 23:00:57 +00:00
|
|
|
description = "PHP FastCGI Process Manager service for pool ${pool}";
|
2017-01-25 22:21:33 +00:00
|
|
|
after = [ "network.target" ];
|
2017-02-27 23:00:57 +00:00
|
|
|
wantedBy = [ "phpfpm.target" ];
|
|
|
|
partOf = [ "phpfpm.target" ];
|
2017-01-25 22:21:33 +00:00
|
|
|
serviceConfig = let
|
2019-07-03 17:11:38 +01:00
|
|
|
cfgFile = fpmCfgFile pool poolOpts;
|
|
|
|
iniFile = phpIni poolOpts;
|
2017-01-25 22:21:33 +00:00
|
|
|
in {
|
2017-02-27 23:00:57 +00:00
|
|
|
Slice = "phpfpm.slice";
|
2017-01-25 22:21:33 +00:00
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
ProtectHome = true;
|
2017-08-28 23:41:31 +01:00
|
|
|
# XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work
|
2019-06-29 02:47:43 +01:00
|
|
|
RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
|
2017-01-25 22:21:33 +00:00
|
|
|
Type = "notify";
|
2019-07-03 17:11:38 +01:00
|
|
|
ExecStart = "${poolOpts.phpPackage}/bin/php-fpm -y ${cfgFile} -c ${iniFile}";
|
2017-01-25 22:21:33 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
|
2019-08-02 02:48:53 +01:00
|
|
|
RuntimeDirectory = "phpfpm";
|
|
|
|
RuntimeDirectoryPreserve = true; # Relevant when multiple processes are running
|
2017-01-25 22:21:33 +00:00
|
|
|
};
|
|
|
|
}
|
2019-07-03 17:11:38 +01:00
|
|
|
) cfg.pools;
|
2014-03-11 22:46:57 +00:00
|
|
|
};
|
|
|
|
}
|