From 5e6a8203cee7cc33b2e0d9a0adb7268f46447292 Mon Sep 17 00:00:00 2001 From: octvs <42993892+octvs@users.noreply.github.com> Date: Fri, 18 Apr 2025 16:38:13 +0200 Subject: [PATCH] khard: add option to set mutiple subdirs (#6823) Add new option `accounts.contact.accounts..khard.addressbooks`. Remove the previous soln, `accounts.contact.accounts..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 --- modules/programs/khard.nix | 49 +++++++++++++------ tests/modules/programs/khard/default.nix | 1 + tests/modules/programs/khard/dirty_path.nix | 4 +- .../programs/khard/dirty_path_expected | 4 +- .../programs/khard/multiple_with_abooks.nix | 28 +++++++++++ .../khard/multiple_with_abooks_expected | 17 +++++++ 6 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 tests/modules/programs/khard/multiple_with_abooks.nix create mode 100644 tests/modules/programs/khard/multiple_with_abooks_expected diff --git a/modules/programs/khard.nix b/modules/programs/khard.nix index 68194f06..80e22475 100644 --- a/modules/programs/khard.nix +++ b/modules/programs/khard.nix @@ -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..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} ''; diff --git a/tests/modules/programs/khard/default.nix b/tests/modules/programs/khard/default.nix index 58b7a6f7..e50d28ba 100644 --- a/tests/modules/programs/khard/default.nix +++ b/tests/modules/programs/khard/default.nix @@ -3,4 +3,5 @@ khard_basic_config = ./basic_config.nix; khard_multiple_accounts = ./multiple_accounts.nix; khard_dirty_path = ./dirty_path.nix; + khard_multiple_with_abooks = ./multiple_with_abooks.nix; } diff --git a/tests/modules/programs/khard/dirty_path.nix b/tests/modules/programs/khard/dirty_path.nix index 8256a73c..941dbacf 100644 --- a/tests/modules/programs/khard/dirty_path.nix +++ b/tests/modules/programs/khard/dirty_path.nix @@ -1,11 +1,11 @@ { accounts.contact = { basePath = "/home/user/who/likes///"; - accounts.forward = { + accounts.kebap-case = { local.type = "filesystem"; khard = { enable = true; - defaultCollection = "////slashes//a/lot"; + addressbooks = [ "named-abook" ]; }; }; }; diff --git a/tests/modules/programs/khard/dirty_path_expected b/tests/modules/programs/khard/dirty_path_expected index 8b7bb63e..e2193099 100644 --- a/tests/modules/programs/khard/dirty_path_expected +++ b/tests/modules/programs/khard/dirty_path_expected @@ -1,6 +1,6 @@ [addressbooks] -[[forward]] -path = /home/user/who/likes/forward/slashes/a/lot +[[kebap-case-named-abook]] +path = /home/user/who/likes/kebap-case/named-abook [general] diff --git a/tests/modules/programs/khard/multiple_with_abooks.nix b/tests/modules/programs/khard/multiple_with_abooks.nix new file mode 100644 index 00000000..9ebaf43c --- /dev/null +++ b/tests/modules/programs/khard/multiple_with_abooks.nix @@ -0,0 +1,28 @@ +{ + accounts.contact = { + basePath = ".contacts"; + accounts.test1 = { + local.type = "filesystem"; + khard = { + enable = true; + addressbooks = [ + "home" + "work" + "family" + ]; + }; + }; + accounts.test2 = { + local.type = "filesystem"; + khard.enable = true; + }; + }; + + programs.khard.enable = true; + + nmt.script = '' + assertFileContent \ + home-files/.config/khard/khard.conf \ + ${./multiple_with_abooks_expected} + ''; +} diff --git a/tests/modules/programs/khard/multiple_with_abooks_expected b/tests/modules/programs/khard/multiple_with_abooks_expected new file mode 100644 index 00000000..49c8804c --- /dev/null +++ b/tests/modules/programs/khard/multiple_with_abooks_expected @@ -0,0 +1,17 @@ +[addressbooks] +[[test1-home]] +path = /home/hm-user/.contacts/test1/home + +[[test1-work]] +path = /home/hm-user/.contacts/test1/work + +[[test1-family]] +path = /home/hm-user/.contacts/test1/family + +[[test2]] +path = /home/hm-user/.contacts/test2 + + +[general] +default_action=list +