2023-11-12 17:33:58 +00:00
|
|
|
{
|
2023-11-13 14:48:05 +00:00
|
|
|
flake,
|
|
|
|
config,
|
2023-11-12 17:33:58 +00:00
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
2023-11-13 14:48:05 +00:00
|
|
|
}: let
|
2023-11-16 18:29:47 +00:00
|
|
|
hzDomain = lib.concatStrings [ "hw" "dz" "z." "net" ];
|
2023-11-14 17:44:46 +00:00
|
|
|
dkimDNSb12fio = ''
|
2024-03-19 20:00:28 +00:00
|
|
|
default._domainkey IN TXT ( "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyla9hW3TvoXvZQxwzaJ4SZ9ict1HU3E6+FWlwNIgE6tIpTCyRJtiSIUDqB8TLTIBoxIs+QQBXZi+QUi3Agu6OSY2RiV0EwO8+oOOqOD9pERftc/aqe51cXuv4kPqwvpXEBwrXFWVM+VxivEubUJ7eKkFyXJpelv0LslXv/MmYbUyed6dF+reOGZCsvnbiRv74qdxbAL/25j62E8WrnxzJwhUtx/JhdBOjsHBvuw9hy6rZsVJL9eXayWyGRV6qmsLRzsRSBs+mDrgmKk4dugADd11+A03ics3i8hplRoWDkqnNKz1qy4f5TsV6v9283IANrAzRfHwX8EvNiFsBz+ZCQIDAQAB" ) ;
|
2023-11-13 14:48:05 +00:00
|
|
|
'';
|
|
|
|
in {
|
|
|
|
age.secrets."b12f.io-dkim-private-rsa" = {
|
|
|
|
file = "${flake.self}/secrets/b12f.io-dkim-private-rsa.age";
|
2023-11-14 17:44:46 +00:00
|
|
|
path = "/var/lib/maddy/dkim_keys/b12f.io_default.key";
|
2023-11-13 14:48:05 +00:00
|
|
|
mode = "400";
|
2023-11-14 17:44:46 +00:00
|
|
|
owner = "maddy";
|
2023-11-13 14:48:05 +00:00
|
|
|
};
|
|
|
|
|
2023-11-12 17:33:58 +00:00
|
|
|
age.secrets."mail@b12f.io-password" = {
|
|
|
|
file = "${flake.self}/secrets/mail@b12f.io-password.age";
|
|
|
|
mode = "400";
|
2023-11-13 14:48:05 +00:00
|
|
|
owner = "maddy";
|
2023-11-12 17:33:58 +00:00
|
|
|
};
|
|
|
|
|
2024-01-03 20:08:59 +00:00
|
|
|
users.users.maddy.extraGroups = [ "nginx" ];
|
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
security.acme.certs = {
|
|
|
|
"mail.b12f.io" = {
|
|
|
|
reloadServices = [ "maddy" ];
|
|
|
|
group = "maddy";
|
|
|
|
};
|
2024-01-03 20:08:59 +00:00
|
|
|
"b12f.io" = {
|
|
|
|
reloadServices = [ "maddy" ];
|
|
|
|
};
|
2023-11-14 17:44:46 +00:00
|
|
|
"mta-sts.b12f.io" = {};
|
2023-11-16 18:29:47 +00:00
|
|
|
"mta-sts.${hzDomain}" = {};
|
2023-11-14 17:44:46 +00:00
|
|
|
};
|
2023-11-13 14:48:05 +00:00
|
|
|
|
2023-11-16 18:29:47 +00:00
|
|
|
services.nginx.virtualHosts = builtins.foldl' (hosts: hostName: hosts // {
|
|
|
|
"mta-sts.${hostName}" = {
|
|
|
|
forceSSL = true;
|
|
|
|
useACMEHost = "mta-sts.${hostName}";
|
|
|
|
locations."/" = {
|
|
|
|
root = pkgs.runCommand "create-well-known-mta-sts" {} ''
|
|
|
|
mkdir -p "$out/.well-known"
|
|
|
|
echo "
|
|
|
|
version: STSv1
|
|
|
|
mode: enforce
|
|
|
|
max_age: 604800
|
|
|
|
mx: mail.${hostName}
|
|
|
|
" > "$out/.well-known/mta-sts.txt"
|
|
|
|
'';
|
|
|
|
tryFiles = "$uri $uri/ =404";
|
|
|
|
};
|
2023-11-14 17:44:46 +00:00
|
|
|
};
|
2023-11-16 18:29:47 +00:00
|
|
|
}) {} [ "b12f.io" hzDomain ];
|
2023-11-12 20:45:02 +00:00
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '/run/maddy' 0750 maddy maddy - -"
|
|
|
|
];
|
2023-11-12 20:45:02 +00:00
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
system.activationScripts.makeMaddyDKIMDNS = lib.stringAfter [ "var" ] ''
|
|
|
|
mkdir -p /var/lib/maddy/dkim_keys
|
2023-11-12 20:45:02 +00:00
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
echo '${dkimDNSb12fio}' >> /var/lib/maddy/dkim_keys/b12f.io_default.dns
|
|
|
|
'';
|
2023-11-13 14:48:05 +00:00
|
|
|
|
2024-01-03 20:08:59 +00:00
|
|
|
networking.firewall.allowedTCPPorts = [ 25 587 993 ];
|
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
services.maddy = {
|
|
|
|
enable = true;
|
2024-01-03 20:08:59 +00:00
|
|
|
openFirewall = false;
|
2023-11-14 17:44:46 +00:00
|
|
|
hostname = "mail.b12f.io";
|
2023-11-12 17:33:58 +00:00
|
|
|
primaryDomain = "b12f.io";
|
|
|
|
ensureAccounts = [
|
|
|
|
"mail@b12f.io"
|
|
|
|
];
|
|
|
|
ensureCredentials = {
|
|
|
|
# Do not use this in production. This will make passwords world-readable
|
|
|
|
# in the Nix store
|
2023-11-13 14:48:05 +00:00
|
|
|
"mail@b12f.io".passwordFile = config.age.secrets."mail@b12f.io-password".path;
|
2023-11-12 17:33:58 +00:00
|
|
|
};
|
|
|
|
tls = {
|
2023-11-14 17:44:46 +00:00
|
|
|
loader = "file";
|
2023-11-12 17:33:58 +00:00
|
|
|
certificates = [
|
|
|
|
{
|
2023-11-14 17:44:46 +00:00
|
|
|
keyPath = "${config.security.acme.certs."mail.b12f.io".directory}/key.pem";
|
|
|
|
certPath = "${config.security.acme.certs."mail.b12f.io".directory}/cert.pem";
|
2023-11-12 17:33:58 +00:00
|
|
|
}
|
2024-01-03 20:08:59 +00:00
|
|
|
{
|
|
|
|
keyPath = "${config.security.acme.certs."b12f.io".directory}/key.pem";
|
|
|
|
certPath = "${config.security.acme.certs."b12f.io".directory}/cert.pem";
|
|
|
|
}
|
2023-11-12 17:33:58 +00:00
|
|
|
];
|
|
|
|
};
|
2023-11-14 17:44:46 +00:00
|
|
|
config = ''
|
|
|
|
# Minimal configuration with TLS disabled, adapted from upstream example
|
|
|
|
# configuration here https://github.com/foxcpp/maddy/blob/master/maddy.conf
|
|
|
|
# Do not use this in production!
|
2023-11-13 14:48:05 +00:00
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
auth.pass_table local_authdb {
|
|
|
|
table sql_table {
|
|
|
|
driver sqlite3
|
|
|
|
dsn credentials.db
|
|
|
|
table_name passwords
|
|
|
|
}
|
2023-11-13 14:48:05 +00:00
|
|
|
}
|
2023-11-14 17:44:46 +00:00
|
|
|
|
|
|
|
storage.imapsql local_mailboxes {
|
|
|
|
driver sqlite3
|
|
|
|
dsn imapsql.db
|
2023-11-13 14:48:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:44:46 +00:00
|
|
|
table.chain local_rewrites {
|
|
|
|
optional_step regexp "(.+)\+(.+)@(.+)" "$1@$3"
|
|
|
|
optional_step static {
|
|
|
|
entry postmaster postmaster@$(primary_domain)
|
|
|
|
}
|
|
|
|
optional_step file /etc/maddy/aliases
|
|
|
|
}
|
|
|
|
|
|
|
|
msgpipeline local_routing {
|
|
|
|
check {
|
|
|
|
rspamd {
|
|
|
|
api_path http://localhost:11334
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:29:47 +00:00
|
|
|
modify {
|
|
|
|
replace_rcpt &local_rewrites
|
|
|
|
}
|
|
|
|
|
|
|
|
# at this point rcpt was normalized to either:
|
|
|
|
# postmaster@$(primary_domain),
|
|
|
|
# local_mailbox_without_tag@$(local_domains),
|
|
|
|
# replacements with alias
|
|
|
|
|
|
|
|
# destination_in block takes priority over destinations
|
|
|
|
destination_in &local_mailboxes {
|
|
|
|
deliver_to &local_mailboxes
|
2023-11-14 17:44:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 18:29:47 +00:00
|
|
|
# if rcpt is not in local_mailboxes, but has our domains,
|
|
|
|
# replace rcpt to catchall and deliver it there
|
|
|
|
destination $(local_domains) {
|
2023-11-14 17:44:46 +00:00
|
|
|
modify {
|
2023-11-16 18:29:47 +00:00
|
|
|
replace_rcpt regexp ".*" "mail@$(primary_domain)"
|
2023-11-14 17:44:46 +00:00
|
|
|
}
|
|
|
|
deliver_to &local_mailboxes
|
|
|
|
}
|
|
|
|
|
|
|
|
default_destination {
|
|
|
|
reject 550 5.1.1 "User doesn't exist"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
smtp tcp://0.0.0.0:25 {
|
|
|
|
limits {
|
|
|
|
all rate 20 1s
|
|
|
|
all concurrency 10
|
|
|
|
}
|
|
|
|
dmarc yes
|
|
|
|
check {
|
|
|
|
require_mx_record
|
|
|
|
dkim
|
|
|
|
spf
|
|
|
|
}
|
|
|
|
source $(local_domains) {
|
|
|
|
reject 501 5.1.8 "Use Submission for outgoing SMTP"
|
|
|
|
}
|
|
|
|
default_source {
|
|
|
|
destination postmaster $(local_domains) {
|
|
|
|
deliver_to &local_routing
|
|
|
|
}
|
|
|
|
default_destination {
|
|
|
|
reject 550 5.1.1 "User doesn't exist"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 20:08:59 +00:00
|
|
|
submission tls://0.0.0.0:587 {
|
2023-11-14 17:44:46 +00:00
|
|
|
limits {
|
|
|
|
all rate 50 1s
|
|
|
|
}
|
|
|
|
auth &local_authdb
|
|
|
|
source $(local_domains) {
|
|
|
|
check {
|
|
|
|
authorize_sender {
|
|
|
|
prepare_email &local_rewrites
|
|
|
|
user_to_email identity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destination postmaster $(local_domains) {
|
|
|
|
deliver_to &local_routing
|
|
|
|
}
|
|
|
|
default_destination {
|
|
|
|
modify {
|
|
|
|
dkim $(primary_domain) $(local_domains) default
|
|
|
|
}
|
|
|
|
deliver_to &remote_queue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default_source {
|
|
|
|
reject 501 5.1.8 "Non-local sender domain"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target.remote outbound_delivery {
|
|
|
|
limits {
|
|
|
|
destination rate 20 1s
|
|
|
|
destination concurrency 10
|
|
|
|
}
|
|
|
|
mx_auth {
|
|
|
|
dane
|
|
|
|
mtasts {
|
|
|
|
cache fs
|
|
|
|
fs_dir mtasts_cache/
|
|
|
|
}
|
|
|
|
local_policy {
|
|
|
|
min_tls_level encrypted
|
|
|
|
min_mx_level none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target.queue remote_queue {
|
|
|
|
target &outbound_delivery
|
|
|
|
autogenerated_msg_domain $(primary_domain)
|
|
|
|
bounce {
|
|
|
|
destination postmaster $(local_domains) {
|
|
|
|
deliver_to &local_routing
|
|
|
|
}
|
|
|
|
default_destination {
|
|
|
|
reject 550 5.0.0 "Refusing to send DSNs to non-local addresses"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 20:08:59 +00:00
|
|
|
imap tls://0.0.0.0:993 {
|
2023-11-14 17:44:46 +00:00
|
|
|
auth &local_authdb
|
|
|
|
storage &local_mailboxes
|
|
|
|
}
|
|
|
|
'';
|
2023-11-12 17:33:58 +00:00
|
|
|
};
|
2023-11-13 14:48:05 +00:00
|
|
|
|
|
|
|
services.rspamd = {
|
|
|
|
enable = true;
|
|
|
|
locals."dkim_signing.conf".text = ''
|
|
|
|
enabled = false;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.rspamd.serviceConfig.SupplementaryGroups = [ "maddy" ];
|
2023-11-12 17:33:58 +00:00
|
|
|
}
|