1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-24 19:29:40 +01:00
home-manager/modules/programs/khard.nix
octvs 5e6a8203ce
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
2025-04-18 09:38:13 -05:00

144 lines
3.8 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
inherit (lib) types;
cfg = config.programs.khard;
accounts = lib.filterAttrs (_: acc: acc.khard.enable) config.accounts.contact.accounts;
renderSettings =
with lib.generators;
toINI {
mkKeyValue = mkKeyValueDefault rec {
mkValueString =
v:
if lib.isList v then
lib.concatStringsSep ", " v
else if lib.isBool v then
if v then "yes" else "no"
else
v;
} "=";
};
in
{
meta.maintainers = [
lib.hm.maintainers.olmokramer
lib.maintainers.antonmosich
];
options = {
programs.khard = {
enable = lib.mkEnableOption "Khard: an address book for the Unix console";
package = lib.mkPackageOption pkgs "khard" { };
settings = lib.mkOption {
type =
with lib.types;
submodule {
freeformType =
let
primOrList = oneOf [
bool
str
(listOf str)
];
in
attrsOf (attrsOf primOrList);
options.general.default_action = lib.mkOption {
type = str;
default = "list";
description = "The default action to execute.";
};
};
default = { };
description = ''
Khard settings. See
<https://khard.readthedocs.io/en/latest/#configuration>
for more information.
'';
example = lib.literalExpression ''
{
general = {
default_action = "list";
editor = ["vim" "-i" "NONE"];
};
"contact table" = {
display = "formatted_name";
preferred_phone_number_type = ["pref" "cell" "home"];
preferred_email_address_type = ["pref" "work" "home"];
};
vcard = {
private_objects = ["Jabber" "Skype" "Twitter"];
};
}
'';
};
};
accounts.contact.accounts = lib.mkOption {
type = types.attrsOf (
types.submodule {
imports = [
(lib.mkRenamedOptionModule [ "khard" "defaultCollection" ] [ "khard" "addressbooks" ])
];
options.khard.enable = lib.mkEnableOption "khard access";
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.
'';
};
}
);
};
};
config = lib.mkIf cfg.enable {
home.packages = lib.mkIf (cfg.package != null) [ cfg.package ];
xdg.configFile."khard/khard.conf".text =
let
makePath =
baseDir: subDir:
builtins.toString (
/.
+ lib.concatStringsSep "/" [
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: lib.concatMapStringsSep "\n" (makeEntry acc) acc.khard.addressbooks
) (lib.attrValues accounts)}
${renderSettings cfg.settings}
'';
};
}