Something went wrong. Try again.
nix (hat) atelier?!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375from atelier.nix import _build_select, _eval_command, clean_error, is_skippable, to_jobfrom atelier.types import MAX_RECURSE_DEPTH
def test_per_system_leaf() -> None: job = to_job( { "attrPath": ["legacyPackages.x86_64-linux", "caddy"], "drvPath": "/nix/store/x.drv", "system": "x86_64-linux", } ) assert job.path == "legacyPackages.x86_64-linux.caddy" assert job.system == "x86_64-linux" assert job.installable == ".#legacyPackages.x86_64-linux.caddy" assert job.error is None
def test_nested_scope_leaf() -> None: job = to_job( { "attrPath": ["legacyPackages.x86_64-linux", "ocamlPackages", "dune"], "drvPath": "/nix/store/y.drv", } ) assert job.path == "legacyPackages.x86_64-linux.ocamlPackages.dune" assert job.system == "x86_64-linux" assert job.installable == ".#legacyPackages.x86_64-linux.ocamlPackages.dune"
def test_error_without_system_field_derives_from_path() -> None: job = to_job( { "attrPath": ["legacyPackages.aarch64-darwin", "bird3"], "error": "boom", "fatal": False, } ) assert job.system == "aarch64-darwin" assert job.installable == "" assert job.error == "boom"
def test_formatter_leaf_derives_system_and_installable() -> None: # a leaf set roots the derivation at "<set>.<system>", so attrPath has a # single element and the system comes from the path's second segment; the # "system" field is omitted so the test proves the path derivation job = to_job( { "attrPath": ["formatter.x86_64-linux"], "drvPath": "/nix/store/f.drv", } ) assert job.path == "formatter.x86_64-linux" assert job.system == "x86_64-linux" assert job.installable == ".#formatter.x86_64-linux"
def test_config_uses_toplevel_installable() -> None: job = to_job( { "attrPath": ["nixosConfigurations", "baldy"], "drvPath": "/nix/store/z.drv", "system": "x86_64-linux", } ) assert job.path == "nixosConfigurations.baldy" assert job.system == "x86_64-linux" assert job.installable == ".#nixosConfigurations.baldy.config.system.build.toplevel"
def test_to_job_marks_cached_when_substitutable() -> None: job = to_job( { "attrPath": ["packages.x86_64-linux", "hello"], "drvPath": "/nix/store/x.drv", "system": "x86_64-linux", "cacheStatus": "cached", } ) assert job.cached is True
def test_to_job_not_cached_when_not_built() -> None: job = to_job( { "attrPath": ["packages.x86_64-linux", "hello"], "drvPath": "/nix/store/x.drv", "cacheStatus": "notBuilt", } ) assert job.cached is False
def test_to_job_local_is_not_cached() -> None: # "local" is present only in this runner's store, not in a shared cache, so a # build on a different runner could not substitute it; it must still build job = to_job( { "attrPath": ["packages.x86_64-linux", "hello"], "drvPath": "/nix/store/x.drv", "cacheStatus": "local", } ) assert job.cached is False
def test_to_job_not_cached_when_status_absent() -> None: job = to_job( {"attrPath": ["packages.x86_64-linux", "hello"], "drvPath": "/nix/store/x.drv"} ) assert job.cached is False
def test_eval_command_requests_cache_status() -> None: assert "--check-cache-status" in _eval_command("flake#", "SELECT", 2, frozenset())
def test_eval_command_passes_sorted_substituters() -> None: cmd = _eval_command( "flake#", "SELECT", 2, frozenset({"https://cache.ysun.co", "https://cache.nixos.org"}), ) value = cmd[cmd.index("extra-substituters") + 1] assert value == "https://cache.nixos.org https://cache.ysun.co" # an untrusted cache's signature is irrelevant to a mere existence check, so # the check must not require a trusted signature or it would miss the path assert cmd[cmd.index("require-sigs") + 1] == "false"
def test_eval_command_omits_substituters_when_none() -> None: assert "extra-substituters" not in _eval_command("flake#", "SELECT", 2, frozenset())
def test_unsupported_platform_is_skippable() -> None: assert is_skippable("error: not available on the requested hostPlatform")
def test_broken_is_skippable() -> None: assert is_skippable("Package foo is marked as broken")
def test_syntax_error_is_not_skippable() -> None: assert not is_skippable("error: syntax error, unexpected end of file")
def test_unsupported_argument_is_not_skippable() -> None: assert not is_skippable("error: function call has an unsupported argument")
def test_missing_broken_attr_is_not_skippable() -> None: assert not is_skippable("error: attribute 'broken' missing")
def test_clean_error_keeps_innermost_message() -> None: raw = "error:\n in the assert\n error: Refusing to evaluate package 'x'" assert clean_error(raw) == "error: Refusing to evaluate package 'x'"
def test_clean_error_neutralises_workflow_commands() -> None: assert "::" not in clean_error("boom ::stop-commands::abcd") # odd-length colon runs must not leave a "::" a single replace would miss assert "::" not in clean_error("boom :::warning file=x::spoof")
def test_build_select_rejects_unallowlisted_values() -> None: import pytest
with pytest.raises(ValueError, match="unknown system"): _build_select(['x"; evil; "y'], ["legacyPackages"], []) with pytest.raises(ValueError, match="unknown output set"): _build_select(["x86_64-linux"], ["evil"], []) with pytest.raises(ValueError, match="unknown output set"): _build_select(["x86_64-linux"], [], [], ["evil"])
def test_build_select_injects_allowlisted_lists() -> None: expr = _build_select( ["x86_64-linux"], ["legacyPackages"], ["nixosConfigurations"], ["formatter"] ) assert 'systems = [ "x86_64-linux" ]' in expr assert 'perSystemSets = [ "legacyPackages" ]' in expr assert 'configSets = [ "nixosConfigurations" ]' in expr assert 'leafSets = [ "formatter" ]' in expr
def test_build_select_sanitizes_per_system_scopes() -> None: # the per system rooting must prune scope plumbing before force-recurse: drop # functions and recurseForDerivations=false aliases, drop the named attrset # plumbing (lib), and bound recursion to the include depth expr = _build_select(["x86_64-linux"], ["legacyPackages"], [], max_depth=4) assert "sanitize" in expr assert "builtins.isFunction raw" in expr assert "recurseForDerivations or true) == false" in expr assert '"lib"' in expr # maxDepth 4 leaves a recursion budget of one level past the depth-3 children assert "maxDepth = 4;" in expr
def test_build_select_clamps_depth_to_cap() -> None: # a caller asking for more depth than the hard cap is clamped, so a pathological # include can never request an unbounded recursion expr = _build_select( ["x86_64-linux"], ["legacyPackages"], [], max_depth=MAX_RECURSE_DEPTH + 99 ) assert f"maxDepth = {MAX_RECURSE_DEPTH};" in expr
def test_build_select_guards_leaf_derivations() -> None: # a non derivation leaf value must throw per attribute, not be recursed # into (its children's 3-segment paths could never match "formatter.*") expr = _build_select(["x86_64-linux"], [], [], ["formatter"]) assert 'if (v.type or null) == "derivation" then v' in expr assert "is not a derivation" in expr
def test_build_select_prunes_excluded_trees() -> None: # "*" rules fold into each requested system and a system nobody asked for # is dropped, so the embedded nix reads one tree per rooted "<set>.<sys>" expr = _build_select( ["x86_64-linux", "aarch64-darwin"], ["legacyPackages"], [], excludes={ "legacyPackages": { "*": {"spotify": True, "ocamlPackages": {"yocaml_unix": True}}, "aarch64-darwin": {"bird3": True}, "aarch64-linux": {"cider-2": True}, } }, ) assert "removeAttrs" in expr assert ( '"x86_64-linux" = { "ocamlPackages" = { "yocaml_unix" = true; };' ' "spotify" = true; };' in expr ) assert ( '"aarch64-darwin" = { "bird3" = true;' ' "ocamlPackages" = { "yocaml_unix" = true; }; "spotify" = true; };' in expr ) assert "cider-2" not in expr
def test_build_select_escapes_exclude_names() -> None: # a crafted attribute name must stay inside its nix string, not break out expr = _build_select( ["x86_64-linux"], ["legacyPackages"], [], excludes={"legacyPackages": {"*": {'evil"; x': True}}}, ) assert '"evil\\"; x"' in expr
def test_build_select_rejects_unknown_exclude_set() -> None: import pytest
with pytest.raises(ValueError, match="unknown output set"): _build_select( ["x86_64-linux"], ["legacyPackages"], [], excludes={"bogus": {"*": {"x": True}}}, )
def test_select_drops_excluded_subtrees_without_forcing() -> None: # the excluded attributes throw when forced, so the eval succeeding at all # proves the prune happens before classification can touch them, and the # surviving attrNames prove exactly the excluded names disappeared import json import shutil import subprocess
import pytest
if shutil.which("nix") is None: pytest.skip("nix not on PATH") select = _build_select( ["x86_64-linux"], ["legacyPackages"], [], excludes={ "legacyPackages": { "*": { "@MAXDEPTH@": True, "spotify": True, "ocamlPackages": {"yocaml_unix": True}, } } }, ) flake = """ { outputs.legacyPackages.x86_64-linux = { caddy = { type = "derivation"; name = "caddy"; }; "@MAXDEPTH@" = throw "forced a token-shaped excluded attribute"; spotify = throw "forced an excluded attribute"; ocamlPackages = { yocaml = { type = "derivation"; name = "yocaml"; }; yocaml_unix = throw "forced an excluded attribute"; }; }; } """ expr = f""" let s = ({select}) ({flake}); r = s."legacyPackages.x86_64-linux"; in {{ top = builtins.attrNames r; ocaml = builtins.attrNames r.ocamlPackages; }} """ proc = subprocess.run( ["nix", "eval", "--json", "--expr", expr], capture_output=True, text=True, check=True, ) assert json.loads(proc.stdout) == { "top": ["caddy", "ocamlPackages"], "ocaml": ["yocaml"], }
def test_select_drops_excluded_configs_without_forcing() -> None: import json import shutil import subprocess
import pytest
if shutil.which("nix") is None: pytest.skip("nix not on PATH") select = _build_select( ["x86_64-linux"], [], ["nixosConfigurations"], excludes={"nixosConfigurations": {"*": {"iso": True}}}, ) flake = """ { outputs.nixosConfigurations = { baldy.config.system.build.toplevel = { type = "derivation"; name = "toplevel"; }; iso = throw "forced an excluded configuration"; }; } """ expr = f""" let s = ({select}) ({flake}); r = s.nixosConfigurations; in {{ hosts = builtins.attrNames r; kept = r.baldy.name; }} """ proc = subprocess.run( ["nix", "eval", "--json", "--expr", expr], capture_output=True, text=True, check=True, ) assert json.loads(proc.stdout) == {"hosts": ["baldy"], "kept": "toplevel"}
def test_select_template_resource_parses() -> None: import shutil import subprocess from importlib import resources
import pytest
if shutil.which("nix-instantiate") is None: pytest.skip("nix-instantiate not on PATH") with resources.as_file(resources.files("atelier") / "select.nix") as path: subprocess.run( ["nix-instantiate", "--parse", str(path)], capture_output=True, check=True )