summaryrefslogtreecommitdiff
path: root/user-profiles/feeds/module.nix
blob: aa04cb48f7a591d4c1482eacae6cf736ca4a843b (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
{ 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" ''
    [ { _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 ]);

    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}
    '';
  };
}