blob: 5fff838b26ddaad49d20f723b366147ecc72ebcd (
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
{
pkgs,
config,
lib,
...
}:
let
cfg = config.services.privatebin;
customToINI = lib.generators.toINI {
mkKeyValue = lib.generators.mkKeyValueDefault {
mkValueString =
v:
if v == true then
"true"
else if v == false then
"false"
else if builtins.isInt v then
"${toString v}"
else if builtins.isPath v then
''"${toString v}"''
else if builtins.isString v then
''"${v}"''
else
lib.generators.mkValueStringDefault { } v;
} "=";
};
privatebinSettings = pkgs.writeTextDir "conf.php" (customToINI cfg.settings);
user = cfg.user;
group = cfg.group;
defaultUser = "privatebin";
defaultGroup = "privatebin";
in
{
options.services.privatebin = {
enable = lib.mkEnableOption "Privatebin: A minimalist, open source online
pastebin where the server has zero knowledge of pasted data.";
user = lib.mkOption {
type = lib.types.str;
default = defaultUser;
description = "User account under which privatebin runs.";
};
group = lib.mkOption {
type = lib.types.str;
default = if cfg.enableNginx then "nginx" else defaultGroup;
defaultText = lib.literalExpression "if config.services.privatebin.enableNginx then \"nginx\" else \"${defaultGroup}\"";
description = ''
Group under which privatebin runs. It is best to set this to the group
of whatever webserver is being used as the frontend.
'';
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/privatebin";
description = ''
The place where privatebin stores its state.
'';
};
package = lib.mkPackageOption pkgs "privatebin" { };
enableNginx = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to enable nginx or not. If enabled, an nginx virtual host will
be created for access to privatebin. If not enabled, then you may use
`''${config.services.privatebin.package}` as your document root in
whichever webserver you wish to setup.
'';
};
virtualHost = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = ''
The hostname at which you wish privatebin to be served. If you have
enabled nginx using `services.privatebin.enableNginx` then this will
be used.
'';
};
poolConfig = lib.mkOption {
type = lib.types.attrsOf (
lib.types.oneOf [
lib.types.str
lib.types.int
lib.types.bool
]
);
defaultText = lib.literalExpression ''
{
"pm" = "dynamic";
"pm.max_children" = 32;
"pm.start_servers" = 2;
"pm.min_spare_servers" = 2;
"pm.max_spare_servers" = 4;
"pm.max_requests" = 500;
}
'';
default = { };
description = ''
Options for the PrivateBin PHP pool. See the documentation on <literal>php-fpm.conf</literal>
for details on configuration directives.
'';
};
settings = lib.mkOption {
default = { };
description = ''
Options for privatebin configuration. Refer to
<https://github.com/PrivateBin/PrivateBin/wiki/Configuration> for
details on supported values.
'';
example = lib.literalExpression ''
{
main = {
name = "NixOS Based Privatebin";
discussion = false;
defaultformatter = "plalib.types.intext";
qrcode = true
};
model.class = "Filesystem";
model_options.dir = "/var/lib/privatebin/data";
}
'';
type = lib.types.submodule { freeformType = lib.types.attrsOf lib.types.anything; };
};
};
config = lib.mkIf cfg.enable {
services.privatebin.settings = {
main = lib.mkDefault { };
model.class = lib.mkDefault "Filesystem";
model_options.dir = lib.mkDefault "${cfg.dataDir}/data";
purge.dir = lib.mkDefault "${cfg.dataDir}/purge";
traffic = {
dir = lib.mkDefault "${cfg.dataDir}/traffic";
header = "X_FORWARDED_FOR";
};
};
services.phpfpm.pools.privatebin = {
inherit user group;
phpPackage = pkgs.php83;
phpOptions = ''
log_errors = on
'';
settings = {
"listen.mode" = lib.mkDefault "0660";
"listen.owner" = lib.mkDefault user;
"listen.group" = lib.mkDefault group;
"pm" = lib.mkDefault "dynamic";
"pm.max_children" = lib.mkDefault 32;
"pm.start_servers" = lib.mkDefault 2;
"pm.min_spare_servers" = lib.mkDefault 2;
"pm.max_spare_servers" = lib.mkDefault 4;
"pm.max_requests" = lib.mkDefault 500;
};
phpEnv.CONFIG_PATH = lib.strings.removeSuffix "/conf.php" (toString privatebinSettings);
};
services.nginx = lib.mkIf cfg.enableNginx {
enable = true;
recommendedTlsSettings = lib.mkDefault true;
recommendedOptimisation = lib.mkDefault true;
recommendedGzipSettings = lib.mkDefault true;
virtualHosts.${cfg.virtualHost} = {
root = "${cfg.package}";
locations = {
"/" = {
tryFiles = "$uri $uri/ /index.php?$query_string";
index = "index.php";
extraConfig = ''
sendfile off;
'';
};
"~ \\.php$" = {
extraConfig = ''
include ${config.services.nginx.package}/conf/fastcgi_params ;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
fastcgi_pass unix:${config.services.phpfpm.pools.privatebin.socket};
'';
};
};
};
};
systemd.tmpfiles.settings."10-privatebin" =
lib.attrsets.genAttrs
[
"${cfg.dataDir}/data"
"${cfg.dataDir}/traffic"
"${cfg.dataDir}/purge"
]
(n: {
d = {
group = group;
mode = "0750";
user = user;
};
});
users = {
users = lib.mkIf (user == defaultUser) {
${defaultUser} = {
description = "Privatebin service user";
inherit group;
isSystemUser = true;
home = cfg.dataDir;
};
};
groups = lib.mkIf (group == defaultGroup) { ${defaultGroup} = { }; };
};
};
}
|