{ 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}.overrideAttrs (oldAttrs: { buildInputs = let oldDepends = filter (attr: if attr ? name then builtins.match "(uri-bytestring|atom-conduit)(-.*)?" attr.name == null else true) (oldAttrs.buildInputs or []); newDepends = [ (pkgs.haskellPackages.callCabal2nix "uri-bytestring" (pkgs.fetchFromGitHub { owner = "gkleen"; repo = "uri-bytestring"; rev = "5f7f32c8274bc4d1b81d99582f5148fe3e8b637e"; sha256 = "XLanwyCDIlMuOkpE5LbTNOBfL+1kZX+URfj9Bhs1Nsc="; fetchSubmodules = true; }) {}) (pkgs.haskellPackages.callCabal2nix "atom-conduit" (pkgs.fetchFromGitHub { owner = "gkleen"; repo = "atom-conduit"; rev = "af33d1162d84f1fca00fe92a2e47f0a1a5275b4b"; sha256 = "FbfA4cvF0Z9Q4ethJmXWAQmWdFZNds7XRIMWFXc+ByA="; fetchSubmodules = true; }) {}) ]; in oldDepends ++ newDepends; }); 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" '' [ { _executable = "${immNotmuchInsert}/bin/imm-notmuch-insert" , _arguments = [] : List Text } ] ''; 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 dateutil html2text ]); 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 ["linkGeneration" "writeBoundary"] '' $DRY_RUN_CMD mkdir -p -m 0750 $VERBOSE_ARG ${databasePath} $DRY_RUN_CMD mkdir -p $VERBOSE_ARG ${databasePath}/new ${databasePath}/cur ${databasePath}/tmp if ! [[ -d ${databasePath}/.notmuch ]]; then NOTMUCH_VERBOSE_ARG="--quiet" if [[ -v VERBOSE ]]; then NOTMUCH_VERBOSE_ARG="--verbose" fi NOTMUCH_CONFIG=${configPath} $DRY_RUN_CMD ${pkgs.notmuch}/bin/notmuch new $NOTMUCH_VERBOSE_ARG fi ''; }; }