aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/notmuch.nix
blob: 7e7a140b20c26c56a22e4fb681670e7f3ef5d0b0 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.notmuch;

  mkIniKeyValue = key: value:
    let
      tweakVal = v:
        if isString v then
          v
        else if isList v then
          concatMapStringsSep ";" tweakVal v
        else if isBool v then
          (if v then "true" else "false")
        else
          toString v;
    in "${key}=${tweakVal value}";

  notmuchIni = recursiveUpdate {
    database = { path = config.accounts.email.maildirBasePath; };

    maildir = { synchronize_flags = cfg.maildir.synchronizeFlags; };

    new = {
      ignore = cfg.new.ignore;
      tags = cfg.new.tags;
    };

    user = let
      accounts = filter (a: a.notmuch.enable)
        (attrValues config.accounts.email.accounts);
      primary = filter (a: a.primary) accounts;
      secondaries = filter (a: !a.primary) accounts;
    in {
      name = catAttrs "realName" primary;
      primary_email = catAttrs "address" primary;
      other_email = catAttrs "aliases" primary ++ catAttrs "address" secondaries
        ++ catAttrs "aliases" secondaries;
    };

    search = { exclude_tags = cfg.search.excludeTags; };
  } cfg.extraConfig;

in {
  options = {
    programs.notmuch = {
      enable = mkEnableOption "Notmuch mail indexer";

      new = mkOption {
        type = types.submodule {
          options = {
            ignore = mkOption {
              type = types.listOf types.str;
              default = [ ];
              description = ''
                A list to specify files and directories that will not be
                searched for messages by <command>notmuch new</command>.
              '';
            };

            tags = mkOption {
              type = types.listOf types.str;
              default = [ "unread" "inbox" ];
              example = [ "new" ];
              description = ''
                A list of tags that will be added to all messages
                incorporated by <command>notmuch new</command>.
              '';
            };
          };
        };
        default = { };
        description = ''
          Options related to email processing performed by
          <command>notmuch new</command>.
        '';
      };

      extraConfig = mkOption {
        type = types.attrsOf (types.attrsOf types.str);
        default = { };
        description = ''
          Options that should be appended to the notmuch configuration file.
        '';
      };

      hooks = {
        preNew = mkOption {
          type = types.lines;
          default = "";
          example = "mbsync --all";
          description = ''
            Bash statements run before scanning or importing new
            messages into the database.
          '';
        };

        postNew = mkOption {
          type = types.lines;
          default = "";
          example = ''
            notmuch tag +nixos -- tag:new and from:nixos1@discoursemail.com
          '';
          description = ''
            Bash statements run after new messages have been imported
            into the database and initial tags have been applied.
          '';
        };

        postInsert = mkOption {
          type = types.lines;
          default = "";
          description = ''
            Bash statements run after a message has been inserted
            into the database and initial tags have been applied.
          '';
        };
      };

      maildir = {
        synchronizeFlags = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Whether to synchronize Maildir flags.
          '';
        };
      };

      search = {
        excludeTags = mkOption {
          type = types.listOf types.str;
          default = [ "deleted" "spam" ];
          example = [ "trash" "spam" ];
          description = ''
            A  list  of  tags  that  will be excluded from search results by
            default. Using an excluded tag in a  query  will  override  that
            exclusion.
          '';
        };
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = notmuchIni.user.name != [ ];
        message = "notmuch: Must have a user name set.";
      }
      {
        assertion = notmuchIni.user.primary_email != [ ];
        message = "notmuch: Must have a user primary email address set.";
      }
    ];

    home.packages = [ pkgs.notmuch ];

    home.sessionVariables = {
      NOTMUCH_CONFIG = "${config.xdg.configHome}/notmuch/notmuchrc";
      NMBGIT = "${config.xdg.dataHome}/notmuch/nmbug";
    };

    xdg.configFile."notmuch/notmuchrc".text =
      let toIni = generators.toINI { mkKeyValue = mkIniKeyValue; };
      in ''
        # Generated by Home Manager.

      '' + toIni notmuchIni;

    home.file = let
      hook = name: cmds: {
        "${notmuchIni.database.path}/.notmuch/hooks/${name}".source =
          pkgs.writeShellScript name ''
            export PATH="${pkgs.notmuch}/bin''${PATH:+:}$PATH"
            export NOTMUCH_CONFIG="${config.xdg.configHome}/notmuch/notmuchrc"
            export NMBGIT="${config.xdg.dataHome}/notmuch/nmbug"

            ${cmds}
          '';
      };
    in optionalAttrs (cfg.hooks.preNew != "") (hook "pre-new" cfg.hooks.preNew)
    // optionalAttrs (cfg.hooks.postNew != "")
    (hook "post-new" cfg.hooks.postNew)
    // optionalAttrs (cfg.hooks.postInsert != "")
    (hook "post-insert" cfg.hooks.postInsert);
  };
}