-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.nix
260 lines (253 loc) · 8.69 KB
/
configuration.nix
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
self:
{ config, pkgs, lib, options, ... }:
with lib;
let
cfg = config.services.nyanpasswd;
in {
imports = [
./autoconfig.nix
(mkRemovedOptionModule [ "services" "nyanpasswd" "dovecot2" "mailLocation" ] ''
Use `services.nyanpasswd.dovecot2.mailhome` instead. Additionally, run the following command to migrate old mailboxes:
```
mkdir $mailhome
for maildir in $mailLocation/*; do
mkdir -p "$mailhome/$(basename "$maildir")"
mv -T "$maildir" "$mailhome/$(basename "$maildir")/Maildir"
end
```
'')
(mkRenamedOptionModule ["services" "nyantec-mail-passwd"] ["services" "nyanpasswd"])
];
options = {
services.nyanpasswd = {
enable = mkEnableOption "nyanpasswd, the password management solution for our mail server";
databaseUri = mkOption {
type = types.nullOr types.str;
default = null;
example = "postgres://localhost?dbname=mail";
description = mdDoc ''
The database connection string to be used. If `null`,
postgres is automatically configured.
Note: MySQL is not supported due to quirks in the database
queries. Postgres is heavily recommended.
'';
};
domain = mkOption {
type = types.str;
default = "localhost";
description = mdDoc ''
nginx vhost on which to deploy the service.
You can add additional configuration to it later. TLS is
mandatory. TLS client certificate validation is
automatically configured.
'';
};
rootCACertificate = mkOption {
type = types.either types.str types.path;
example = "/var/lib/nyantec-crl/nyantec_Root_CA.pem";
description = mdDoc ''
TLS root CA certificate against which client certificates
are validated.
'';
};
crlFile = mkOption {
type = types.either types.str types.path;
example = "/var/lib/nyantec-crl/nyantec-combined.pem";
description = mdDoc ''
A certificate revocation list against which certificates are
validated.
'';
};
adminUids = mkOption {
type = types.listOf types.str;
default = [];
example = ["mvs" "mak" "vsh"];
description = mdDoc ''
A list of UIDs that will be granted access to the
administrative functions.
'';
};
user = mkOption {
type = types.nullOr types.str;
example = "mailpasswd";
default = null;
description = mdDoc ''
The user ID under which mail-passwd will be running. Leave as `null`
to autoconfigure.
'';
};
dovecot2.enable = mkEnableOption "integration with Dovecot";
dovecot2.mailhome = mkOption {
type = types.str;
default = "/var/vmail";
example = "/persist/vmail";
description = mdDoc ''
The location to store mail data of virtual users managed by nyanpasswd in.
'';
};
postfix = {
enable = mkEnableOption "integration with Postfix";
};
radicale = {
enable = mkEnableOption "integration with Radicale";
};
};
};
config = lib.mkMerge [
(lib.mkIf cfg.enable {
services.nginx.enable = true;
services.nginx.virtualHosts."${cfg.domain}" = {
forceSSL = true;
locations."/" = {
proxyPass = "http://localhost:3000/";
extraConfig = ''
proxy_set_header X-SSL-Verify $ssl_client_verify;
proxy_set_header X-SSL-Client-Dn $ssl_client_s_dn;
'';
};
locations."/api" = {
extraConfig = ''
return 403;
'';
};
extraConfig = ''
ssl_verify_client on;
ssl_client_certificate ${cfg.rootCACertificate};
ssl_crl ${cfg.crlFile};
'';
};
nixpkgs.overlays = [(final: prev: {
mail-passwd = lib.warn "mail-passwd was renamed to nyanpasswd." final.nyanpasswd;
nyanpasswd = self.packages.${config.nixpkgs.localSystem.system}.default;
})];
systemd.services.nyanpasswd = {
after = [ "network-online.target" ];
serviceConfig = {
ExecStart = "${pkgs.nyanpasswd}/bin/nyanpasswd";
User = lib.mkIf (cfg.user != null) cfg.user;
};
environment = {
DATABASE_URL = if (cfg.databaseUri == null)
then
"postgres://localhost?dbname=mailpasswd&host=/run/postgresql"
else
cfg.databaseUri;
ADMIN_UIDS = lib.concatStringsSep " " cfg.adminUids;
};
};
})
(lib.mkIf (cfg.enable && cfg.user == null) {
users.users.mailpasswd = {
isSystemUser = true;
group = "mailpasswd";
};
users.groups.mailpasswd = {};
systemd.services.nyanpasswd = {
serviceConfig.User = "mailpasswd";
};
})
(lib.mkIf (cfg.enable && cfg.dovecot2.enable) {
assertions = [
{
assertion = let
getLastChar = s: lib.strings.substring
((lib.strings.stringLength s)-1)
((lib.strings.stringLength s)-1)
s;
endsWith = c: s: (getLastChar s) == c;
in !(endsWith "/" cfg.dovecot2.mailhome);
message = "services.nyanpasswd.dovecot2.mailhome must not end with a slash";
}
];
systemd.services.dovecot2 = {
wants = [ "nyanpasswd.service" ];
};
services.dovecot2.extraConfig = let
makeLuaPath = subDir: paths: concatStringsSep ";" (map (path: path + "/" + subDir) (filter (x: x != null) paths));
packages = with pkgs.lua53Packages; [
rapidjson
];
luaPath = (makeLuaPath "lib/lua/5.3/?.lua" packages);
luaCPath = (makeLuaPath "lib/lua/5.3/?.so" packages);
userdb = pkgs.substituteAll {
src = ./userdb.lua;
lua_path = luaPath;
lua_cpath = luaCPath;
mailhome = cfg.dovecot2.mailhome;
};
in ''
userdb {
driver = lua
args = file=${userdb}
}
passdb {
driver = lua
args = file=${userdb}
}
'';
# they removed `services.dovecot2.package`...
nixpkgs.overlays = [(final: prev: {
dovecot = prev.dovecot.override { withLua = true; };
})];
systemd.tmpfiles.rules = [
"d ${cfg.dovecot2.mailhome} 0770 ${config.services.dovecot2.mailUser} ${config.services.dovecot2.mailGroup} -"
];
})
(lib.mkIf (cfg.enable && cfg.databaseUri == null) {
systemd.services.nyanpasswd = {
wants = [ "postgresql.service" ];
after = [ "postgresql.service" ];
};
services.postgresql = {
enable = true;
initialScript = pkgs.writeText "initial-script.sql" ''
CREATE DATABASE mailpasswd TEMPLATE template0 ENCODING 'utf8' LOCALE 'C';
\connect mailpasswd
CREATE SCHEMA IF NOT EXISTS mailpasswd;
CREATE USER mailpasswd;
ALTER SCHEMA mailpasswd OWNER TO mailpasswd;
GRANT ALL PRIVILEGES ON DATABASE mailpasswd TO mailpasswd;
${lib.optionalString cfg.postfix.enable ''
CREATE USER postfix;
GRANT CONNECT ON DATABASE mailpasswd TO postfix;
GRANT USAGE ON SCHEMA mailpasswd TO postfix;
SET ROLE mailpasswd;
ALTER DEFAULT PRIVILEGES FOR USER mailpasswd IN SCHEMA mailpasswd GRANT SELECT ON TABLES TO postfix;
RESET ROLE;
''}
'';
};
})
(lib.mkIf (cfg.enable && cfg.postfix.enable) {
nixpkgs.overlays = [
(final: prev: {
postfix = prev.postfix.override { withPgSQL = true; };
})
];
services.postfix.config = {
# Use nyanpasswd's database for virtual alias maps
virtual_alias_maps = "pgsql:${pkgs.writeText "postfix-nyanpasswd-aliases.cf" ''
hosts = postgresql:///mailpasswd?host=/run/postgresql
dbname = mailpasswd
query = SELECT userdb.username FROM mailpasswd.userdb INNER JOIN mailpasswd.aliases ON userdb.id = aliases.destination WHERE alias_name = '%u'
''}";
};
})
(lib.mkIf (cfg.enable && cfg.radicale.enable) {
services.radicale = {
enable = true;
package = pkgs.radicale.overrideAttrs (old: {
propagatedBuildInputs = old.propagatedBuildInputs ++ [
self.packages.${config.nixpkgs.localSystem.system}.radicale-plugin-nyanpasswd
];
});
settings = {
auth = {
type = "radicale_mail_passwd_auth";
mail_passwd_uri = "http://localhost:3000";
};
};
};
})
];
}