1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-19 01:29:46 +01:00
home-manager/modules/programs/aerc.nix
Lukas Nagel 8da1135365
aerc: improve module (#3150)
* aerc: add space after definitions

* aerc: only generate files, if options were set

* aerc: improve file permission warning

* aerc: remove redundant access to builtins

* aerc: allow overwriting of derived values

the order of merging the config subsets did not allow the user to specify
outgoing, source and password command values,
if they were previously derived from the SMTP, IMAP, Maildir etc config.

The values from `account.<name>.extraAccounts` now have the highest precedence.
Appropriate tests were added as well.

* aerc: write primary account first
2023-06-13 10:59:42 +02:00

204 lines
6.5 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.aerc;
primitive = with types;
((type: either type (listOf type)) (nullOr (oneOf [ str int bool float ])))
// {
description =
"values (null, bool, int, string of float) or a list of values, that will be joined with a comma";
};
confSection = types.attrsOf primitive;
confSections = types.attrsOf confSection;
sectionsOrLines = types.either types.lines confSections;
accounts = import ./aerc-accounts.nix {
inherit config pkgs lib confSection confSections;
};
aerc-accounts =
attrsets.filterAttrs (_: v: v.aerc.enable) config.accounts.email.accounts;
in {
meta.maintainers = with lib.hm.maintainers; [ lukasngl ];
options.accounts.email.accounts = accounts.type;
options.programs.aerc = {
enable = mkEnableOption "aerc";
package = mkPackageOption pkgs "aerc" { };
extraAccounts = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression
''{ Work = { source = "maildir://~/Maildir/work"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/accounts.conf</filename>.
See aerc-config(5).
'';
};
extraBinds = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression ''{ messages = { q = ":quit<Enter>"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/binds.conf</filename>.
Global keybindings can be set in the `global` section.
See aerc-config(5).
'';
};
extraConfig = mkOption {
type = sectionsOrLines;
default = { };
example = literalExpression ''{ ui = { sort = "-r date"; }; }'';
description = ''
Extra lines added to <filename>$HOME/.config/aerc/aerc.conf</filename>.
See aerc-config(5).
'';
};
stylesets = mkOption {
type = with types; attrsOf (either confSection lines);
default = { };
example = literalExpression ''
{ default = { ui = { "tab.selected.reverse" = toggle; }; }; };
'';
description = ''
Stylesets added to <filename>$HOME/.config/aerc/stylesets/</filename>.
See aerc-stylesets(7).
'';
};
templates = mkOption {
type = with types; attrsOf lines;
default = { };
example = literalExpression ''
{ new_message = "Hello!"; };
'';
description = ''
Templates added to <filename>$HOME/.config/aerc/templates/</filename>.
See aerc-templates(7).
'';
};
};
config = let
joinCfg = cfgs: concatStringsSep "\n" (filter (v: v != "") cfgs);
toINI = conf: # quirk: global section is prepended w/o section heading
let
global = conf.global or { };
local = removeAttrs conf [ "global" ];
optNewLine = if global != { } && local != { } then "\n" else "";
mkValueString = v:
if isList v then # join with comma
concatStringsSep "," (map (generators.mkValueStringDefault { }) v)
else
generators.mkValueStringDefault { } v;
mkKeyValue =
generators.mkKeyValueDefault { inherit mkValueString; } " = ";
in joinCfg [
(generators.toKeyValue { inherit mkKeyValue; } global)
(generators.toINI { inherit mkKeyValue; } local)
];
mkINI = conf: if isString conf then conf else toINI conf;
mkStyleset = attrsets.mapAttrs' (k: v:
let value = if isString v then v else toINI { global = v; };
in {
name = "aerc/stylesets/${k}";
value.text = joinCfg [ header value ];
});
mkTemplates = attrsets.mapAttrs' (k: v: {
name = "aerc/templates/${k}";
value.text = v;
});
primaryAccount = attrsets.filterAttrs (_: v: v.primary) aerc-accounts;
otherAccounts = attrsets.filterAttrs (_: v: !v.primary) aerc-accounts;
primaryAccountAccounts = mapAttrs accounts.mkAccount primaryAccount;
accountsExtraAccounts = mapAttrs accounts.mkAccount otherAccounts;
accountsExtraConfig = mapAttrs accounts.mkAccountConfig aerc-accounts;
accountsExtraBinds = mapAttrs accounts.mkAccountBinds aerc-accounts;
joinContextual = contextual: joinCfg (map mkINI (attrValues contextual));
isRecursivelyEmpty = x:
if isAttrs x then
all (x: x == { } || isRecursivelyEmpty x) (attrValues x)
else
false;
genAccountsConf = ((cfg.extraAccounts != "" && cfg.extraAccounts != { })
|| !(isRecursivelyEmpty accountsExtraAccounts)
|| !(isRecursivelyEmpty primaryAccountAccounts));
genAercConf = ((cfg.extraConfig != "" && cfg.extraConfig != { })
|| !(isRecursivelyEmpty accountsExtraConfig));
genBindsConf = ((cfg.extraBinds != "" && cfg.extraBinds != { })
|| !(isRecursivelyEmpty accountsExtraBinds));
header = ''
# Generated by Home Manager.
'';
in mkIf cfg.enable {
warnings = if genAccountsConf
&& (cfg.extraConfig.general.unsafe-accounts-conf or false) == false then [''
aerc: An email account was configured, but `extraConfig.general.unsafe-accounts-conf` is set to false or unset.
This will prevent aerc from starting, see `unsafe-accounts-conf` in the man page aerc-config(5), which states:
> By default, the file permissions of accounts.conf must be restrictive and only allow reading by the file owner (0600).
> Set this option to true to ignore this permission check. Use this with care as it may expose your credentials.
These file permissions are not possible with home-manger, since the generated file is stored in the nix-store with read-only access for all users (0444).
If `passwordCommand` is properly set, no credentials will be stored in the nix store.
Therefore, consider setting the option `extraConfig.general.unsafe-accounts-conf` to true.
''] else
[ ];
home.packages = [ cfg.package ];
xdg.configFile = {
"aerc/accounts.conf" = mkIf genAccountsConf {
text = joinCfg [
header
(mkINI cfg.extraAccounts)
(mkINI primaryAccountAccounts)
(mkINI accountsExtraAccounts)
];
};
"aerc/aerc.conf" = mkIf genAercConf {
text = joinCfg [
header
(mkINI cfg.extraConfig)
(joinContextual accountsExtraConfig)
];
};
"aerc/binds.conf" = mkIf genBindsConf {
text = joinCfg [
header
(mkINI cfg.extraBinds)
(joinContextual accountsExtraBinds)
];
};
} // (mkStyleset cfg.stylesets) // (mkTemplates cfg.templates);
};
}