khard: add option to set mutiple subdirs (#6823)

Add new option `accounts.contact.accounts.<name>.khard.addressbooks`.

Remove the previous soln,
`accounts.contact.accounts.<name>.khard.defaultCollection`, which is
superseded with the new option.

Add a new test to check the new `addressbooks` option. Modify an
existing test which was checking the removed `defaultCollection`.

Previous commit a38f88 allowed a hardcoded path to be set for khard if
the path set for its local storage is not the actual `vdir`. This was
accomplished via adding the `defaultCollection` option. However this
accepted only a single sub-directory, and when one has more than a
single collection on the same dir this would require repetition on
configuration to set [1].

This is a continuation of the soln given to
nix-community/home-manager#4531, refer to there and the previous PR [2]
for reference.

[1]: https://github.com/nix-community/home-manager/issues/4531#issuecomment-2701156246
[2]: https://github.com/nix-community/home-manager/pull/5220
This commit is contained in:
octvs
2025-04-18 16:38:13 +02:00
committed by GitHub
parent 72526a5f7c
commit 5e6a8203ce
6 changed files with 84 additions and 19 deletions

View File

@@ -5,6 +5,8 @@
...
}:
let
inherit (lib) types;
cfg = config.programs.khard;
accounts = lib.filterAttrs (_: acc: acc.khard.enable) config.accounts.contact.accounts;
@@ -25,6 +27,7 @@ let
};
in
{
meta.maintainers = [
lib.hm.maintainers.olmokramer
lib.maintainers.antonmosich
@@ -84,16 +87,28 @@ in
};
accounts.contact.accounts = lib.mkOption {
type =
with lib.types;
attrsOf (submodule {
type = types.attrsOf (
types.submodule {
imports = [
(lib.mkRenamedOptionModule [ "khard" "defaultCollection" ] [ "khard" "addressbooks" ])
];
options.khard.enable = lib.mkEnableOption "khard access";
options.khard.defaultCollection = lib.mkOption {
type = types.str;
default = "";
description = "VCARD collection to be searched by khard.";
options.khard.addressbooks = lib.mkOption {
type = types.coercedTo types.str lib.toList (types.listOf types.str);
default = [ "" ];
description = ''
If provided, each item on this list will generate an
entry on khard configuration file as a separate addressbook
(vdir).
This is used for hardcoding sub-directories under the local
storage path
(accounts.contact.accounts.<name>.local.path) for khard. The
default value will set the aforementioned path as a single vdir.
'';
};
});
}
);
};
};
@@ -103,21 +118,25 @@ in
xdg.configFile."khard/khard.conf".text =
let
makePath =
anAccount:
baseDir: subDir:
builtins.toString (
/.
+ lib.concatStringsSep "/" [
anAccount.local.path
anAccount.khard.defaultCollection
baseDir
subDir
]
);
makeName = accName: abookName: accName + lib.optionalString (abookName != "") "-${abookName}";
makeEntry = anAccount: anAbook: ''
[[${makeName anAccount.name anAbook}]]
path = ${makePath anAccount.local.path anAbook}
'';
in
''
[addressbooks]
${lib.concatMapStringsSep "\n" (acc: ''
[[${acc.name}]]
path = ${makePath acc}
'') (lib.attrValues accounts)}
${lib.concatMapStringsSep "\n" (
acc: lib.concatMapStringsSep "\n" (makeEntry acc) acc.khard.addressbooks
) (lib.attrValues accounts)}
${renderSettings cfg.settings}
'';