blob: d3f60ab0ee965622dbc5b6391e04dff2efac12a2 (
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
|
{config, pkgs, lib, ...}:
let
cfg = config.wilkuu.services.stalwart;
hostname = config.networking.hostName;
in
{
options.wilkuu.services.stalwart = with lib; {
domain = mkOption {
type = types.str;
default = "mail.${hostname}.local";
example = "mail.wilkuu.xyz";
description = "Domain for http connections.";
};
wellKnownDomains = mkOption {
type = types.listOf types.str;
default = [ "${hostname}.local" ];
example = [ "wilkuu.xyz"] ;
description = "Domain for well-known items";
};
doACME = mkEnableOption "Enable ACME for stalwart here";
enable = mkEnableOption "Enable the email service";
dataDir = mkOption {
type = types.path;
description = "Storage localtion for Stalwart user data";
default = "/srv/data/stalwart";
example = "/srv/data/stalwart";
};
};
config = lib.mkIf cfg.enable (let
sopsPath = ../secrets/${hostname}/stalwart.yaml;
secrets = ["user_admin_password"];
toSops = (sname: "stalwart/${sname}");
toCredfilePath = (name: config.sops.secrets.${toSops name}.path);
toStalwartCred = name: "%{file:/run/credentials/stalwart-mail.service/${name}}%";
basicListener = proto: port: tls: {
bind = ["[::]:${toString port}"];
protocol = proto;
tls.implicit = tls;
};
proxyWellKnown = names: let
uris = map (n: "/.well-known/${n}") names;
in (lib.genAttrs uris (uri: {
proxyPass = "http://localhost:3080${uri}";
recommendedProxySettings = true;
}));
makeHttpRedirect = target: https: { return = "302 ${if https then "https" else "http"}://${target}";};
in {
networking.hosts = {
"127.0.0.1" = [cfg.domain];
};
# Need this bc otherwise sops will complain for some reason
users = {
groups.stalwart-mail = { };
users.stalwart-mail = {
isSystemUser = true;
group = "stalwart-mail";
};
};
# TODO: Move this into a util function or option;
sops.secrets =
(lib.genAttrs (map toSops secrets)
(name: {
sopsFile = sopsPath;
mode = "0440";
owner = "stalwart-mail";
}));
services.nginx.virtualHosts = (lib.genAttrs cfg.wellKnownDomains (
(wdomain: {
locations = (proxyWellKnown ["jmap" "mta-sts.txt" "mail-v1.xml" "autoconfig/mail"])
// (lib.genAttrs ["/.well_known/caldav" "/.well_known/webdav" ] (uri: (makeHttpRedirect "${cfg.domain}${uri}") cfg.doACME ));
})))
//
(lib.genAttrs (map (x: "${x}${cfg.domain}") ["" "autodiscover." "autoconfig."]) (domain: {
addSSL = cfg.doACME;
enableACME = cfg.doACME;
serverName = "${domain}";
locations."/" = {
proxyPass = "http://localhost:3080";
recommendedProxySettings = true;
};
}));
services.stalwart-mail = {
enable = true;
dataDir = cfg.dataDir;
openFirewall = false;
credentials = lib.genAttrs secrets toCredfilePath;
settings = {
server.listener = {
smtp = basicListener "smtp" 25 false;
submission = basicListener "smtp" 465 true;
imaptls = basicListener "imap" 993 true;
imap = basicListener "imap" 143 true;
webdav = basicListener "http" 3080 false;
jmap = basicListener "http" 3080 false;
http = basicListener "http" 3080 false;
};
store.rocksdb = {
type = "rocksdb";
path = cfg.dataDir;
compression = "lz4";
};
directory.internal = {
type = "internal";
store = "rocksdb";
};
storage = {
data = "rocksdb";
fts = "rocksdb";
blob = "rocksdb";
lookup = "rocksdb";
directory = "internal";
};
authentication.fallback-admin = {
user = "admin";
secret = toStalwartCred "user_admin_password";
};
http = {
use-x-forwarded = true;
url = "protocol + \"${cfg.domain}\"";
};
};
};
});
}
|