]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/phpfpm/default.nix
Move kanboard passwords to secure location
[perso/Immae/Config/Nix.git] / nixops / modules / websites / phpfpm / default.nix
1 { config, lib, pkgs, ... }:
2
3 with lib;
4
5 let
6 cfg = config.services.myPhpfpm;
7 enabled = cfg.poolConfigs != {} || cfg.pools != {};
8
9 stateDir = "/run/phpfpm";
10
11 poolConfigs = cfg.poolConfigs // mapAttrs mkPool cfg.pools;
12
13 mkPool = n: p: ''
14 listen = ${p.listen}
15 ${p.extraConfig}
16 '';
17
18 fpmCfgFile = pool: poolConfig: pkgs.writeText "phpfpm-${pool}.conf" ''
19 [global]
20 error_log = syslog
21 daemonize = no
22 ${cfg.extraConfig}
23
24 [${pool}]
25 ${poolConfig}
26 '';
27
28 phpIni = poolPhpOptions: (pkgs.runCommand "php.ini" {
29 inherit (cfg) phpPackage phpOptions;
30 inherit poolPhpOptions;
31 nixDefaults = ''
32 sendmail_path = "/run/wrappers/bin/sendmail -t -i"
33 '';
34 passAsFile = [ "nixDefaults" "phpOptions" "poolPhpOptions" ];
35 } ''
36 cat $phpPackage/etc/php.ini $nixDefaultsPath $phpOptionsPath $poolPhpOptionsPath > $out
37 '');
38
39 in {
40
41 options = {
42 services.myPhpfpm = {
43 extraConfig = mkOption {
44 type = types.lines;
45 default = "";
46 description = ''
47 Extra configuration that should be put in the global section of
48 the PHP-FPM configuration file. Do not specify the options
49 <literal>error_log</literal> or
50 <literal>daemonize</literal> here, since they are generated by
51 NixOS.
52 '';
53 };
54
55 phpPackage = mkOption {
56 type = types.package;
57 default = pkgs.php;
58 defaultText = "pkgs.php";
59 description = ''
60 The PHP package to use for running the PHP-FPM service.
61 '';
62 };
63
64 phpOptions = mkOption {
65 type = types.lines;
66 default = "";
67 example =
68 ''
69 date.timezone = "CET"
70 '';
71 description =
72 "Options appended to the PHP configuration file <filename>php.ini</filename>.";
73 };
74
75 serviceDependencies = mkOption {
76 default = {};
77 type = types.attrsOf (types.listOf types.string);
78 example = literalExample ''
79 { mypool = ["postgresql.service"]; }
80 '';
81 description = ''
82 Extra service dependencies specific to pool.
83 '';
84 };
85
86 poolPhpConfigs = mkOption {
87 default = {};
88 type = types.attrsOf types.lines;
89 example = literalExample ''
90 { mypool = '''
91 extension = some_extension.so
92 ''';
93 }
94 '';
95 description = ''
96 Extra lines that go into the php configuration specific to pool.
97 '';
98 };
99
100 poolConfigs = mkOption {
101 default = {};
102 type = types.attrsOf types.lines;
103 example = literalExample ''
104 { mypool = '''
105 listen = /run/phpfpm/mypool
106 user = nobody
107 pm = dynamic
108 pm.max_children = 75
109 pm.start_servers = 10
110 pm.min_spare_servers = 5
111 pm.max_spare_servers = 20
112 pm.max_requests = 500
113 ''';
114 }
115 '';
116 description = ''
117 A mapping between PHP-FPM pool names and their configurations.
118 See the documentation on <literal>php-fpm.conf</literal> for
119 details on configuration directives. If no pools are defined,
120 the phpfpm service is disabled.
121 '';
122 };
123
124 pools = mkOption {
125 type = types.attrsOf (types.submodule (import ./pool-options.nix {
126 inherit lib;
127 }));
128 default = {};
129 example = literalExample ''
130 {
131 mypool = {
132 listen = "/path/to/unix/socket";
133 extraConfig = '''
134 user = nobody
135 pm = dynamic
136 pm.max_children = 75
137 pm.start_servers = 10
138 pm.min_spare_servers = 5
139 pm.max_spare_servers = 20
140 pm.max_requests = 500
141 ''';
142 }
143 }'';
144 description = ''
145 PHP-FPM pools. If no pools or poolConfigs are defined, the PHP-FPM
146 service is disabled.
147 '';
148 };
149 };
150 };
151
152 config = mkIf enabled {
153
154 systemd.slices.phpfpm = {
155 description = "PHP FastCGI Process manager pools slice";
156 };
157
158 systemd.targets.phpfpm = {
159 description = "PHP FastCGI Process manager pools target";
160 wantedBy = [ "multi-user.target" ];
161 };
162
163 systemd.services = flip mapAttrs' poolConfigs (pool: poolConfig:
164 nameValuePair "phpfpm-${pool}" {
165 description = "PHP FastCGI Process Manager service for pool ${pool}";
166 after = [ "network.target" ] ++ (cfg.serviceDependencies.${pool} or []);
167 wants = cfg.serviceDependencies.${pool} or [];
168 wantedBy = [ "phpfpm.target" ];
169 partOf = [ "phpfpm.target" ];
170 preStart = ''
171 mkdir -p ${stateDir}
172 '';
173 serviceConfig = let
174 cfgFile = fpmCfgFile pool poolConfig;
175 poolPhpIni = cfg.poolPhpConfigs.${pool} or "";
176 in {
177 Slice = "phpfpm.slice";
178 PrivateDevices = true;
179 ProtectSystem = "full";
180 ProtectHome = true;
181 # XXX: We need AF_NETLINK to make the sendmail SUID binary from postfix work
182 RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_NETLINK";
183 Type = "notify";
184 ExecStart = "${cfg.phpPackage}/bin/php-fpm -y ${cfgFile} -c ${phpIni poolPhpIni}";
185 ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
186 };
187 }
188 );
189 };
190 }