From eecebbf18632facd6d6fac51b2d54496cac68d92 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Thu, 28 Jun 2018 23:42:25 +0200 Subject: [PATCH] notmuch: add module Co-authored-by: Matthieu Coudron --- modules/misc/news.nix | 7 ++ modules/modules.nix | 1 + modules/programs/mbsync.nix | 2 + modules/programs/notmuch.nix | 196 +++++++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 modules/programs/notmuch.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index f87daed8..aba6e3be 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -697,6 +697,13 @@ in A new module is available: 'programs.mbsync'. ''; } + + { + time = "2018-07-01T16:12:20+00:00"; + message = '' + A new module is available: 'programs.notmuch'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 312c9c97..b9727ff3 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -45,6 +45,7 @@ let ./programs/mercurial.nix ./programs/neovim.nix ./programs/newsboat.nix + ./programs/notmuch.nix ./programs/pidgin.nix ./programs/rofi.nix ./programs/ssh.nix diff --git a/modules/programs/mbsync.nix b/modules/programs/mbsync.nix index f0e6fc1f..87a78355 100644 --- a/modules/programs/mbsync.nix +++ b/modules/programs/mbsync.nix @@ -219,6 +219,8 @@ in home.packages = [ cfg.package ]; + programs.notmuch.new.ignore = [ ".uidvalidity" ".mbsyncstate" ]; + home.file.".mbsyncrc".text = let accountsConfig = map genAccountConfig mbsyncAccounts; diff --git a/modules/programs/notmuch.nix b/modules/programs/notmuch.nix new file mode 100644 index 00000000..3d7a651f --- /dev/null +++ b/modules/programs/notmuch.nix @@ -0,0 +1,196 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + dag = config.lib.dag; + + 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 toJSON v + else toString v; + in + "${key}=${tweakVal value}"; + + notmuchIni = + recursiveUpdate + { + database = { + path = config.accounts.email.maildirBasePath; + }; + + 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 "address" secondaries; + }; + + search = { + exclude_tags = [ "deleted" "spam" ]; + }; + } + 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 notmuch new. + ''; + }; + + 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 notmuch new. + ''; + }; + }; + }; + default = {}; + description = '' + Options related to email processing performed by + notmuch new. + ''; + }; + + extraConfig = mkOption { + type = types.attrsOf (types.attrsOf types.str); + default = { + maildir = { synchronize_flags = "True"; }; + }; + 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. + ''; + }; + }; + }; + + accounts.email.accounts = mkOption { + options = [ + { + notmuch = { + enable = mkEnableOption "notmuch indexing"; + }; + } + ]; + }; + }; + + 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.\n\n" + + toIni notmuchIni; + + home.file = + let + hook = name: cmds: + { + target = "${notmuchIni.database.path}/.notmuch/hooks/${name}"; + source = pkgs.writeScript name '' + #!${pkgs.stdenv.shell} + + export PATH="${pkgs.notmuch}/bin''${PATH:+:}$PATH" + export NOTMUCH_CONFIG="${config.xdg.configHome}/notmuch/notmuchrc" + export NMBGIT="${config.xdg.dataHome}/notmuch/nmbug" + + ${cmds} + ''; + executable = true; + }; + in + optional (cfg.hooks.preNew != "") + (hook "pre-new" cfg.hooks.preNew) + ++ + optional (cfg.hooks.postNew != "") + (hook "post-new" cfg.hooks.postNew) + ++ + optional (cfg.hooks.postInsert != "") + (hook "post-insert" cfg.hooks.postInsert); + }; +}