From 6fc27d402b746fdaa71ac0fb02652fe6484932bb Mon Sep 17 00:00:00 2001 From: DavHau Date: Tue, 17 Sep 2024 21:11:27 +0200 Subject: [PATCH] vars: allow setting a default for values --- nixosModules/clanCore/vars/public/in_repo.nix | 48 ++++++++++++------- pkgs/clan-cli/tests/test_vars.py | 41 ++++++++++++++++ 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/nixosModules/clanCore/vars/public/in_repo.nix b/nixosModules/clanCore/vars/public/in_repo.nix index cc49a3e4e..f2fafa58b 100644 --- a/nixosModules/clanCore/vars/public/in_repo.nix +++ b/nixosModules/clanCore/vars/public/in_repo.nix @@ -1,20 +1,34 @@ { config, lib, ... }: +let + inherit (lib) + mkOptionDefault + mkIf + readFile + pathExists + ; +in { - config.clan.core.vars.settings = - lib.mkIf (config.clan.core.vars.settings.publicStore == "in_repo") - { - publicModule = "clan_cli.vars.public_modules.in_repo"; - fileModule = file: { - path = lib.mkIf (file.config.secret == false) ( - if file.config.share then - (config.clan.core.clanDir + "/vars/shared/${file.config.generatorName}/${file.config.name}/value") - else - ( - config.clan.core.clanDir - + "/vars/per-machine/${config.clan.core.machineName}/${file.config.generatorName}/${file.config.name}/value" - ) - ); - value = lib.mkIf (file.config.secret == false) (lib.readFile file.config.path); - }; - }; + config.clan.core.vars.settings = mkIf (config.clan.core.vars.settings.publicStore == "in_repo") { + publicModule = "clan_cli.vars.public_modules.in_repo"; + fileModule = file: { + path = mkIf (file.config.secret == false) ( + if file.config.share then + (config.clan.core.clanDir + "/vars/shared/${file.config.generatorName}/${file.config.name}/value") + else + ( + config.clan.core.clanDir + + "/vars/per-machine/${config.clan.core.machineName}/${file.config.generatorName}/${file.config.name}/value" + ) + ); + value = mkIf (file.config.secret == false) ( + # dynamically adjust priority to allow overriding with mkDefault in case the file is not found + if (pathExists file.config.path) then + # if the file is found it should have normal priority + readFile file.config.path + else + # if the file is not found, we want to downgrade the priority, to allow overriding via mkDefault + mkOptionDefault (readFile file.config.path) + ); + }; + }; } diff --git a/pkgs/clan-cli/tests/test_vars.py b/pkgs/clan-cli/tests/test_vars.py index 378b32f40..9a35147d5 100644 --- a/pkgs/clan-cli/tests/test_vars.py +++ b/pkgs/clan-cli/tests/test_vars.py @@ -595,3 +595,44 @@ def test_commit_message( commit_message == "Update vars via generator my_secret_generator for machine my_machine" ) + + +@pytest.mark.impure +def test_default_value( + monkeypatch: pytest.MonkeyPatch, + temporary_home: Path, +) -> None: + config = nested_dict() + my_generator = config["clan"]["core"]["vars"]["generators"]["my_generator"] + my_generator["files"]["my_value"]["secret"] = False + my_generator["files"]["my_value"]["value"]["_type"] = "override" + my_generator["files"]["my_value"]["value"]["priority"] = 1000 # mkDefault + my_generator["files"]["my_value"]["value"]["content"] = "foo" + my_generator["script"] = "echo -n hello > $out/my_value" + flake = generate_flake( + temporary_home, + flake_template=CLAN_CORE / "templates" / "minimal", + machine_configs={"my_machine": config}, + monkeypatch=monkeypatch, + ) + monkeypatch.chdir(flake.path) + # ensure evaluating the default value works without generating the value + value_eval = run( + nix_eval( + [ + f"{flake.path}#nixosConfigurations.my_machine.config.clan.core.vars.generators.my_generator.files.my_value.value", + ] + ) + ).stdout.strip() + assert json.loads(value_eval) == "foo" + # generate + cli.run(["vars", "generate", "--flake", str(flake.path), "my_machine"]) + # ensure the value is set correctly + value_eval = run( + nix_eval( + [ + f"{flake.path}#nixosConfigurations.my_machine.config.clan.core.vars.generators.my_generator.files.my_value.value", + ] + ) + ).stdout.strip() + assert json.loads(value_eval) == "hello"