aboutsummaryrefslogtreecommitdiffstats
path: root/modules/communication/neomutt/default.nix
blob: c379a0286ee5a06dab9d619c4341635bf2bf7f2d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Email suite entry point
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.communication.neomutt;

  mbsyncNamesFromName = (import ./lib.nix { inherit lib; }).mbsyncNamesFromName;

  fullSyncChannels = lib.mapAttrsToList (name: account: (mbsyncNamesFromName name).channels.full) (
    lib.filterAttrs (name: account: account.enable) cfg.accounts
  );

  neomuttSyncScript = pkgs.writeShellApplication {
    name = "neomutt-sync";
    runtimeInputs = [
      config.terminal.bash.package
      pkgs.dialog
      pkgs.coreutils
      pkgs.gawk
      pkgs.gnused
      pkgs.util-linux
      config.programs.mbsync.package
      config.programs.notmuch.package
    ];
    text = builtins.readFile ./neomutt-sync.sh;
  };

  neomuttSyncDaemon = pkgs.writeShellApplication {
    name = "neomutt-sync-daemon";
    runtimeInputs = [
      config.terminal.bash.package
      pkgs.coreutils
      pkgs.procps
      config.programs.mbsync.package
      config.programs.notmuch.package
    ];
    text = builtins.readFile ./neomutt-sync-daemon.sh;
  };

  neomuttLauncher = pkgs.writeShellApplication {
    name = "neomutt-launcher";
    runtimeInputs = [
      pkgs.systemd
      pkgs.procps
      pkgs.neomutt
      pkgs.urlscan
    ];
    text =
      builtins.replaceStrings
        [
          "@@NEOMUTT_BIN@@"
        ]
        [
          (lib.getExe pkgs.neomutt)
        ]
        (builtins.readFile ./neomutt-launcher.sh);
  };

  neomuttSyncCompletion =
    pkgs.runCommand "neomutt-sync-completion"
      {
        nativeBuildInputs = [ pkgs.installShellFiles ];
      }
      ''
        mkdir -p $out/share/zsh/site-functions
        installShellCompletion --zsh --name _neomutt-sync ${pkgs.writeText "neomutt-sync.zsh" (builtins.readFile ./neomutt-sync.zsh)}
      '';

  neomuttSync = pkgs.symlinkJoin {
    name = "nix-update";
    paths = [
      neomuttSyncScript
      neomuttSyncCompletion
    ];
    meta = neomuttSyncScript.meta;
  };
in
{
  imports = [
    ./account
    ./mail
    ./assertions.nix
    ./keybindings.nix
    ./macros.nix
  ];

  options.communication.neomutt = {
    enable = lib.mkEnableOption "Email suite (neomutt + mbsync + notmuch)";

    package = lib.mkOption {
      type = lib.types.package;
      readOnly = true;
      default = config.programs.neomutt.package;
      description = "The neomutt package to use, wrapped with urlscan in PATH.";
    };
  };

  config = lib.mkMerge [
    (lib.mkIf cfg.enable {
      accounts.email.maildirBasePath = "${config.home.homeDirectory}/.local/share/mail";
      home.sessionVariables.MAILDIR = config.accounts.email.maildirBasePath;

      xdg.configFile."neomutt/mailcap".text =
        builtins.replaceStrings [ "@@HTML_VIEWER@@" ] [ (lib.getExe config.web.chawan.package) ]
          (builtins.readFile ./mailcap);

      programs.neomutt = {
        enable = true;
        package = pkgs.symlinkJoin {
          name = "neomutt-wrapped";
          paths = [
            neomuttLauncher
            pkgs.neomutt
            neomuttSync
          ];
          meta.mainProgram = "neomutt-launcher";
        };
        sidebar.enable = true;
        sort = "reverse-threads";
        vimKeys = false;
        unmailboxes = true;
        checkStatsInterval = 20;
        extraConfig = builtins.replaceStrings [ "@@PAGER@@" ] [ (lib.getExe config.web.chawan.package) ] (
          builtins.readFile ./.neomuttrc
        );
      };

      systemd.user.services.neomutt-sync = {
        Unit = {
          Description = "Full mail sync while neomutt is running";
        };
        Service = {
          Type = "simple";
          ExecStart = "${lib.getExe neomuttSyncDaemon} ${lib.concatStringsSep " " fullSyncChannels}";
        };
      };
    })
    (lib.mkIf (cfg.enable && config.terminal.kitty.enable) {
      xdg.desktopEntries."neomutt" = {
        name = "NeoMutt";
        exec = "${lib.getExe config.terminal.kitty.package} --class neomutt -e ${lib.getExe config.programs.neomutt.package}";
        icon = "neomutt";
        categories = [
          "Network"
          "Email"
        ];
        comment = "Terminal email client";
        terminal = false;
        type = "Application";
      };
      xdg.mimeApps.defaultApplications = {
        "x-scheme-handler/mailto" = "neomutt.desktop";
      };
    })
  ];
}