From a38f88045e464c7ad5686e8d5fdaac39d3360e5b Mon Sep 17 00:00:00 2001
From: "O. C. Taskin" <42993892+octvs@users.noreply.github.com>
Date: Tue, 16 Jul 2024 15:17:37 +0200
Subject: [PATCH] khard: add option to contact module for khard dir

Add an extra option,

    accounts.contact.accounts.<name>.khard.defaultCollection

to hardcode a subdirectory for khard to function as intended. Khard
expects to be given a collection directory directly, from which there
can be multiple of in a vdir. This contrasts khal or vdirsyncer which
support recursive search.

Fixes #4531
---
 modules/programs/khard.nix                    | 15 +++++++++++--
 tests/modules/programs/khard/default.nix      |  1 +
 tests/modules/programs/khard/dirty_path.nix   | 22 +++++++++++++++++++
 .../programs/khard/dirty_path_expected        |  8 +++++++
 4 files changed, 44 insertions(+), 2 deletions(-)
 create mode 100644 tests/modules/programs/khard/dirty_path.nix
 create mode 100644 tests/modules/programs/khard/dirty_path_expected

diff --git a/modules/programs/khard.nix b/modules/programs/khard.nix
index a3e73fb07..cbd8c1f07 100644
--- a/modules/programs/khard.nix
+++ b/modules/programs/khard.nix
@@ -68,6 +68,11 @@ in {
       type = with lib.types;
         attrsOf (submodule {
           options.khard.enable = lib.mkEnableOption "khard access";
+          options.khard.defaultCollection = lib.mkOption {
+            type = types.str;
+            default = "";
+            description = "VCARD collection to be searched by khard.";
+          };
         });
     };
   };
@@ -75,11 +80,17 @@ in {
   config = lib.mkIf cfg.enable {
     home.packages = [ pkgs.khard ];
 
-    xdg.configFile."khard/khard.conf".text = ''
+    xdg.configFile."khard/khard.conf".text = let
+      makePath = anAccount:
+        builtins.toString (/. + lib.concatStringsSep "/" [
+          anAccount.local.path
+          anAccount.khard.defaultCollection
+        ]);
+    in ''
       [addressbooks]
       ${lib.concatMapStringsSep "\n" (acc: ''
         [[${acc.name}]]
-        path = ${acc.local.path}
+        path = ${makePath acc}
       '') (lib.attrValues accounts)}
 
       ${renderSettings cfg.settings}
diff --git a/tests/modules/programs/khard/default.nix b/tests/modules/programs/khard/default.nix
index 3762dc0f1..58b7a6f74 100644
--- a/tests/modules/programs/khard/default.nix
+++ b/tests/modules/programs/khard/default.nix
@@ -2,4 +2,5 @@
   khard_empty_config = ./empty_config.nix;
   khard_basic_config = ./basic_config.nix;
   khard_multiple_accounts = ./multiple_accounts.nix;
+  khard_dirty_path = ./dirty_path.nix;
 }
diff --git a/tests/modules/programs/khard/dirty_path.nix b/tests/modules/programs/khard/dirty_path.nix
new file mode 100644
index 000000000..07396a144
--- /dev/null
+++ b/tests/modules/programs/khard/dirty_path.nix
@@ -0,0 +1,22 @@
+{
+  accounts.contact = {
+    basePath = "/home/user/who/likes///";
+    accounts.forward = {
+      local.type = "filesystem";
+      khard = {
+        enable = true;
+        defaultCollection = "////slashes//a/lot";
+      };
+    };
+  };
+
+  programs.khard.enable = true;
+
+  test.stubs.khard = { };
+
+  nmt.script = ''
+    assertFileContent \
+      home-files/.config/khard/khard.conf \
+      ${./dirty_path_expected}
+  '';
+}
diff --git a/tests/modules/programs/khard/dirty_path_expected b/tests/modules/programs/khard/dirty_path_expected
new file mode 100644
index 000000000..8b7bb63eb
--- /dev/null
+++ b/tests/modules/programs/khard/dirty_path_expected
@@ -0,0 +1,8 @@
+[addressbooks]
+[[forward]]
+path = /home/user/who/likes/forward/slashes/a/lot
+
+
+[general]
+default_action=list
+