aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/astroid.nix
blob: 0463cd15528aed3a27664725d8fb5a1b60fa25a7 (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
{ config, lib, pkgs, ... }:

with lib;
with builtins;

let

  cfg = config.programs.astroid;

  astroidAccounts =
    filterAttrs
      (n: v: v.astroid.enable)
      config.accounts.email.accounts;

  boolOpt = b: if b then "true" else "false";

  accountAttr = account: with account; {
    email = address;
    name = realName;
    sendmail = astroid.sendMailCommand;
    additional_sent_tags = "";
    default = boolOpt primary;
    save_drafts_to = folders.drafts;
    save_sent = "true";
    save_sent_to = folders.sent;
    select_query = "";
  }
  // optionalAttrs (signature.showSignature != "none") {
    signature_attach = boolOpt (signature.showSignature == "attach");
    signature_default_on = boolOpt (signature.showSignature != "none");
    signature_file = pkgs.writeText "signature.txt" signature.text;
    signature_file_markdown = "false";
    signature_separate = "true";  # prepends '--\n' to the signature
  }
  // optionalAttrs (gpg != null) {
    always_gpg_sign = boolOpt gpg.signByDefault;
    gpgkey = gpg.key;
  }
  // astroid.extraConfig;

  # See https://github.com/astroidmail/astroid/wiki/Configuration-Reference
  configFile = mailAccounts:
    let
      template = fromJSON (readFile ./astroid-config-template.json);
      astroidConfig = foldl' recursiveUpdate template [
        {
          astroid.notmuch_config = "${config.xdg.configHome}/notmuch/notmuchrc";
          accounts = mapAttrs (n: accountAttr) astroidAccounts;
          crypto.gpg.path = "${pkgs.gnupg}/bin/gpg";
        }
        cfg.extraConfig
        cfg.externalEditor
      ];
    in
      builtins.toJSON astroidConfig;

in

{
  options = {
    programs.astroid = {
      enable = mkEnableOption "Astroid";

      pollScript = mkOption {
        type = types.str;
        default = "";
        example = "mbsync gmail";
        description = ''
          Script to run to fetch/update mails.
        '';
      };

      externalEditor = mkOption {
        type = types.nullOr types.str;
        default = null;
        # Converts it into JSON that can be merged into the configuration.
        apply = cmd:
          optionalAttrs (cmd != null) {
            editor = {
              "external_editor" = "true";
              "cmd" = cmd;
            };
          };
        example = "nvim-qt -- -c 'set ft=mail' '+set fileencoding=utf-8' '+set ff=unix' '+set enc=utf-8' '+set fo+=w' %1";
        description = ''
          You can use <code>%1</code>, <code>%2</code>, and
          <code>%3</code> to refer respectively to:
          <orderedlist numeration="arabic">
            <listitem><para>file name</para></listitem>
            <listitem><para>server name</para></listitem>
            <listitem><para>socket ID</para></listitem>
          </orderedlist>
          See <link xlink:href='https://github.com/astroidmail/astroid/wiki/Customizing-editor' />.
        '';
      };

      extraConfig = mkOption {
        type = types.attrs;
        default = {};
        example = { poll.interval = 0; };
        description = ''
          JSON config that will override the default Astroid configuration.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = config.programs.notmuch.maildir.synchronizeFlags;
        message = "The astroid module requires"
          + " 'programs.notmuch.maildir.synchronizeFlags = true'.";
      }
    ];

    home.packages =  [ pkgs.astroid ];

    xdg.configFile."astroid/config".source =
      pkgs.runCommand "out.json"
        {
          json = configFile astroidAccounts;
          preferLocalBuild = true;
          allowSubstitutes = false;
        }
        ''
          echo -n "$json" | ${pkgs.jq}/bin/jq . > $out
        '';

    xdg.configFile."astroid/poll.sh" = {
      executable = true;
      text = ''
        # Generated by Home Manager

        ${cfg.pollScript}
      '';
    };
  };
}