vars: allow setting a default for values
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
{ 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")
|
||||
{
|
||||
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 = lib.mkIf (file.config.secret == false) (
|
||||
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
|
||||
@@ -14,7 +20,15 @@
|
||||
+ "/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);
|
||||
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)
|
||||
);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user