accounts.email: introduce accounts.email.order

Nix does not preserve attrset order, instead sorting attributes by alphabetical order.

However, aerc display's accounts based on their order in `.config/aerc/accounts.conf`.

This commit introduces a `accounts.email.order` list that provides an optional order for accounts in `accounts.email.accounts`.

Support for this is implemeted in the `aerc` module. If a partial order is provided those will be ordered after the primary account, and any unordered accounts will be included with the previous alphabetical behavior after.
This commit is contained in:
Ryan Gibb 2024-01-08 13:56:21 +00:00
parent cd88671199
commit 0e78a207ea
No known key found for this signature in database
GPG key ID: 19F9DB637E4CAB74
2 changed files with 24 additions and 10 deletions

View file

@ -509,6 +509,13 @@ in {
default = { }; default = { };
description = "List of email accounts."; description = "List of email accounts.";
}; };
order = mkOption {
type = types.listOf types.str;
default = [ ];
description =
"Order of email accounts from [](#opt-accounts.email.accounts).";
};
}; };
config = mkIf (cfg.accounts != { }) { config = mkIf (cfg.accounts != { }) {

View file

@ -23,6 +23,9 @@ let
aerc-accounts = aerc-accounts =
attrsets.filterAttrs (_: v: v.aerc.enable) config.accounts.email.accounts; attrsets.filterAttrs (_: v: v.aerc.enable) config.accounts.email.accounts;
aerc-accounts-order =
builtins.filter (n: config.accounts.email.accounts.${n}.aerc.enable)
config.accounts.email.order;
configDir = if (pkgs.stdenv.isDarwin && !config.xdg.enable) then configDir = if (pkgs.stdenv.isDarwin && !config.xdg.enable) then
"Library/Preferences/aerc" "Library/Preferences/aerc"
@ -136,11 +139,15 @@ in {
}); });
primaryAccount = attrsets.filterAttrs (_: v: v.primary) aerc-accounts; primaryAccount = attrsets.filterAttrs (_: v: v.primary) aerc-accounts;
otherAccounts = attrsets.filterAttrs (_: v: !v.primary) aerc-accounts; orderedAccountsList =
builtins.map (n: { ${n} = aerc-accounts."${n}"; }) aerc-accounts-order;
unorderedAccounts = attrsets.filterAttrs
(n: v: !v.primary && !builtins.elem n aerc-accounts-order) aerc-accounts;
primaryAccountAccounts = mapAttrs accounts.mkAccount primaryAccount; primaryAccountAccounts = mapAttrs accounts.mkAccount primaryAccount;
orderedAccountsAccountsList =
accountsExtraAccounts = mapAttrs accounts.mkAccount otherAccounts; builtins.map (mapAttrs accounts.mkAccount) orderedAccountsList;
unorderedAccountsAccounts = mapAttrs accounts.mkAccount unorderedAccounts;
accountsExtraConfig = mapAttrs accounts.mkAccountConfig aerc-accounts; accountsExtraConfig = mapAttrs accounts.mkAccountConfig aerc-accounts;
@ -155,7 +162,9 @@ in {
false; false;
genAccountsConf = ((cfg.extraAccounts != "" && cfg.extraAccounts != { }) genAccountsConf = ((cfg.extraAccounts != "" && cfg.extraAccounts != { })
|| !(isRecursivelyEmpty accountsExtraAccounts) || !(isRecursivelyEmpty unorderedAccountsAccounts)
|| !(builtins.all (v: v)
(builtins.map isRecursivelyEmpty orderedAccountsAccountsList))
|| !(isRecursivelyEmpty primaryAccountAccounts)); || !(isRecursivelyEmpty primaryAccountAccounts));
genAercConf = ((cfg.extraConfig != "" && cfg.extraConfig != { }) genAercConf = ((cfg.extraConfig != "" && cfg.extraConfig != { })
@ -197,12 +206,10 @@ in {
home.file = { home.file = {
"${configDir}/accounts.conf" = mkIf genAccountsConf { "${configDir}/accounts.conf" = mkIf genAccountsConf {
text = joinCfg [ text = joinCfg
header ([ header (mkINI cfg.extraAccounts) (mkINI primaryAccountAccounts) ]
(mkINI cfg.extraAccounts) ++ (builtins.map mkINI orderedAccountsAccountsList)
(mkINI primaryAccountAccounts) ++ [ (mkINI unorderedAccountsAccounts) ]);
(mkINI accountsExtraAccounts)
];
}; };
"${configDir}/aerc.conf" = mkIf genAercConf { "${configDir}/aerc.conf" = mkIf genAercConf {