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 type = types.package;
40 default = pkgs.php;
41 defaultText = "pkgs.php";
42 description = ''
43 The PHP package to use for running the FPM service.
44 '';
45 };
46
47 phpIni = mkOption {
48 type = types.path;
49 default = "${cfg.phpPackage}/etc/php-recommended.ini";
50 description = "php.ini file to use.";
51 };
52
53 poolConfigs = mkOption {
54 type = types.attrsOf types.lines;
55 default = {};
56 example = literalExample ''
57 { mypool = '''
58 listen = /run/phpfpm/mypool
59 user = nobody
60 pm = dynamic
61 pm.max_children = 75
62 pm.start_servers = 10
63 pm.min_spare_servers = 5
64 pm.max_spare_servers = 20
65 pm.max_requests = 500
66 ''';
67 }
68 '';
69 description = ''
70 A mapping between PHP FPM pool names and their configurations.
71 See the documentation on <literal>php-fpm.conf</literal> for
72 details on configuration directives. If no pools are defined,
73 the phpfpm service is disabled.
74 '';
75 };
76 };
77 };
78
79 config = mkIf (cfg.poolConfigs != {}) {
80
81 systemd.services.phpfpm = {
82 wantedBy = [ "multi-user.target" ];
83 preStart = ''
84 mkdir -p "${stateDir}"
85 '';
86 serviceConfig = {
87 ExecStart = "${cfg.phpPackage}/sbin/php-fpm -y ${cfgFile} -c ${cfg.phpIni}";
88 PIDFile = pidFile;
89 };
90 };
91
92 };
93}