{ lib, pkgs, config, ... }:
let
getDomains = p: lib.mapAttrsToList (n: v: v) (lib.filterAttrs (n: v: v.receive) p.emailPolicies);
bydomain = builtins.mapAttrs (n: getDomains) config.myServices.dns.zones;
domains = lib.flatten (builtins.attrValues bydomain);
mxes = lib.mapAttrsToList
(n: v: v.mx.subdomain)
(lib.attrsets.filterAttrs (n: v: v.mx.enable) config.myEnv.servers);
file = d: pkgs.writeText "mta-sts-${d.fqdn}.txt" (
builtins.concatStringsSep "\r\n" ([ "version: STSv1" "mode: testing" ]
++ (map (v: "mx: ${v}.${d.domain}") mxes)
++ [ "max_age: 604800" ]
));
root = pkgs.runCommand "mta-sts_root" {} ''
mkdir -p $out
${builtins.concatStringsSep "\n" (map (d:
"cp ${file d} $out/${d.fqdn}.txt"
) domains)}
'';
autoconfigRoot =
let autoconfig = pkgs.writeText "config-v1.1.xml" ''
mail.immae.eu
%EMAILDOMAIN%
Immae E-mail
Immae E-mail
imap.immae.eu
143
STARTTLS
%EMAILADDRESS%
password-cleartext
imap.immae.eu
993
SSL
%EMAILADDRESS%
password-cleartext
pop.immae.eu
110
STARTTLS
%EMAILADDRESS%
password-cleartext
pop.immae.eu
995
SSL
%EMAILADDRESS%
password-cleartext
smtp.immae.eu
587
STARTTLS
%EMAILADDRESS%
password-cleartext
smtp.immae.eu
465
SSL
%EMAILADDRESS%
password-cleartext
'';
autodiscover = pkgs.writeText "Autodiscover.xml" ''
Name
email
settings
IMAP
1
imap.immae.eu
143
on
off
off
on
POP3
1
pop3.immae.eu
110
on
off
off
on
SMTP
1
smtp.immae.eu
587
on
off
off
on
IMAP
1
imap.immae.eu
993
on
off
on
on
POP3
1
pop3.immae.eu
995
on
off
on
on
SMTP
1
smtp.immae.eu
465
on
off
on
on
'';
in
pkgs.runCommand "autoconfig" {} ''
mkdir -p $out/mail
ln -s ${autoconfig} $out/mail/config-v1.1.xml
mkdir -p $out/AutoDiscover
ln -s ${autodiscover} $out/AutoDiscover/AutoDiscover.xml
mkdir -p $out/Autodiscover
ln -s ${autodiscover} $out/Autodiscover/Autodiscover.xml
mkdir -p $out/autodiscover
ln -s ${autodiscover} $out/autodiscover/autodiscover.xml
'';
cfg = config.myServices.websites.tools.email;
in
{
config = lib.mkIf cfg.enable {
security.acme.certs.mail.extraDomainNames =
[ "mta-sts.mail.immae.eu" "autoconfig.mail.immae.eu" "autodiscover.mail.immae.eu" ]
++ map (v: "mta-sts.${v.fqdn}") domains
++ map (v: "autoconfig.${v.fqdn}") domains
++ map (v: "autodiscover.${v.fqdn}") domains;
services.websites.env.tools.vhostConfs.mta_sts = {
certName = "mail";
hosts = ["mta-sts.mail.immae.eu"] ++ map (v: "mta-sts.${v.fqdn}") domains;
root = root;
extraConfig = [
''
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mta-sts.(.*)$
RewriteRule ^/.well-known/mta-sts.txt$ %{DOCUMENT_ROOT}/%1.txt [L]
Require all granted
Options -Indexes
''
];
};
services.websites.env.tools.vhostConfs.mail_autoconfig = {
certName = "mail";
hosts = ["autoconfig.mail.immae.eu" "autodiscover.mail.immae.eu" ]
++ map (v: "autoconfig.${v.fqdn}") domains
++ map (v: "autodiscover.${v.fqdn}") domains;
root = autoconfigRoot;
extraConfig = [
''
Require all granted
Options -Indexes
''
];
};
};
}