From a54e05bc12d88ff2df941d0dc1183cb5235fa438 Mon Sep 17 00:00:00 2001 From: Nikita Pedorich Date: Sun, 18 Feb 2024 23:14:41 +0100 Subject: [PATCH] tealdeer: module improvements - Cache update on HM activation removed - freeformType settings - More tests added - Platform-dependent tests - Maintainer added --- modules/programs/tealdeer.nix | 81 ++++++++++++------- .../programs/tealdeer/custom-settings.nix | 35 ++++++++ .../programs/tealdeer/default-settings.nix | 12 ++- tests/modules/programs/tealdeer/default.nix | 5 +- 4 files changed, 101 insertions(+), 32 deletions(-) create mode 100644 tests/modules/programs/tealdeer/custom-settings.nix diff --git a/modules/programs/tealdeer.nix b/modules/programs/tealdeer.nix index 3f03c715..55b6c22b 100644 --- a/modules/programs/tealdeer.nix +++ b/modules/programs/tealdeer.nix @@ -4,31 +4,64 @@ with lib; let cfg = config.programs.tealdeer; - tomlFormat = pkgs.formats.toml { }; - configDir = if pkgs.stdenv.isDarwin then "Library/Application Support" else config.xdg.configHome; + tomlFormat = pkgs.formats.toml { }; + + settingsFormat = let + updatesSection = types.submodule { + options = { + auto_update = mkEnableOption "auto-update"; + + auto_update_interval_hours = mkOption { + type = types.ints.positive; + default = 720; + example = literalExpression "24"; + description = '' + Duration, since the last cache update, after which the cache will be refreshed. + This parameter is ignored if {var}`auto_update` is set to `false`. + ''; + }; + }; + }; + in types.submodule { + freeformType = tomlFormat.type; + options = { + updates = mkOption { + type = updatesSection; + default = { }; + description = '' + Tealdeer can refresh the cache automatically when it is outdated. + This behavior can be configured in the updates section. + ''; + }; + }; + }; + in { - meta.maintainers = [ ]; + meta.maintainers = [ hm.maintainers.pedorich-n ]; + + imports = [ + (mkRemovedOptionModule [ "programs" "tealdeer" "updateOnActivation" ] '' + Updating tealdeer's cache requires network access. + The activation script should be fast and idempotent, so the option was removed. + Please use + + `programs.teadleer.settings.updates.auto_update = true` + + instead, to make sure tealdeer's cache is updated periodically. + '') + ]; options.programs.tealdeer = { enable = mkEnableOption "Tealdeer"; - updateOnActivation = mkOption { - type = with types; bool; - default = true; - description = '' - Whether to update tealdeer's cache on activation. - ''; - }; - settings = mkOption { - type = tomlFormat.type; - default = { }; - defaultText = literalExpression "{ }"; + type = types.nullOr settingsFormat; + default = null; example = literalExpression '' { display = { @@ -43,10 +76,8 @@ in { description = '' Configuration written to {file}`$XDG_CONFIG_HOME/tealdeer/config.toml` on Linux or - {file}`$HOME/Library/Application Support/tealdeer/config.toml` - on Darwin. See - - for more information. + {file}`$HOME/Library/Application Support/tealdeer/config.toml` on Darwin. + See for more information. ''; }; }; @@ -54,15 +85,9 @@ in { config = mkIf cfg.enable { home.packages = [ pkgs.tealdeer ]; - home.file."${configDir}/tealdeer/config.toml" = mkIf (cfg.settings != { }) { - source = tomlFormat.generate "tealdeer-config" cfg.settings; - }; - - home.activation = mkIf cfg.updateOnActivation { - tealdeerCache = hm.dag.entryAfter [ "linkGeneration" ] '' - $VERBOSE_ECHO "Rebuilding tealdeer cache" - $DRY_RUN_CMD ${getExe pkgs.tealdeer} --update - ''; - }; + home.file."${configDir}/tealdeer/config.toml" = + mkIf (cfg.settings != null && cfg.settings != { }) { + source = tomlFormat.generate "tealdeer-config" cfg.settings; + }; }; } diff --git a/tests/modules/programs/tealdeer/custom-settings.nix b/tests/modules/programs/tealdeer/custom-settings.nix new file mode 100644 index 00000000..aa9768cf --- /dev/null +++ b/tests/modules/programs/tealdeer/custom-settings.nix @@ -0,0 +1,35 @@ +{ config, pkgs, ... }: { + config = { + programs.tealdeer = { + package = config.lib.test.mkStubPackage { name = "tldr"; }; + enable = true; + settings = { + updates = { + auto_update = true; + auto_update_interval_hours = 72; + }; + display = { use_pager = false; }; + }; + }; + + nmt.script = let + expectedConfDir = if pkgs.stdenv.isDarwin then + "Library/Application Support" + else + ".config"; + expectedConfigPath = "home-files/${expectedConfDir}/tealdeer/config.toml"; + in '' + assertFileExists "${expectedConfigPath}" + assertFileContent "${expectedConfigPath}" ${ + pkgs.writeText "tealdeer.config-custom.expected" '' + [display] + use_pager = false + + [updates] + auto_update = true + auto_update_interval_hours = 72 + '' + } + ''; + }; +} diff --git a/tests/modules/programs/tealdeer/default-settings.nix b/tests/modules/programs/tealdeer/default-settings.nix index 0044ca8f..438b6edd 100644 --- a/tests/modules/programs/tealdeer/default-settings.nix +++ b/tests/modules/programs/tealdeer/default-settings.nix @@ -1,12 +1,18 @@ -{ config, ... }: { +{ config, pkgs, ... }: { config = { programs.tealdeer = { package = config.lib.test.mkStubPackage { name = "tldr"; }; enable = true; }; - nmt.script = '' - assertFileRegex activate '/nix/store/.*tealdeer.*/bin/tldr --update' + nmt.script = let + expectedConfDir = if pkgs.stdenv.isDarwin then + "Library/Application Support" + else + ".config"; + expectedConfigPath = "home-files/${expectedConfDir}/tealdeer/config.toml"; + in '' + assertPathNotExists "${expectedConfigPath}" ''; }; } diff --git a/tests/modules/programs/tealdeer/default.nix b/tests/modules/programs/tealdeer/default.nix index e9deed93..147a664e 100644 --- a/tests/modules/programs/tealdeer/default.nix +++ b/tests/modules/programs/tealdeer/default.nix @@ -1 +1,4 @@ -{ tealdeer-default-settings = ./default-settings.nix; } +{ + tealdeer-default-settings = ./default-settings.nix; + tealdeer-custom-settings = ./custom-settings.nix; +}