{ lib, pkgs, config, ... }: { config = lib.mkIf config.myServices.mail.enable { services.duplyBackup.profiles.mail.excludeFile = '' + /var/lib/postfix ''; secrets.keys = [ { dest = "postfix/mysql_alias_maps"; user = config.services.postfix.user; group = config.services.postfix.group; permissions = "0440"; text = '' # We need to specify that option to trigger ssl connection tls_ciphers = TLSv1.2 user = ${config.myEnv.mail.postfix.mysql.user} password = ${config.myEnv.mail.postfix.mysql.password} hosts = unix:${config.myEnv.mail.postfix.mysql.socket} dbname = ${config.myEnv.mail.postfix.mysql.database} query = SELECT DISTINCT destination FROM forwardings_merge WHERE ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) AND active = 1 AND '%s' NOT IN ( SELECT source FROM forwardings_blacklisted WHERE source = '%s' ) UNION SELECT 'devnull@immae.eu' FROM forwardings_blacklisted WHERE source = '%s' ''; } { dest = "postfix/mysql_mailbox_maps"; user = config.services.postfix.user; group = config.services.postfix.group; permissions = "0440"; text = '' # We need to specify that option to trigger ssl connection tls_ciphers = TLSv1.2 user = ${config.myEnv.mail.postfix.mysql.user} password = ${config.myEnv.mail.postfix.mysql.password} hosts = unix:${config.myEnv.mail.postfix.mysql.socket} dbname = ${config.myEnv.mail.postfix.mysql.database} result_format = /%d/%u query = SELECT DISTINCT '%s' FROM mailboxes WHERE active = 1 AND ( (domain = '%d' AND user = '%u' AND regex = 0) OR ( regex = 1 AND '%d' REGEXP CONCAT('^',domain,'$') AND '%u' REGEXP CONCAT('^',user,'$') ) ) LIMIT 1 ''; } { dest = "postfix/mysql_sender_login_maps"; user = config.services.postfix.user; group = config.services.postfix.group; permissions = "0440"; text = '' # We need to specify that option to trigger ssl connection tls_ciphers = TLSv1.2 user = ${config.myEnv.mail.postfix.mysql.user} password = ${config.myEnv.mail.postfix.mysql.password} hosts = unix:${config.myEnv.mail.postfix.mysql.socket} dbname = ${config.myEnv.mail.postfix.mysql.database} query = SELECT DISTINCT destination FROM forwardings_merge WHERE ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) AND active = 1 UNION SELECT '%s' AS destination ''; } { dest = "postfix/ldap_ejabberd_users_immae_fr"; user = config.services.postfix.user; group = config.services.postfix.group; permissions = "0440"; text = '' server_host = ldaps://${config.myEnv.jabber.ldap.host}:636 search_base = ${config.myEnv.jabber.ldap.base} query_filter = ${config.myEnv.jabber.postfix_user_filter} domain = immae.fr bind_dn = ${config.myEnv.jabber.ldap.dn} bind_pw = ${config.myEnv.jabber.ldap.password} result_attribute = immaeXmppUid result_format = ejabberd@localhost version = 3 ''; } ]; networking.firewall.allowedTCPPorts = [ 25 465 587 ]; nixpkgs.overlays = [ (self: super: { postfix = super.postfix.override { withMySQL = true; }; }) ]; users.users."${config.services.postfix.user}".extraGroups = [ "keys" ]; services.filesWatcher.postfix = { restart = true; paths = [ config.secrets.fullPaths."postfix/mysql_alias_maps" config.secrets.fullPaths."postfix/mysql_mailbox_maps" config.secrets.fullPaths."postfix/mysql_sender_login_maps" config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr" ]; }; services.postfix = { extraAliases = let toScript = name: script: pkgs.writeScript name '' #! ${pkgs.stdenv.shell} mail=$(${pkgs.coreutils}/bin/cat -) output=$(echo "$mail" | ${script} 2>&1) ret=$? if [ "$ret" != "0" ]; then echo "$mail" \ | ${pkgs.procmail}/bin/formail -i "X-Return-Code: $ret" \ | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu messageId=$(echo "$mail" | ${pkgs.procmail}/bin/formail -x "Message-Id:") repeat=$(echo "$mail" | ${pkgs.procmail}/bin/formail -X "From:" -X "Received:") ${pkgs.coreutils}/bin/cat <