From 09d82350943286abc4a0b66c84287eb30a4b6f43 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 4 Sep 2021 21:52:24 +0200 Subject: feeds: imm-notmuch-insert --- overlays/worktime/default.nix | 7 +- user-profiles/feeds/default.nix | 25 +++---- user-profiles/feeds/imm-notmuch-insert.py | 37 ++++++++++ user-profiles/feeds/module.nix | 114 ++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 17 deletions(-) create mode 100644 user-profiles/feeds/imm-notmuch-insert.py create mode 100644 user-profiles/feeds/module.nix diff --git a/overlays/worktime/default.nix b/overlays/worktime/default.nix index ab6fb40a..a8fe79c6 100644 --- a/overlays/worktime/default.nix +++ b/overlays/worktime/default.nix @@ -3,7 +3,7 @@ final: prev: { name = "worktime"; src = ./worktime.py; - phases = [ "buildPhase" "installPhase" ]; + phases = [ "buildPhase" "checkPhase" "installPhase" ]; python = prev.python39.withPackages (ps: with ps; [pyxdg dateutil uritools requests configparser tabulate]); @@ -11,6 +11,11 @@ final: prev: { substituteAll $src worktime ''; + doCheck = true; + checkPhase = '' + ${python}/bin/python -m py_compile worktime + ''; + installPhase = '' install -m 0755 -D -t $out/bin \ worktime diff --git a/user-profiles/feeds/default.nix b/user-profiles/feeds/default.nix index cfecf17e..82be90c7 100644 --- a/user-profiles/feeds/default.nix +++ b/user-profiles/feeds/default.nix @@ -1,18 +1,11 @@ -{ config, flakeInputs, userName, pkgs, lib, ... }: - -with lib; - -let - cfg = config.home-manager.users.${userName}; - inherit (flakeInputs.home-manager.lib) hm; - - imm = flakeInputs.imm.defaultPackage.${config.nixpkgs.system}; -in { - config.home-manager.users.${userName} = { - home.packages = [ imm ]; - - home.activation.createImm = hm.dag.entryAfter ["writeBoundary"] '' - $DRY_RUN_CMD mkdir -p $VERBOSE_ARG ${cfg.xdg.configHome}/imm - ''; +{ config, flakeInputs, pkgs, lib, userName, customUtils, ... }: +{ + home-manager.users.${userName} = {...}: { + imports = [ + (customUtils.overrideModuleArgs + (import ./module.nix) + (inputs: inputs // { inherit flakeInputs; inherit (config.nixpkgs) system; }) + ) + ]; }; } diff --git a/user-profiles/feeds/imm-notmuch-insert.py b/user-profiles/feeds/imm-notmuch-insert.py new file mode 100644 index 00000000..855f3a83 --- /dev/null +++ b/user-profiles/feeds/imm-notmuch-insert.py @@ -0,0 +1,37 @@ +#!@python@/bin/python + +import json +import sys +import subprocess +from io import StringIO +from email.message import EmailMessage +import configparser +from os import environ +from dateutil.parser import isoparse + +def main(): + notmuchConfig = configparser.ConfigParser() + notmuchConfig.read(environ.get('NOTMUCH_CONFIG')) + + callbackMessage = json.load(sys.stdin) + + msg = EmailMessage() + authors = ', '.join(map(lambda author: author['name'], callbackMessage['feed_item']['authors'])) + msg['From'] = f"{callbackMessage['feed_definition']['title']} ({authors}) " + msg['To'] = f"{notmuchConfig['user']['name']} <{notmuchConfig['user']['primary_email']}>" + msg['Subject'] = callbackMessage['feed_item']['title'] + msg['Message-ID'] = f"{callbackMessage['feed_item']['identifier']@imm.invalid}" + for link in callbackMessage['feed_item']['links']: + msg.add_header('Link', link) + if 'date' in callbackMessage['feed_item']: + date = isoparse(callbackMessage['feed_item']['date']) + msg['Date'] = date.strftime('%a, %e %b %Y %T %z') + + subprocess.run( + args=['notmuch', 'insert'], + check=True, + stdin=StringIO(bytes(msg)) + ) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/user-profiles/feeds/module.nix b/user-profiles/feeds/module.nix new file mode 100644 index 00000000..7a29331b --- /dev/null +++ b/user-profiles/feeds/module.nix @@ -0,0 +1,114 @@ +{ config, flakeInputs, pkgs, lib, system, ... }: + +with lib; + +let + inherit (flakeInputs.home-manager.lib) hm; + + configPath = "${config.xdg.configHome}/feeds/notmuchrc"; + databasePath = "${config.xdg.dataHome}/feeds"; + + imm = flakeInputs.imm.defaultPackage.${system}; + immWrapped = pkgs.runCommand "${imm.name}-wrapped-${config.home.username}" + { nativeBuildInputs = with pkgs; [ makeWrapper imm ]; + } '' + mkdir -p $out/bin + makeWrapper ${imm}/bin/imm $out/bin/imm \ + --add-flags --callbacks=${notmuchCallbacks} + ''; + + notmuchCallbacks = pkgs.writeText "imm-callbacks-${config.home.username}.dhall" '' + let Callback : Type = + { _executable : Text + , _arguments : List Text + } + + let notmuchInsert = + { _executable = "${immNotmuchInsert}/bin/imm-notmuch-insert" + , _arguments = [] + } + + let config : List Callback = [ notmuchInsert ] + in config + ''; + + immNotmuchInsert = pkgs.stdenv.mkDerivation rec { + name = "imm-notmuch-insert-${config.home.username}"; + src = ./imm-notmuch-insert.py; + + phases = [ "buildPhase" "checkPhase" "installPhase" "fixupPhase" ]; + + python = pkgs.python39.withPackages (ps: with ps; [ configparser ]); + + nativeBuildInputs = with pkgs; [ makeWrapper ]; + + buildPhase = '' + substituteAll $src imm-notmuch-insert + ''; + + doCheck = true; + checkPhase = '' + ${python}/bin/python -m py_compile imm-notmuch-insert + ''; + + installPhase = '' + install -m 0755 -D -t $out/bin \ + imm-notmuch-insert + ''; + + fixupPhase = '' + wrapProgram $out/bin/imm-notmuch-insert \ + --prefix PATH : ${pkgs.notmuch}/bin \ + --set NOTMUCH_CONFIG ${configPath} + ''; + }; + + 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 = { + database = { path = databasePath; }; + + maildir = { synchronize_flags = false; }; + + new = { + ignore = []; + tags = ["new"]; + }; + + user = { + name = config.home.username; + primary_email = "${config.home.username}@imm.invalid"; + }; + + search = { exclude_tags = ["deleted"]; }; + }; +in { + config = { + home.packages = [ immWrapped ]; + + home.activation.createImm = hm.dag.entryAfter ["writeBoundary"] '' + $DRY_RUN_CMD mkdir -p $VERBOSE_ARG ${config.xdg.configHome}/imm + ''; + + xdg.configFile."feeds/notmuchrc".text = + let toIni = generators.toINI { mkKeyValue = mkIniKeyValue; }; + in '' + # Generated by Home Manager. + '' + toIni notmuchIni; + + home.activation.createFeedsDatabase = hm.dag.entryAfter ["writeBoundary"] '' + $DRY_RUN_CMD mkdir -p $VERBOSE_ARG ${databasePath} + ''; + }; +} -- cgit v1.2.3