1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.phpfpm;
7
8 stateDir = "/run/phpfpm";
9
10 pidFile = "${stateDir}/phpfpm.pid";
11
12 cfgFile = pkgs.writeText "phpfpm.conf" ''
13 [global]
14 pid = ${pidFile}
15 error_log = syslog
16 daemonize = yes
17 ${cfg.extraConfig}
18
19 ${concatStringsSep "\n" (mapAttrsToList (n: v: "[${n}]\n${v}") cfg.poolConfigs)}
20 '';
21
22in {
23
24 options = {
25 services.phpfpm = {
26 extraConfig = mkOption {
27 type = types.lines;
28 default = "";
29 description = ''
30 Extra configuration that should be put in the global section of
31 the PHP FPM configuration file. Do not specify the options
32 <literal>pid</literal>, <literal>error_log</literal> or
33 <literal>daemonize</literal> here, since they are generated by
34 NixOS.
35 '';
36 };
37
38 phpPackage = mkOption {
39 default = pkgs.php;
40 description = ''
41 The PHP package to use for running the FPM service.
42 '';
43 };
44
45 phpIni = mkOption {
46 type = types.path;
47 default = "${cfg.phpPackage}/etc/php-recommended.ini";
48 description = "php.ini file to use.";
49 };
50
51 poolConfigs = mkOption {
52 type = types.attrsOf types.lines;
53 default = {};
54 example = {
55 mypool = ''
56 listen = /run/phpfpm/mypool
57 user = nobody
58 pm = dynamic
59 pm.max_children = 75
60 pm.start_servers = 10
61 pm.min_spare_servers = 5
62 pm.max_spare_servers = 20
63 pm.max_requests = 500
64 '';
65 };
66 description = ''
67 A mapping between PHP FPM pool names and their configurations.
68 See the documentation on <literal>php-fpm.conf</literal> for
69 details on configuration directives. If no pools are defined,
70 the phpfpm service is disabled.
71 '';
72 };
73 };
74 };
75
76 config = mkIf (cfg.poolConfigs != {}) {
77
78 systemd.services.phpfpm = {
79 wantedBy = [ "multi-user.target" ];
80 preStart = ''
81 mkdir -p "${stateDir}"
82 '';
83 serviceConfig = {
84 ExecStart = "${cfg.phpPackage}/sbin/php-fpm -y ${cfgFile} -c ${cfg.phpIni}";
85 PIDFile = pidFile;
86 };
87 };
88
89 };
90}