This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / 2configs / mailserver.nix
7.6 kB 263 lines
1{ 2 self, 3 config, 4 lib, 5 pkgs, 6 ... 7}: 8let 9 relayHosts = map (host: host.nets.retiolum.ip6.addr) [ 10 config.krebs.hosts.aergia 11 config.krebs.hosts.ignavia 12 config.krebs.hosts.coaxmetal 13 config.krebs.hosts.green 14 config.krebs.hosts.mors 15 ]; 16 17 mynetworks = map (ip: "[${ip}]") relayHosts; 18in 19{ 20 imports = [ 21 self.inputs.nixos-mailserver.nixosModules.default 22 ]; 23 24 mailserver = { 25 enable = true; 26 fqdn = "mail.lassul.us"; 27 domains = [ "lassul.us" ]; 28 29 loginAccounts = { 30 "lass@lassul.us" = { 31 hashedPasswordFile = 32 config.clan.core.vars.generators.mailserver-lass.files."lass-mail-password-hash".path; 33 aliases = [ 34 "postmaster@lassul.us" 35 "root@lassul.us" 36 "abuse@lassul.us" 37 "hostmaster@lassul.us" 38 "webmaster@lassul.us" 39 "security@lassul.us" 40 "noc@lassul.us" 41 "mailer-daemon@lassul.us" 42 "nobody@lassul.us" 43 "lassulus@lassul.us" 44 ]; 45 catchAll = [ "lassul.us" ]; 46 }; 47 "bot@lassul.us" = { 48 hashedPasswordFile = 49 config.clan.core.vars.generators.mailserver-bot.files."bot-mail-password-hash".path; 50 }; 51 }; 52 53 # TLS via existing ACME cert 54 x509.useACMEHost = "mail.lassul.us"; 55 56 # DKIM — auto-managed by module 57 dkimSigning = true; 58 dkimSelector = "mail"; 59 60 # ManageSieve: lets mail clients manage sieve rules dynamically (port 4190) 61 enableManageSieve = true; 62 63 # Full-text search for IMAP clients 64 fullTextSearch = { 65 enable = true; 66 memoryLimit = 500; 67 }; 68 69 stateVersion = 3; 70 }; 71 72 # dovecot pre-start creates /var/vmail with 0700; widen for lass (virtualMail group) 73 # POSIX ACL default ensures new files inherit lass access (needed for muchsync hardlinks) 74 systemd.services.dovecot.serviceConfig.ExecStartPost = [ 75 "+${pkgs.coreutils}/bin/chmod 2770 /var/vmail" 76 "+${pkgs.acl}/bin/setfacl -R -m u:lass:rwX /var/vmail/lassul.us/lass/mail" 77 "+${pkgs.acl}/bin/setfacl -R -d -m u:lass:rwX /var/vmail/lassul.us/lass/mail" 78 ]; 79 80 # Password generation for mail accounts 81 clan.core.vars.generators.mailserver-lass = { 82 files."lass-mail-password" = { }; 83 files."lass-mail-password-hash" = { }; 84 runtimeInputs = with pkgs; [ 85 coreutils 86 mkpasswd 87 pwgen 88 ]; 89 script = '' 90 pwgen -s 32 1 > "$out/lass-mail-password" 91 mkpasswd -sm bcrypt < "$out/lass-mail-password" > "$out/lass-mail-password-hash" 92 ''; 93 }; 94 95 clan.core.vars.generators.mailserver-bot = { 96 files."bot-mail-password" = { }; 97 files."bot-mail-password-hash" = { }; 98 files."mail.env" = { }; 99 runtimeInputs = with pkgs; [ 100 coreutils 101 mkpasswd 102 pwgen 103 ]; 104 script = '' 105 pwgen -s 32 1 > "$out/bot-mail-password" 106 mkpasswd -sm bcrypt < "$out/bot-mail-password" > "$out/bot-mail-password-hash" 107 cat > "$out/mail.env" << EOF 108 IMAP_USER=bot@lassul.us 109 IMAP_PASSWORD=$(cat "$out/bot-mail-password") 110 IMAP_HOST=localhost 111 EOF 112 ''; 113 }; 114 115 # Retiolum relay: allow krebs hosts to relay through postfix 116 services.postfix.settings.main.mynetworks = lib.mkForce ( 117 [ 118 "127.0.0.0/8" 119 "[::1]/128" 120 ] 121 ++ mynetworks 122 ); 123 124 # ACME cert — reuse existing setup 125 security.acme.certs."mail.lassul.us" = { 126 group = "lasscert"; 127 webroot = "/var/lib/acme/acme-challenge"; 128 }; 129 users.groups.lasscert.members = [ 130 "dovecot2" 131 "postfix" 132 "nginx" 133 ]; 134 135 # Firewall: SMTP(25), submission(587), submissions(465), IMAPS(993), ManageSieve(4190) 136 networking.firewall.allowedTCPPorts = [ 137 25 138 587 139 465 140 993 141 4190 142 ]; 143 144 # muchsync: lass user needs access to vmail for notmuch/muchsync 145 users.groups.virtualMail.members = [ "lass" ]; 146 systemd.tmpfiles.rules = [ 147 "L+ /home/lass/Maildir - - - - /var/vmail/lassul.us/lass/mail" 148 "z /var/vmail/lassul.us 2770 virtualMail virtualMail -" 149 "z /var/vmail/lassul.us/lass 2770 virtualMail virtualMail -" 150 "d /var/vmail/lassul.us/lass/mail 2770 virtualMail virtualMail -" 151 ]; 152 153 # Thunderbird autoconfig 154 services.nginx.virtualHosts."autoconfig.lassul.us" = { 155 forceSSL = true; 156 enableACME = true; 157 locations."= /mail/config-v1.1.xml".extraConfig = '' 158 default_type application/xml; 159 return 200 '<?xml version="1.0" encoding="UTF-8"?> 160 <clientConfig version="1.1"> 161 <emailProvider id="lassul.us"> 162 <domain>lassul.us</domain> 163 <displayName>lassul.us Mail</displayName> 164 <displayShortName>lassul.us</displayShortName> 165 <incomingServer type="imap"> 166 <hostname>mail.lassul.us</hostname> 167 <port>993</port> 168 <socketType>SSL</socketType> 169 <authentication>password-cleartext</authentication> 170 <username>%EMAILADDRESS%</username> 171 </incomingServer> 172 <outgoingServer type="smtp"> 173 <hostname>mail.lassul.us</hostname> 174 <port>465</port> 175 <socketType>SSL</socketType> 176 <authentication>password-cleartext</authentication> 177 <username>%EMAILADDRESS%</username> 178 </outgoingServer> 179 </emailProvider> 180 </clientConfig>'; 181 ''; 182 }; 183 184 # Dovecot virtual mailbox + ACL-restricted shared namespace for bot 185 services.dovecot2.mailPlugins.globally.enable = [ 186 "virtual" 187 "acl" 188 ]; 189 services.dovecot2.extraConfig = 190 let 191 virtualDir = pkgs.linkFarm "dovecot-virtual" [ 192 { 193 name = "Unread/dovecot-virtual"; 194 path = pkgs.writeText "dovecot-virtual-unread" "*\n unseen\n"; 195 } 196 ]; 197 in 198 '' 199 plugin { 200 acl = vfile 201 acl_shared_dict = file:/var/vmail/shared-mailboxes.db 202 } 203 204 namespace shared { 205 type = shared 206 separator = . 207 prefix = shared.%%n. 208 location = maildir:/var/vmail/lassul.us/%%n/mail:LAYOUT=Maildir++ 209 subscriptions = no 210 list = children 211 } 212 213 namespace virtual { 214 prefix = virtual. 215 separator = . 216 location = virtual:${virtualDir}:INDEX=/var/vmail/virtual-indexes/%u 217 subscriptions = no 218 mailbox Unread { 219 auto = subscribe 220 } 221 } 222 ''; 223 224 # Write dovecot-acl files to grant bot read-only + flag access to lass's mail 225 systemd.services.dovecot-acl-sync = { 226 description = "Sync dovecot ACL files for bot shared access"; 227 after = [ "dovecot.service" ]; 228 serviceConfig = { 229 Type = "oneshot"; 230 User = "virtualMail"; 231 Group = "virtualMail"; 232 }; 233 # l=lookup, r=read, w=write-flags, s=write-seen (no insert/delete/expunge) 234 script = '' 235 acl_line="user=bot@lassul.us lrws" 236 # Only write ACL in mailbox root dirs, skip cur/new/tmp/notmuch subdirs 237 find /var/vmail/lassul.us/lass/mail -type d \ 238 -not -name cur -not -name new -not -name tmp \ 239 -not -path '*/.notmuch/*' -not -name .notmuch \ 240 -not -path '*/fts-flatcurve/*' -not -name fts-flatcurve \ 241 | while IFS= read -r dir; do 242 acl_file="$dir/dovecot-acl" 243 if [ ! -f "$acl_file" ] || [ "$(cat "$acl_file")" != "$acl_line" ]; then 244 echo "$acl_line" > "$acl_file" 245 fi 246 done 247 ''; 248 }; 249 systemd.timers.dovecot-acl-sync = { 250 wantedBy = [ "timers.target" ]; 251 timerConfig = { 252 OnBootSec = "1min"; 253 OnUnitActiveSec = "1h"; 254 }; 255 }; 256 257 # notmuch + muchsync + msmtp for CLI mail access 258 environment.systemPackages = [ 259 self.packages.${pkgs.system}.notmuch 260 pkgs.muchsync 261 pkgs.msmtp 262 ]; 263}