fork/home-manager

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

Knut Magnus Aasrudaerc: fix config paths on darwin1369d2ce

master
7.1 KiB225 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4let
5  cfg = config.programs.aerc;
6
7  primitive = with types;
8    ((type: either type (listOf type)) (nullOr (oneOf [ str int bool float ])))
9    // {
10      description =
11        "values (null, bool, int, string, or float) or a list of values, that will be joined with a comma";
12    };
13
14  confSection = types.attrsOf primitive;
15
16  confSections = types.attrsOf confSection;
17
18  sectionsOrLines = types.either types.lines confSections;
19
20  accounts = import ./aerc-accounts.nix {
21    inherit config pkgs lib confSection confSections;
22  };
23
24  aerc-accounts =
25    attrsets.filterAttrs (_: v: v.aerc.enable) config.accounts.email.accounts;
26
27  configDir = if (pkgs.stdenv.isDarwin && !config.xdg.enable) then
28    "Library/Preferences/aerc"
29  else
30    "${config.xdg.configHome}/aerc";
31
32in {
33  meta.maintainers = with lib.hm.maintainers; [ lukasngl ];
34
35  options.accounts.email.accounts = accounts.type;
36
37  options.programs.aerc = {
38
39    enable = mkEnableOption "aerc";
40
41    package = mkPackageOption pkgs "aerc" { };
42
43    extraAccounts = mkOption {
44      type = sectionsOrLines;
45      default = { };
46      example = literalExpression
47        ''{ Work = { source = "maildir://~/Maildir/work"; }; }'';
48      description = ''
49        Extra lines added to {file}`$HOME/.config/aerc/accounts.conf`.
50
51        See {manpage}`aerc-config(5)`.
52      '';
53    };
54
55    extraBinds = mkOption {
56      type = sectionsOrLines;
57      default = { };
58      example = literalExpression ''{ messages = { q = ":quit<Enter>"; }; }'';
59      description = ''
60        Extra lines added to {file}`$HOME/.config/aerc/binds.conf`.
61        Global keybindings can be set in the `global` section.
62
63        See {manpage}`aerc-config(5)`.
64      '';
65    };
66
67    extraConfig = mkOption {
68      type = sectionsOrLines;
69      default = { };
70      example = literalExpression ''{ ui = { sort = "-r date"; }; }'';
71      description = ''
72        Extra lines added to {file}`$HOME/.config/aerc/aerc.conf`.
73
74        See {manpage}`aerc-config(5)`.
75      '';
76    };
77
78    stylesets = mkOption {
79      type = with types; attrsOf (either confSection lines);
80      default = { };
81      example = literalExpression ''
82        { default = { ui = { "tab.selected.reverse" = toggle; }; }; };
83      '';
84      description = ''
85        Stylesets added to {file}`$HOME/.config/aerc/stylesets/`.
86
87        See {manpage}`aerc-stylesets(7)`.
88      '';
89    };
90
91    templates = mkOption {
92      type = with types; attrsOf lines;
93      default = { };
94      example = literalExpression ''
95        { new_message = "Hello!"; };
96      '';
97      description = ''
98        Templates added to {file}`$HOME/.config/aerc/templates/`.
99
100        See {manpage}`aerc-templates(7)`.
101      '';
102    };
103  };
104
105  config = let
106    joinCfg = cfgs: concatStringsSep "\n" (filter (v: v != "") cfgs);
107
108    toINI = conf: # quirk: global section is prepended w/o section heading
109      let
110        global = conf.global or { };
111        local = removeAttrs conf [ "global" ];
112        mkValueString = v:
113          if isList v then # join with comma
114            concatStringsSep "," (map (generators.mkValueStringDefault { }) v)
115          else
116            generators.mkValueStringDefault { } v;
117        mkKeyValue =
118          generators.mkKeyValueDefault { inherit mkValueString; } " = ";
119      in joinCfg [
120        (generators.toKeyValue { inherit mkKeyValue; } global)
121        (generators.toINI { inherit mkKeyValue; } local)
122      ];
123
124    mkINI = conf: if isString conf then conf else toINI conf;
125
126    mkStyleset = attrsets.mapAttrs' (k: v:
127      let value = if isString v then v else toINI { global = v; };
128      in {
129        name = "${configDir}/stylesets/${k}";
130        value.text = joinCfg [ header value ];
131      });
132
133    mkTemplates = attrsets.mapAttrs' (k: v: {
134      name = "${configDir}/templates/${k}";
135      value.text = v;
136    });
137
138    primaryAccount = attrsets.filterAttrs (_: v: v.primary) aerc-accounts;
139    otherAccounts = attrsets.filterAttrs (_: v: !v.primary) aerc-accounts;
140
141    primaryAccountAccounts = mapAttrs accounts.mkAccount primaryAccount;
142
143    accountsExtraAccounts = mapAttrs accounts.mkAccount otherAccounts;
144
145    accountsExtraConfig = mapAttrs accounts.mkAccountConfig aerc-accounts;
146
147    accountsExtraBinds = mapAttrs accounts.mkAccountBinds aerc-accounts;
148
149    joinContextual = contextual: joinCfg (map mkINI (attrValues contextual));
150
151    isRecursivelyEmpty = x:
152      if isAttrs x then
153        all (x: x == { } || isRecursivelyEmpty x) (attrValues x)
154      else
155        false;
156
157    genAccountsConf = ((cfg.extraAccounts != "" && cfg.extraAccounts != { })
158      || !(isRecursivelyEmpty accountsExtraAccounts)
159      || !(isRecursivelyEmpty primaryAccountAccounts));
160
161    genAercConf = ((cfg.extraConfig != "" && cfg.extraConfig != { })
162      || !(isRecursivelyEmpty accountsExtraConfig));
163
164    genBindsConf = ((cfg.extraBinds != "" && cfg.extraBinds != { })
165      || !(isRecursivelyEmpty accountsExtraBinds));
166
167    header = ''
168      # Generated by Home Manager.
169    '';
170
171  in mkIf cfg.enable {
172    warnings = if genAccountsConf
173    && (cfg.extraConfig.general.unsafe-accounts-conf or false) == false then [''
174      aerc: `programs.aerc.enable` is set, but `...extraConfig.general.unsafe-accounts-conf` is set to false or unset.
175      This will prevent aerc from starting; see `unsafe-accounts-conf` in the man page aerc-config(5):
176      > By default, the file permissions of accounts.conf must be restrictive and only allow reading by the file owner (0600).
177      > Set this option to true to ignore this permission check. Use this with care as it may expose your credentials.
178      These permissions are not possible with home-manager, since the generated file is in the nix-store (permissions 0444).
179      Therefore, please set `programs.aerc.extraConfig.general.unsafe-accounts-conf = true`.
180      This option is safe; if `passwordCommand` is properly set, no credentials will be written to the nix store.
181    ''] else
182      [ ];
183
184    assertions = [{
185      assertion = let
186        extraConfigSections = (unique (flatten
187          (mapAttrsToList (_: v: attrNames v.aerc.extraConfig) aerc-accounts)));
188      in extraConfigSections == [ ] || extraConfigSections == [ "ui" ];
189      message = ''
190        Only the ui section of $XDG_CONFIG_HOME/aerc.conf supports contextual (per-account) configuration.
191        Please configure it with accounts.email.accounts._.aerc.extraConfig.ui and move any other
192        configuration to programs.aerc.extraConfig.
193      '';
194    }];
195
196    home.packages = [ cfg.package ];
197
198    home.file = {
199      "${configDir}/accounts.conf" = mkIf genAccountsConf {
200        text = joinCfg [
201          header
202          (mkINI cfg.extraAccounts)
203          (mkINI primaryAccountAccounts)
204          (mkINI accountsExtraAccounts)
205        ];
206      };
207
208      "${configDir}/aerc.conf" = mkIf genAercConf {
209        text = joinCfg [
210          header
211          (mkINI cfg.extraConfig)
212          (joinContextual accountsExtraConfig)
213        ];
214      };
215
216      "${configDir}/binds.conf" = mkIf genBindsConf {
217        text = joinCfg [
218          header
219          (mkINI cfg.extraBinds)
220          (joinContextual accountsExtraBinds)
221        ];
222      };
223    } // (mkStyleset cfg.stylesets) // (mkTemplates cfg.templates);
224  };
225}