fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
3.5 KiB133 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4with builtins;
5
6let
7
8  cfg = config.programs.astroid;
9
10  jsonFormat = pkgs.formats.json { };
11
12  astroidAccounts =
13    filterAttrs (n: v: v.astroid.enable) config.accounts.email.accounts;
14
15  boolOpt = b: if b then "true" else "false";
16
17  accountAttr = account:
18    with account;
19    {
20      email = address;
21      name = realName;
22      sendmail = astroid.sendMailCommand;
23      additional_sent_tags = "";
24      default = boolOpt primary;
25      save_drafts_to = "${maildir.absPath}/${folders.drafts}/cur/";
26      save_sent = "true";
27      save_sent_to = "${maildir.absPath}/${folders.sent}/cur/";
28      select_query = "";
29    } // optionalAttrs (signature.showSignature != "none") {
30      signature_attach = boolOpt (signature.showSignature == "attach");
31      signature_default_on = boolOpt (signature.showSignature != "none");
32      signature_file = pkgs.writeText "signature.txt" signature.text;
33      signature_file_markdown = "false";
34      signature_separate = "true"; # prepends '--\n' to the signature
35    } // optionalAttrs (gpg != null) {
36      always_gpg_sign = boolOpt gpg.signByDefault;
37      gpgkey = gpg.key;
38    } // astroid.extraConfig;
39
40  # See https://github.com/astroidmail/astroid/wiki/Configuration-Reference
41  finalConfig = let
42    template = fromJSON (readFile ./astroid-config-template.json);
43    astroidConfig = foldl' recursiveUpdate template [
44      {
45        astroid.notmuch_config =
46          "${config.xdg.configHome}/notmuch/default/config";
47        accounts = mapAttrs (n: accountAttr) astroidAccounts;
48        crypto.gpg.path = "${pkgs.gnupg}/bin/gpg";
49      }
50      cfg.extraConfig
51      cfg.externalEditor
52    ];
53  in astroidConfig;
54
55in {
56  options = {
57    programs.astroid = {
58      enable = mkEnableOption "Astroid";
59
60      pollScript = mkOption {
61        type = types.str;
62        default = "";
63        example = "mbsync gmail";
64        description = ''
65          Script to run to fetch/update mails.
66        '';
67      };
68
69      externalEditor = mkOption {
70        type = types.nullOr types.str;
71        default = null;
72        # Converts it into JSON that can be merged into the configuration.
73        apply = cmd:
74          optionalAttrs (cmd != null) {
75            editor = {
76              "external_editor" = "true";
77              "cmd" = cmd;
78            };
79          };
80        example =
81          "nvim-qt -- -c 'set ft=mail' '+set fileencoding=utf-8' '+set ff=unix' '+set enc=utf-8' '+set fo+=w' %1";
82        description = ''
83          You can use the following variables:
84
85          `%1`
86          : file name
87
88          `%2`
89          : server name
90
91          `%3`
92          : socket ID
93
94          See [Customizing editor](https://github.com/astroidmail/astroid/wiki/Customizing-editor)
95          on the Astroid wiki.
96        '';
97      };
98
99      extraConfig = mkOption {
100        type = jsonFormat.type;
101        default = { };
102        example = literalExpression ''
103          {
104            poll.interval = 0;
105          }
106        '';
107        description = ''
108          JSON config that will override the default Astroid configuration.
109        '';
110      };
111    };
112
113    accounts.email.accounts = mkOption {
114      type = with types; attrsOf (submodule (import ./astroid-accounts.nix));
115    };
116  };
117
118  config = mkIf cfg.enable {
119    home.packages = [ pkgs.astroid ];
120
121    xdg.configFile."astroid/config".source =
122      jsonFormat.generate "astroid-config" finalConfig;
123
124    xdg.configFile."astroid/poll.sh" = {
125      executable = true;
126      text = ''
127        # Generated by Home Manager
128
129        ${cfg.pollScript}
130      '';
131    };
132  };
133}