summaryrefslogtreecommitdiff
path: root/sys/web/php-fpm.nix
blob: 33efe1a0f4f87c4adc6e30f3a0c6480c285f2423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Based on <https://gist.github.com/aanderse/3344baef2c3b86c8a1e98e63bd9256ea>
# See also:
# - <https://albert.cx/20181125-use-separate-systemd-units-for-php-fpm-pools>
# - <https://freedesktop.org/wiki/Software/systemd/DaemonSocketActivation/>
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.services.php-fpm-isolated;

  configFile = {
    pool,
    poolOpts,
    runtimeDir,
    sockFile,
    pidFile,
  }: let
    config = {
      global = {
        daemonize = false;
        error_log = "syslog";
        pid = pidFile;
      };

      "${pool}" = let
        enforced = {
          inherit (poolOpts) user group;
          listen = sockFile;
        };

        defaults = {
          "pm" = "dynamic";
          "pm.max_children" = 16;
          "pm.min_spare_servers" = 1;
          "pm.max_spare_servers" = 4;
          "pm.start_servers" = 1;
          "catch_workers_output" = true;
          "php_admin_flag[log_errors]" = true;
          "env[PATH]" = makeBinPath [pkgs.php];
        };

        env =
          mapAttrs'
          (name: value: {
            name = "env[${name}]";
            value = "\"${escape ["\""] value}\"";
          })
          poolOpts.env;
      in
        defaults // poolOpts.config // env // enforced;
    };
  in
    (pkgs.formats.ini {}).generate "php-fpm-pool-${pool}.conf" config;
in {
  options.services.php-fpm-isolated.pools = mkOption {
    default = {};

    type = with types;
      attrsOf (submodule {
        options = {
          enable = mkEnableOption "PHP-FPM pool";

          user = mkOption {
            type = str;
          };

          group = mkOption {
            type = str;
          };

          unveil = mkOption {
            type = listOf (either package str);
          };

          env = mkOption {
            type = attrsOf str;
            default = {};
          };

          config = mkOption {
            type = attrsOf (oneOf [int str bool]);
            default = {};
          };
        };
      });
  };

  config.systemd = let
    php-fpm = "${pkgs.php}/bin/php-fpm";

    unitsFor = pool: poolOpts: let
      runtimeBase = "php-fpm-isolated/${pool}";
      runtimeDir = "/run/${runtimeBase}";
      pidFile = "${runtimeDir}/${pool}.pid";
      sockFile = "${runtimeDir}/${pool}.sock";
    in {
      name = "php-fpm-pool-${pool}";

      value.service = {
        description = "PHP-FPM process manager for pool '${pool}'";
        after = ["network.target"];

        confinement.enable = true;

        serviceConfig = {
          Type = "notify";
          ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
          PIDFile = pidFile;

          Environment = "FPM_SOCKETS=${sockFile}=3";

          ExecStart = let
            fpmConfig = configFile {
              inherit pool poolOpts runtimeDir sockFile pidFile;
            };
          in "${php-fpm} --nodaemonize --fpm-config ${fpmConfig} --pid ${pidFile}";

          PrivateTmp = true;
          PrivateNetwork = true;
          PrivateDevices = true;
          # XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work
          RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";

          User = poolOpts.user;
          Group = poolOpts.group;
          RuntimeDirectory = runtimeBase;

          BindReadOnlyPaths = let
            unveiled = map builtins.toString poolOpts.unveil;
          in
            ["/run/systemd/journal/socket"] ++ unveiled;
        };
      };

      value.socket = {
        description = "PHP-FPM socket for pool '${pool}'";
        listenStreams = [sockFile];

        socketConfig = {
          User = poolOpts.user;
          Group = poolOpts.group;
        };
      };
    };

    units = mapAttrs' unitsFor (filterAttrs (_: pool: pool.enable) cfg.pools);
  in {
    sockets = mapAttrs (_: unit: unit.socket) units;
    services = mapAttrs (_: unit: unit.service) units;
  };
}