tealdeer: module improvements

- Cache update on HM activation removed
- freeformType settings
- More tests added
- Platform-dependent tests
- Maintainer added
This commit is contained in:
Nikita Pedorich 2024-02-18 23:14:41 +01:00 committed by GitHub
parent 738527f866
commit a54e05bc12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 101 additions and 32 deletions

View file

@ -4,31 +4,64 @@ with lib;
let let
cfg = config.programs.tealdeer; cfg = config.programs.tealdeer;
tomlFormat = pkgs.formats.toml { };
configDir = if pkgs.stdenv.isDarwin then configDir = if pkgs.stdenv.isDarwin then
"Library/Application Support" "Library/Application Support"
else else
config.xdg.configHome; 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 { 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 = { options.programs.tealdeer = {
enable = mkEnableOption "Tealdeer"; enable = mkEnableOption "Tealdeer";
updateOnActivation = mkOption {
type = with types; bool;
default = true;
description = ''
Whether to update tealdeer's cache on activation.
'';
};
settings = mkOption { settings = mkOption {
type = tomlFormat.type; type = types.nullOr settingsFormat;
default = { }; default = null;
defaultText = literalExpression "{ }";
example = literalExpression '' example = literalExpression ''
{ {
display = { display = {
@ -43,10 +76,8 @@ in {
description = '' description = ''
Configuration written to Configuration written to
{file}`$XDG_CONFIG_HOME/tealdeer/config.toml` on Linux or {file}`$XDG_CONFIG_HOME/tealdeer/config.toml` on Linux or
{file}`$HOME/Library/Application Support/tealdeer/config.toml` {file}`$HOME/Library/Application Support/tealdeer/config.toml` on Darwin.
on Darwin. See See <https://dbrgn.github.io/tealdeer/config.html> for more information.
<https://dbrgn.github.io/tealdeer/config.html>
for more information.
''; '';
}; };
}; };
@ -54,15 +85,9 @@ in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
home.packages = [ pkgs.tealdeer ]; home.packages = [ pkgs.tealdeer ];
home.file."${configDir}/tealdeer/config.toml" = mkIf (cfg.settings != { }) { home.file."${configDir}/tealdeer/config.toml" =
source = tomlFormat.generate "tealdeer-config" cfg.settings; mkIf (cfg.settings != null && 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
'';
};
}; };
} }

View file

@ -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
''
}
'';
};
}

View file

@ -1,12 +1,18 @@
{ config, ... }: { { config, pkgs, ... }: {
config = { config = {
programs.tealdeer = { programs.tealdeer = {
package = config.lib.test.mkStubPackage { name = "tldr"; }; package = config.lib.test.mkStubPackage { name = "tldr"; };
enable = true; enable = true;
}; };
nmt.script = '' nmt.script = let
assertFileRegex activate '/nix/store/.*tealdeer.*/bin/tldr --update' expectedConfDir = if pkgs.stdenv.isDarwin then
"Library/Application Support"
else
".config";
expectedConfigPath = "home-files/${expectedConfDir}/tealdeer/config.toml";
in ''
assertPathNotExists "${expectedConfigPath}"
''; '';
}; };
} }

View file

@ -1 +1,4 @@
{ tealdeer-default-settings = ./default-settings.nix; } {
tealdeer-default-settings = ./default-settings.nix;
tealdeer-custom-settings = ./custom-settings.nix;
}