home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

mails.nix (7338B)


      1 # Generated from an org file 💃
      2 # See : https://sbr.pm/technical/configurations/mails.html
      3 { config, lib, pkgs, ... }:
      4 
      5 with lib;
      6 let
      7   cfg = config.profiles.mails;
      8 in
      9 {
     10 
     11   options = {
     12     profiles.mails = {
     13       enable = mkEnableOption "Enable mails configuration";
     14       sync = mkEnableOption "Enable sync mail service";
     15       frequency = mkOption {
     16         default = "*:0/30";
     17         description = "Frequency at which the mail should be checked";
     18         type = types.str;
     19       };
     20     };
     21   };
     22 
     23   config = mkIf cfg.enable (
     24     mkMerge [
     25       {
     26         accounts.email = {
     27           maildirBasePath = "desktop/mails";
     28           accounts = {
     29             "redhat" = {
     30               address = "vdemeest@redhat.com";
     31               userName = "vdemeest@redhat.com";
     32               realName = "Vincent Demeester";
     33               passwordCommand = "${pkgs.gnupg}/bin/gpg -q --for-your-eyes-only --no-tty --exit-on-status-write-error --batch --passphrase-file ${config.home.homeDirectory}/sync/rh.pass -d ${config.home.homeDirectory}/desktop/documents/rh.pass.gpg";
     34               imap.host = "imap.gmail.com";
     35               smtp.host = "smtp.gmail.com";
     36               mbsync = {
     37                 enable = true;
     38                 create = "both";
     39                 expunge = "both";
     40                 patterns = [ "*" "![Gmail]*" "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" ];
     41                 extraConfig = {
     42                   channel = {
     43                     Sync = "All";
     44                   };
     45                   account = {
     46                     Timeout = 120;
     47                     PipelineDepth = 1;
     48                   };
     49                 };
     50               };
     51               notmuch.enable = cfg.sync;
     52               astroid.enable = cfg.sync;
     53               msmtp.enable = true;
     54             };
     55             "perso" = {
     56               address = "vinc.demeester@gmail.com";
     57               userName = "vinc.demeester@gmail.com";
     58               realName = "Vincent Demeester";
     59               passwordCommand = "${pkgs.gnupg}/bin/gpg -q --for-your-eyes-only --no-tty --exit-on-status-write-error --batch --passphrase-file ${config.home.homeDirectory}/sync/perso.pass -d ${config.home.homeDirectory}/desktop/documents/perso.pass.gpg";
     60               imap.host = "imap.gmail.com";
     61               smtp.host = "smtp.gmail.com";
     62               mbsync = {
     63                 enable = true;
     64                 create = "both";
     65                 expunge = "both";
     66                 patterns = [ "*" "![Gmail]*" "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail" ];
     67                 extraConfig = {
     68                   channel = {
     69                     Sync = "All";
     70                   };
     71                   account = {
     72                     Timeout = 120;
     73                     PipelineDepth = 1;
     74                   };
     75                 };
     76               };
     77               notmuch.enable = cfg.sync;
     78               astroid.enable = cfg.sync;
     79               msmtp.enable = true;
     80             };
     81             "prv" = {
     82               primary = true;
     83               address = "vincent@demeester.fr";
     84               userName = "vincent@demeester.fr";
     85               realName = "Vincent Demeester";
     86               passwordCommand = "${pkgs.gnupg}/bin/gpg -q --for-your-eyes-only --no-tty --exit-on-status-write-error --batch --passphrase-file ${config.home.homeDirectory}/sync/prv.pass -d ${config.home.homeDirectory}/desktop/documents/prv.pass.gpg";
     87               imap.host = "mail.gandi.net";
     88               smtp.host = "mail.gandi.net";
     89               mbsync = {
     90                 enable = true;
     91                 create = "both";
     92                 expunge = "both";
     93                 patterns = [ "*" ];
     94                 extraConfig = {
     95                   channel = {
     96                     Sync = "All";
     97                   };
     98                   account = {
     99                     Timeout = 120;
    100                     PipelineDepth = 1;
    101                   };
    102                 };
    103               };
    104               notmuch.enable = cfg.sync;
    105               astroid.enable = cfg.sync;
    106               msmtp.enable = true;
    107             };
    108           };
    109         };
    110 
    111         home.file."bin/msmtp" = {
    112           text = ''
    113             #!${pkgs.stdenv.shell}
    114             ${pkgs.libnotify}/bin/notify-send "Sending mail ✉️"
    115             ${pkgs.msmtp}/bin/msmtp --read-envelope-from $@
    116           '';
    117           executable = true;
    118         };
    119 
    120         programs.msmtp.enable = true;
    121 
    122       }
    123 
    124       (
    125         mkIf cfg.sync {
    126 
    127           services.mbsync = {
    128             enable = true;
    129             preExec = "${config.xdg.configHome}/mbsync/preExec";
    130             postExec = "${config.xdg.configHome}/mbsync/postExec";
    131             frequency = cfg.frequency;
    132           };
    133 
    134           xdg.configFile."mbsync/preExec" = {
    135             text = ''
    136               #!${pkgs.stdenv.shell}
    137 
    138               export NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/notmuchrc
    139               export NMBGIT=${config.xdg.dataHome}/notmuch/nmbug
    140 
    141               ${pkgs.coreutils}/bin/mkdir -p ${config.home.homeDirectory}/desktop/mails/redhat ${config.home.homeDirectory}/desktop/mails/perso
    142               ${pkgs.afew}/bin/afew -C  ${config.xdg.configHome}/notmuch/notmuchrc -m -v || true
    143             '';
    144             executable = true;
    145           };
    146 
    147           xdg.configFile."mbsync/postExec" = {
    148             text = ''
    149               #!${pkgs.stdenv.shell}
    150 
    151               export NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/notmuchrc
    152               export NMBGIT=${config.xdg.dataHome}/notmuch/nmbug
    153 
    154               ${pkgs.notmuch}/bin/notmuch new
    155               ${pkgs.afew}/bin/afew -C ${config.xdg.configHome}/notmuch/notmuchrc --tag --new -v
    156               # Remove inbox (lower-case)
    157               ${pkgs.notmuch}/bin/notmuch tag -inbox -- tag:inbox
    158               # Remove Inbox tagged message that are not in an Inbox
    159               ${pkgs.notmuch}/bin/notmuch tag -Inbox -- not folder:redhat/Inbox and not folder:perso/Inbox and tag:Inbox
    160               ${pkgs.libnotify}/bin/notify-send "Mails synced 📬"
    161             '';
    162             executable = true;
    163           };
    164 
    165           home.file."bin/msync" = {
    166             text = ''
    167               #!${pkgs.stdenv.shell}
    168               ${pkgs.libnotify}/bin/notify-send "Syncing mails 📫️"
    169               systemctl --user start mbsync
    170             '';
    171             executable = true;
    172           };
    173 
    174           programs.mbsync.enable = true;
    175           programs.notmuch.enable = true;
    176 
    177           programs.afew = {
    178             enable = true;
    179             extraConfig = ''
    180               [SpamFilter]
    181               [KillThreadsFilter]
    182               [ListMailsFilter]
    183               [ArchiveSentMailsFilter]
    184               [FolderNameFilter]
    185               maildir_separator = /
    186 
    187               [MailMover]
    188               folders = perso/Inbox redhat/Inbox
    189               rename = true
    190 
    191               perso/Inbox = 'NOT tag:Inbox':"perso/[Gmail]/All Mail"
    192               redhat/Inbox = 'NOT tag:Inbox':"redhat/[Gmail]/All Mail"
    193             '';
    194           };
    195 
    196           programs.astroid = {
    197             enable = true;
    198             externalEditor = "emacsclient -c";
    199             extraConfig = {
    200               startup.queries.inbox = "tag:Inbox";
    201               startup.queries.inbox_perso = "folder:perso/Inbox";
    202               startup.queries.inbox_redhat = "folder:redhat/Inbox";
    203             };
    204           };
    205 
    206         }
    207       )
    208 
    209     ]
    210   );
    211 }