fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
5.5 KiB198 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.notmuch;
8
9  mkIniKeyValue = key: value:
10    let
11      tweakVal = v:
12        if isString v then
13          v
14        else if isList v then
15          concatMapStringsSep ";" tweakVal v
16        else if isBool v then
17          (if v then "true" else "false")
18        else
19          toString v;
20    in "${key}=${tweakVal value}";
21
22  notmuchIni = recursiveUpdate {
23    database = { path = config.accounts.email.maildirBasePath; };
24
25    maildir = { synchronize_flags = cfg.maildir.synchronizeFlags; };
26
27    new = {
28      ignore = cfg.new.ignore;
29      tags = cfg.new.tags;
30    };
31
32    user = let
33      accounts = filter (a: a.notmuch.enable)
34        (attrValues config.accounts.email.accounts);
35      primary = filter (a: a.primary) accounts;
36      secondaries = filter (a: !a.primary) accounts;
37    in {
38      name = catAttrs "realName" primary;
39      primary_email = catAttrs "address" primary;
40      other_email = catAttrs "aliases" primary ++ catAttrs "address" secondaries
41        ++ catAttrs "aliases" secondaries;
42    };
43
44    search = { exclude_tags = cfg.search.excludeTags; };
45  } cfg.extraConfig;
46
47in {
48  options = {
49    programs.notmuch = {
50      enable = mkEnableOption "Notmuch mail indexer";
51
52      new = mkOption {
53        type = types.submodule {
54          options = {
55            ignore = mkOption {
56              type = types.listOf types.str;
57              default = [ ];
58              description = ''
59                A list to specify files and directories that will not be
60                searched for messages by {command}`notmuch new`.
61              '';
62            };
63
64            tags = mkOption {
65              type = types.listOf types.str;
66              default = [ "unread" "inbox" ];
67              example = [ "new" ];
68              description = ''
69                A list of tags that will be added to all messages
70                incorporated by {command}`notmuch new`.
71              '';
72            };
73          };
74        };
75        default = { };
76        description = ''
77          Options related to email processing performed by
78          {command}`notmuch new`.
79        '';
80      };
81
82      extraConfig = mkOption {
83        type = types.attrsOf (types.attrsOf types.str);
84        default = { };
85        description = ''
86          Options that should be appended to the notmuch configuration file.
87        '';
88      };
89
90      hooks = {
91        preNew = mkOption {
92          type = types.lines;
93          default = "";
94          example = "mbsync --all";
95          description = ''
96            Bash statements run before scanning or importing new
97            messages into the database.
98          '';
99        };
100
101        postNew = mkOption {
102          type = types.lines;
103          default = "";
104          example = ''
105            notmuch tag +nixos -- tag:new and from:nixos1@discoursemail.com
106          '';
107          description = ''
108            Bash statements run after new messages have been imported
109            into the database and initial tags have been applied.
110          '';
111        };
112
113        postInsert = mkOption {
114          type = types.lines;
115          default = "";
116          description = ''
117            Bash statements run after a message has been inserted
118            into the database and initial tags have been applied.
119          '';
120        };
121      };
122
123      maildir = {
124        synchronizeFlags = mkOption {
125          type = types.bool;
126          default = true;
127          description = ''
128            Whether to synchronize Maildir flags.
129          '';
130        };
131      };
132
133      search = {
134        excludeTags = mkOption {
135          type = types.listOf types.str;
136          default = [ "deleted" "spam" ];
137          example = [ "trash" "spam" ];
138          description = ''
139            A  list  of  tags  that  will be excluded from search results by
140            default. Using an excluded tag in a  query  will  override  that
141            exclusion.
142          '';
143        };
144      };
145    };
146
147    accounts.email.accounts = mkOption {
148      type = with types;
149        attrsOf (submodule {
150          options.notmuch.enable = mkEnableOption "notmuch indexing";
151        });
152    };
153  };
154
155  config = mkIf cfg.enable {
156    assertions = [
157      {
158        assertion = notmuchIni.user.name != [ ];
159        message = "notmuch: Must have a user name set.";
160      }
161      {
162        assertion = notmuchIni.user.primary_email != [ ];
163        message = "notmuch: Must have a user primary email address set.";
164      }
165    ];
166
167    home.packages = [ pkgs.notmuch ];
168
169    home.sessionVariables = {
170      NOTMUCH_CONFIG = "${config.xdg.configHome}/notmuch/default/config";
171      NMBGIT = "${config.xdg.dataHome}/notmuch/nmbug";
172    };
173
174    xdg.configFile = let
175      hook = name: cmds: {
176        "notmuch/default/hooks/${name}".source = pkgs.writeShellScript name ''
177          export PATH="${pkgs.notmuch}/bin''${PATH:+:}$PATH"
178          export NOTMUCH_CONFIG="${config.xdg.configHome}/notmuch/default/config"
179          export NMBGIT="${config.xdg.dataHome}/notmuch/nmbug"
180
181          ${cmds}
182        '';
183      };
184    in {
185      "notmuch/default/config".text =
186        let toIni = generators.toINI { mkKeyValue = mkIniKeyValue; };
187        in ''
188          # Generated by Home Manager.
189
190        '' + toIni notmuchIni;
191    }
192    // optionalAttrs (cfg.hooks.preNew != "") (hook "pre-new" cfg.hooks.preNew)
193    // optionalAttrs (cfg.hooks.postNew != "")
194    (hook "post-new" cfg.hooks.postNew)
195    // optionalAttrs (cfg.hooks.postInsert != "")
196    (hook "post-insert" cfg.hooks.postInsert);
197  };
198}