about summary refs log tree commit diff
path: root/home/mail/accounts/default.nix
blob: 5ebba2c247486e4f66afffb0d396c30a7bd85ff1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{ config, lib, pkgs, ... }:
let cfg = config.my.home.mail;
in
{
  config = lib.mkIf cfg.enable {
    accounts.email = {
      accounts = {
        Fastmail = rec {
          primary = true;
          address = "franck@fcuny.net";
          userName = address;
          realName = "Franck Cuny";
          aliases = [ "franck.cuny@gmail.com" ];
          passwordCommand = "pass email/imap.fastmail.com";
          imap.host = "imap.fastmail.com";
          smtp.host = "smtp.fastmail.com";
          mbsync = {
            enable = true;
            create = "maildir";
            expunge = "both";
            extraConfig.channel.CopyArrivalDate = "yes";
          };
          msmtp.enable = true;
          notmuch.enable = true;
        };
      };
    };

    programs.mbsync.enable = true;
    programs.msmtp.enable = true;

    programs.afew = {
      enable = true;
      extraConfig = ''
        [SpamFilter]
        [KillThreadsFilter]
        [ArchiveSentMailsFilter]

        [FolderNameFilter]
        maildir_separator = /
        folder_transforms = Archive:archive Drafts:draft Sent:sent
        folder_lowercases = true

        [MailMover]
        folders = Fastmail/Inbox
        rename = True
        max_age = 30

        # rules
        Fastmail/Inbox = 'tag:archive':Fastmail/Archive
      '';
    };

    programs.notmuch = {
      enable = true;
      maildir.synchronizeFlags = true;
      new.tags = [ "unread" "new" ];
      new.ignore = [ "Trash" ];
      search.excludeTags = [ "spam" "deleted" ];
      hooks = {
        postNew = "${config.home.profileDirectory}/bin/afew -v --tag --new --notmuch-config=${config.xdg.configHome}/notmuch/default/config";
      };
    };

    systemd.user.services.mbsync = {
      Unit = { Description = "mbsync synchronization"; };
      Service = {
        Type = "oneshot";
        Environment = [
          "PASSWORD_STORE_DIR=${config.programs.password-store.settings.PASSWORD_STORE_DIR}"
        ];
        ExecStartPre = [
          "${pkgs.notmuch}/bin/notmuch tag '-inbox' 'tag:inbox AND tag:archive'"
          "${pkgs.afew}/bin/afew -m -v --notmuch-config=${config.xdg.configHome}/notmuch/default/config"
        ];
        ExecStart = "${pkgs.isync}/bin/mbsync -a";
        ExecStartPost = "${pkgs.notmuch}/bin/notmuch new --quiet";
      };
    };

    systemd.user.timers.mbsync = {
      Unit = { Description = "mbsync synchronization"; };
      Timer = {
        OnBootSec = "30";
        OnUnitActiveSec = "5m";
      };
      Install = { WantedBy = [ "timers.target" ]; };
    };
  };
}