diff --git a/modules/misc/news.nix b/modules/misc/news.nix index b7395a79..af289935 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1251,6 +1251,13 @@ in A new module is available: 'programs.wpaperd'. ''; } + + { + time = "2023-10-01T07:28:45+00:00"; + message = '' + A new module is available: 'programs.khard'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 07c9f352..76c742cc 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -121,6 +121,7 @@ let ./programs/kakoune.nix ./programs/keychain.nix ./programs/khal.nix + ./programs/khard.nix ./programs/kitty.nix ./programs/kodi.nix ./programs/lazygit.nix diff --git a/modules/programs/khard.nix b/modules/programs/khard.nix new file mode 100644 index 00000000..a3e73fb0 --- /dev/null +++ b/modules/programs/khard.nix @@ -0,0 +1,88 @@ +{ config, lib, pkgs, ... }: +let + 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"; + + 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 + + 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 = with lib.types; + attrsOf (submodule { + options.khard.enable = lib.mkEnableOption "khard access"; + }); + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ pkgs.khard ]; + + xdg.configFile."khard/khard.conf".text = '' + [addressbooks] + ${lib.concatMapStringsSep "\n" (acc: '' + [[${acc.name}]] + path = ${acc.local.path} + '') (lib.attrValues accounts)} + + ${renderSettings cfg.settings} + ''; + }; +} diff --git a/tests/default.nix b/tests/default.nix index d82953d8..7ae8e6dc 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -93,6 +93,7 @@ import nmt { ./modules/programs/k9s ./modules/programs/kakoune ./modules/programs/khal + ./modules/programs/khard ./modules/programs/kitty ./modules/programs/ledger ./modules/programs/less diff --git a/tests/modules/programs/khard/basic_config.nix b/tests/modules/programs/khard/basic_config.nix new file mode 100644 index 00000000..f6f0838c --- /dev/null +++ b/tests/modules/programs/khard/basic_config.nix @@ -0,0 +1,34 @@ +{ + accounts.contact = { + basePath = ".contacts"; + accounts.test = { + local.type = "filesystem"; + khard.enable = true; + }; + }; + + programs.khard = { + enable = true; + settings = { + general = { + default_action = "list"; + editor = [ "vim" "-i" "NONE" ]; + }; + + "contact table" = { + group_by_address_book = true; + reverse = false; + preferred_phone_number_type = [ "pref" "cell" "home" ]; + preferred_email_address_type = [ "pref" "work" "home" ]; + }; + }; + }; + + test.stubs.khard = { }; + + nmt.script = '' + assertFileContent \ + home-files/.config/khard/khard.conf \ + ${./basic_config_expected} + ''; +} diff --git a/tests/modules/programs/khard/basic_config_expected b/tests/modules/programs/khard/basic_config_expected new file mode 100644 index 00000000..e636b5c6 --- /dev/null +++ b/tests/modules/programs/khard/basic_config_expected @@ -0,0 +1,15 @@ +[addressbooks] +[[test]] +path = /home/hm-user/.contacts/test + + +[contact table] +group_by_address_book=yes +preferred_email_address_type=pref, work, home +preferred_phone_number_type=pref, cell, home +reverse=no + +[general] +default_action=list +editor=vim, -i, NONE + diff --git a/tests/modules/programs/khard/default.nix b/tests/modules/programs/khard/default.nix new file mode 100644 index 00000000..3762dc0f --- /dev/null +++ b/tests/modules/programs/khard/default.nix @@ -0,0 +1,5 @@ +{ + khard_empty_config = ./empty_config.nix; + khard_basic_config = ./basic_config.nix; + khard_multiple_accounts = ./multiple_accounts.nix; +} diff --git a/tests/modules/programs/khard/empty_config.nix b/tests/modules/programs/khard/empty_config.nix new file mode 100644 index 00000000..9d19d11c --- /dev/null +++ b/tests/modules/programs/khard/empty_config.nix @@ -0,0 +1,19 @@ +{ + accounts.contact = { + basePath = ".contacts"; + accounts.test = { + local.type = "filesystem"; + khard.enable = true; + }; + }; + + programs.khard.enable = true; + + test.stubs.khard = { }; + + nmt.script = '' + assertFileContent \ + home-files/.config/khard/khard.conf \ + ${./empty_config_expected} + ''; +} diff --git a/tests/modules/programs/khard/empty_config_expected b/tests/modules/programs/khard/empty_config_expected new file mode 100644 index 00000000..10e79133 --- /dev/null +++ b/tests/modules/programs/khard/empty_config_expected @@ -0,0 +1,8 @@ +[addressbooks] +[[test]] +path = /home/hm-user/.contacts/test + + +[general] +default_action=list + diff --git a/tests/modules/programs/khard/multiple_accounts.nix b/tests/modules/programs/khard/multiple_accounts.nix new file mode 100644 index 00000000..393510bf --- /dev/null +++ b/tests/modules/programs/khard/multiple_accounts.nix @@ -0,0 +1,23 @@ +{ + accounts.contact = { + basePath = ".contacts"; + accounts.test1 = { + local.type = "filesystem"; + khard.enable = true; + }; + accounts.test2 = { + local.type = "filesystem"; + khard.enable = true; + }; + }; + + programs.khard.enable = true; + + test.stubs.khard = { }; + + nmt.script = '' + assertFileContent \ + home-files/.config/khard/khard.conf \ + ${./multiple_accounts_expected} + ''; +} diff --git a/tests/modules/programs/khard/multiple_accounts_expected b/tests/modules/programs/khard/multiple_accounts_expected new file mode 100644 index 00000000..b30e2662 --- /dev/null +++ b/tests/modules/programs/khard/multiple_accounts_expected @@ -0,0 +1,11 @@ +[addressbooks] +[[test1]] +path = /home/hm-user/.contacts/test1 + +[[test2]] +path = /home/hm-user/.contacts/test2 + + +[general] +default_action=list +