aboutsummaryrefslogtreecommitdiff
path: root/modules/apps/php-fpm.nix
blob: 4a0e6be3754d3d9e483c57d5c25ec19154135181 (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
{ config, pkgs, lib, ... }:

let

  inherit (builtins)
    filter isAttrs isBool ;
  inherit (lib)
    concatStringsSep filterAttrs foldl hasPrefix
    mapAttrsToList mkIf mkOption types ;
  inherit (types)
    attrsOf bool either enum int nullOr package path str
    submodule ;

  explicit = filterAttrs (n: v: n != "_module" && v != null);
  concatNonEmpty = sep: list: concatStringsSep sep (filter (s: s != "") list);

  attrs = opts: submodule { options = opts; };
  default = d: t: mkOption { type = t; default = d; };
  mandatory = t: mkOption { type = t; };
  optional = t: mkOption { type = nullOr t; default = null; };

  instances = explicit (config.nixsap.apps.php-fpm);

  users = mapAttrsToList (_: v: v.pool.user) instances;

  mkService = name: cfg:
    let
      show = v: if isBool v then (if v then "yes" else "no") else toString v;

      mkGroup = group: opts: main:
        let f = k: v: if k == main
                      then "${group} = ${show v}"
                      else "${group}.${k} = ${show v}";
        in concatNonEmpty "\n" (mapAttrsToList f (explicit opts));

      mkEnv = t: k: v: "${t}[${k}] = ${show v}";

      mkPool = k: v:
        if k == "listen" then mkGroup k v "socket"
        else if k == "env" || hasPrefix "php_" k then concatNonEmpty "\n" (mapAttrsToList (mkEnv k) v)
        else if k == "pm" then mkGroup k v "strategy"
        else if isAttrs v then mkGroup k v ""
        else "${k} = ${show v}";

      mkGlobal = k: v:
        if k == "php-ini" || k == "pool" || k == "package" then ""
        else if isAttrs v then mkGroup k v ""
        else "${k} = ${show v}";

      conf = pkgs.writeText "php-fpm-${name}.conf" ''
        [global]
        daemonize = no
        ${concatNonEmpty "\n" (mapAttrsToList mkGlobal (explicit cfg))}

        [pool]
        ${concatNonEmpty "\n" (mapAttrsToList mkPool (explicit cfg.pool))}
      '';
      exec = "${cfg.package}/bin/php-fpm --fpm-config ${conf} "
           + ( if cfg.php-ini != null
               then "--php-ini ${cfg.php-ini}"
               else "--no-php-ini" );
    in {
      "php-fpm-${name}" = {
        description = "PHP FastCGI Process Manager (${name})";
        after = [ "local-fs.target" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          ExecStart = exec;
          Restart = "always";
        };
      };
    };

in {

  options.nixsap.apps.php-fpm = default {}
    (attrsOf (submodule( { config, name, ... }: {
      options = {
        package = default pkgs.php package;
        emergency_restart_interval = optional int;
        emergency_restart_threshold = optional int;
        error_log = default "/var/log/php-fpm-${name}.log" path;
        log_level = optional (enum ["alert" "error" "warning" "notice" "debug"]);
        php-ini = optional path;
        process_control_timeout = optional int;
        rlimit_core = optional int;
        rlimit_files = optional int;

        process = optional (attrs {
          max      = optional int;
          priority = optional int;
        });

        pool = default {} (submodule({
          options = {
            catch_workers_output      = optional bool;
            chdir                     = optional path;
            clear_env                 = optional bool;
            env                       = default {} (attrsOf str);
            php_admin_flag            = default {} (attrsOf bool);
            php_admin_value           = default {} (attrsOf (either str int));
            php_flag                  = default {} (attrsOf bool);
            php_value                 = default {} (attrsOf (either str int));
            request_terminate_timeout = optional int;
            rlimit_core               = optional int;
            rlimit_files              = optional int;
            user                      = default "php-fpm-${name}" str;
            listen = default {} (attrs {
              acl_groups = optional str;
              backlog    = optional int;
              group      = optional str;
              mode       = optional str;
              owner      = default config.pool.user str;
              socket     = default "/run/php-fpm-${name}.sock" path;
            });
            pm = mandatory (attrs {
              max_children      = mandatory int;
              max_requests      = optional int;
              max_spare_servers = optional int;
              min_spare_servers = optional int;
              start_servers     = optional int;
              status_path       = optional path;
              strategy          = mandatory (enum ["static" "ondemand" "dynamic"]);
            });
            ping = optional (attrs {
              path     = optional path;
              response = optional str;
            });
          };
        }));
      };
    })));

  config = mkIf ({} != instances) {
    nixsap.system.users.daemons = users;
    systemd.services = foldl (a: b: a//b) {} (mapAttrsToList mkService instances);
  };
}