nixos-config/hosts/falkenstein/modules/mail/default.nix

272 lines
7.3 KiB
Nix
Raw Normal View History

2024-02-11 19:57:42 +01:00
{ config, pkgs, lib, ... }:
2023-07-05 15:55:17 +02:00
let
2023-09-19 13:47:08 +02:00
domain = config.networking.domain;
hostname = "mail.${domain}";
2023-09-01 14:15:17 +02:00
# see https://www.kuketz-blog.de/e-mail-anbieter-ip-stripping-aus-datenschutzgruenden/
header_cleanup = pkgs.writeText "header_cleanup_outgoing" ''
/^\s*(Received: from)[^\n]*(.*)/ REPLACE $1 127.0.0.1 (localhost [127.0.0.1])$2
/^\s*User-Agent/ IGNORE
/^\s*X-Enigmail/ IGNORE
/^\s*X-Mailer/ IGNORE
/^\s*X-Originating-IP/ IGNORE
/^\s*Mime-Version/ IGNORE
'';
login_maps = pkgs.writeText "login_maps.pcre" ''
# basic username => username@rfive.de
/^([^@+]*)(\+[^@]*)?@rfive\.de$/ ''${1}
'';
2023-07-05 15:55:17 +02:00
in
2023-05-24 15:51:58 +02:00
{
2023-07-05 15:55:17 +02:00
networking.firewall.allowedTCPPorts = [
2023-11-25 23:22:16 +01:00
25 # SMTP
465 # SUBMISSONS
993 # IMAPS
2023-07-05 15:55:17 +02:00
4190 # sieve
];
users.users.rouven = {
description = "Rouven Seifert";
isNormalUser = true;
};
services = {
postfix = {
enable = true;
enableSubmission = true;
enableSubmissions = true;
2024-02-17 19:14:03 +01:00
recipientDelimiter = "+";
2023-07-05 15:55:17 +02:00
hostname = "${hostname}";
domain = "${domain}";
origin = "${domain}";
destination = [ "${hostname}" "${domain}" "localhost" ];
2024-01-04 11:30:19 +01:00
networks = [ "127.0.0.1" ];
2023-07-05 15:55:17 +02:00
sslCert = "/var/lib/acme/${hostname}/fullchain.pem";
sslKey = "/var/lib/acme/${hostname}/key.pem";
config = {
home_mailbox = "Maildir/";
2024-03-09 12:40:02 +01:00
smtp_helo_name = config.networking.fqdn;
smtpd_banner = "${config.networking.fqdn} ESMTP $mail_name";
2023-07-05 15:55:17 +02:00
smtp_use_tls = true;
smtpd_use_tls = true;
smtpd_tls_protocols = [
"!SSLv2"
"!SSLv3"
"!TLSv1"
"!TLSv1.1"
];
smtpd_recipient_restrictions = [
"permit_sasl_authenticated"
"permit_mynetworks"
"reject_unauth_destination"
"reject_non_fqdn_sender"
"reject_non_fqdn_recipient"
"reject_unknown_sender_domain"
"reject_unknown_recipient_domain"
"reject_unauth_destination"
"reject_unauth_pipelining"
"reject_invalid_hostname"
];
smtpd_relay_restrictions = [
"permit_sasl_authenticated"
"permit_mynetworks"
"reject_unauth_destination"
];
smtpd_sender_restrictions = [
"reject_authenticated_sender_login_mismatch"
];
smtpd_sender_login_maps = [ "pcre:${login_maps}" ];
2023-09-01 14:15:17 +02:00
smtp_header_checks = "pcre:${header_cleanup}";
2023-09-19 13:47:08 +02:00
2023-07-05 15:55:17 +02:00
alias_maps = [ "hash:/etc/aliases" ];
smtpd_sasl_auth_enable = true;
smtpd_sasl_path = "/var/lib/postfix/auth";
smtpd_sasl_type = "dovecot";
2023-07-09 22:49:16 +02:00
mailbox_transport = "lmtp:unix:/run/dovecot2/dovecot-lmtp";
2023-07-05 15:55:17 +02:00
};
2023-05-28 13:15:29 +02:00
};
2023-05-24 16:37:45 +02:00
2023-07-05 15:55:17 +02:00
dovecot2 = {
enable = true;
enableImap = true;
enableQuota = false;
enableLmtp = true;
mailLocation = "maildir:~/Maildir";
sslServerCert = "/var/lib/acme/${hostname}/fullchain.pem";
sslServerKey = "/var/lib/acme/${hostname}/key.pem";
protocols = [ "imap" "sieve" ];
mailPlugins = {
globally.enable = [ "listescape" ];
2023-07-05 15:55:17 +02:00
perProtocol = {
imap = {
enable = [ "imap_sieve" "imap_filter_sieve" ];
2023-07-05 15:55:17 +02:00
};
lmtp = {
enable = [ "sieve" ];
};
};
2023-05-24 16:37:45 +02:00
};
2023-07-05 15:55:17 +02:00
mailboxes = {
Spam = {
auto = "create";
specialUse = "Junk";
};
Sent = {
auto = "create";
specialUse = "Sent";
};
Drafts = {
auto = "create";
specialUse = "Drafts";
};
Trash = {
auto = "create";
specialUse = "Trash";
};
2024-02-26 19:14:36 +01:00
Archive = {
auto = "no";
specialUse = "Archive";
};
2023-07-05 15:55:17 +02:00
};
modules = [
pkgs.dovecot_pigeonhole
];
2024-02-11 19:57:42 +01:00
sieve = {
# just pot something in here to prevent empty strings
extensions = [ "notify" ];
# globalExtensions = [ "+vnd.dovecot.pipe" ];
pipeBins = map lib.getExe [
(pkgs.writeShellScriptBin "learn-ham.sh" "exec ${pkgs.rspamd}/bin/rspamc learn_ham")
(pkgs.writeShellScriptBin "learn-spam.sh" "exec ${pkgs.rspamd}/bin/rspamc learn_spam")
];
plugins = [
"sieve_imapsieve"
"sieve_extprograms"
];
};
imapsieve.mailbox = [
{
# Spam: From elsewhere to Spam folder or flag changed in Spam folder
name = "Spam";
causes = [ "COPY" "APPEND" "FLAG" ];
before = ./report-spam.sieve;
}
{
# From Junk folder to elsewhere
name = "*";
from = "Spam";
causes = [ "COPY" ];
before = ./report-ham.sieve;
}
];
2023-07-05 15:55:17 +02:00
extraConfig = ''
auth_username_format = %Ln
userdb {
driver = passwd
args = blocking=no
}
service auth {
unix_listener /var/lib/postfix/auth {
group = postfix
mode = 0660
user = postfix
}
}
service managesieve-login {
inet_listener sieve {
port = 4190
2023-07-05 15:55:17 +02:00
}
service_count = 1
}
namespace inbox {
separator = /
inbox = yes
}
service lmtp {
unix_listener dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
2023-07-05 15:55:17 +02:00
}
client_limit = 1
}
2023-07-05 15:55:17 +02:00
'';
2023-05-24 16:37:45 +02:00
};
2023-07-05 15:55:17 +02:00
rspamd = {
enable = true;
postfix.enable = true;
locals = {
2023-07-20 21:35:12 +02:00
"worker-controller.inc".text = ''
password = "$2$g1jh7t5cxschj11set5wksd656ixd5ie$cgwrj53hfb87xndqbh5r3ow9qfi1ejii8dxok1ihbnhamccn1rxy";
'';
2023-07-05 15:55:17 +02:00
"redis.conf".text = ''
2024-03-04 20:04:18 +01:00
read_servers = "/run/redis-rspamd/redis.sock";
write_servers = "/run/redis-rspamd/redis.sock";
2023-07-05 15:55:17 +02:00
'';
2023-11-16 01:27:01 +01:00
"milter_headers.conf".text = ''
use = ["x-spam-level", "x-spam-status", "x-spamd-result", "authentication-results" ];
'';
2023-09-12 11:09:32 +02:00
"dmarc.conf".text = ''
reporting {
2024-03-04 20:04:18 +01:00
enabled = true;
email = 'reports@${config.networking.domain}';
domain = '${config.networking.domain}';
org_name = '${config.networking.domain}';
2023-09-12 11:09:32 +02:00
from_name = 'DMARC Aggregate Report';
}
'';
2023-11-16 01:27:01 +01:00
"dkim_signing.conf".text = ''
selector = "rspamd";
allow_username_mismatch = true;
2024-03-04 20:04:18 +01:00
path = /var/lib/rspamd/dkim/$domain.key;
2023-11-16 01:27:01 +01:00
'';
2023-07-05 15:55:17 +02:00
};
};
redis = {
vmOverCommit = true;
servers.rspamd = {
enable = true;
};
};
};
security.acme.certs."${domain}" = {
reloadServices = [
"postfix.service"
"dovecot2.service"
];
2023-05-24 15:51:58 +02:00
};
2023-07-05 15:55:17 +02:00
services.nginx.virtualHosts = {
"${hostname}" = {
enableACME = true;
forceSSL = true;
};
2023-09-19 13:47:08 +02:00
"rspamd.${config.networking.domain}" = {
2023-07-05 15:55:17 +02:00
enableACME = true;
forceSSL = true;
locations = {
"/" = {
proxyPass = "http://127.0.0.1:11334";
proxyWebsockets = true;
};
2023-05-24 17:08:44 +02:00
};
};
};
2024-03-04 20:04:18 +01:00
users.users.rspamd.extraGroups = [ "redis-rspamd" ];
2023-10-26 12:12:45 +02:00
systemd = {
services.rspamd-dmarc-report = {
description = "rspamd dmarc reporter";
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.rspamd}/bin/rspamadm dmarc_report -v";
User = "rspamd";
Group = "rspamd";
};
2024-03-04 20:04:18 +01:00
startAt = "daily";
2023-10-26 12:12:45 +02:00
};
};
2023-05-24 15:51:58 +02:00
}