Compare commits

...

1 Commits

Author SHA1 Message Date
DavHau
9043365734 WIP 2025-02-05 11:52:23 +07:00
2 changed files with 40 additions and 7 deletions

View File

@@ -292,8 +292,24 @@ class Flake:
in
flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (builtins.toJSON [ ({" ".join([f'flake.clanInternals.lib.select "{attr}" flake' for attr in selectors])}) ])
"""
build_output = run(nix_build(["--expr", nix_code])).stdout.strip()
cmd = nix_build(["--expr", nix_code, '-vvv'])
# breakpoint()
import subprocess
print("running nix build")
# breakpoint()
# build_output = run(cmd).stdout.strip()
import time
t = time.time()
# breakpoint()
import os
print(os.environ["HOME"])
build_output = subprocess.run(cmd, capture_output=True, text=True).stdout.strip()
print(f"ran nix build 1: {time.time() - t}")
t = time.time()
build_output = subprocess.run(cmd, capture_output=True, text=True).stdout.strip()
print(f"ran nix build 2: {time.time() - t}")
outputs = json.loads(Path(build_output).read_text())
print("got output")
if len(outputs) != len(selectors):
msg = f"flake_prepare_cache: Expected {len(outputs)} outputs, got {len(outputs)}"
raise ClanError(msg)
@@ -303,5 +319,7 @@ class Flake:
def select(self, selector: str) -> Any:
if not self.cache.is_cached(selector):
log.info(f"Cache miss for {selector}")
print("preparing cache")
self.prepare_cache([selector])
print("cache prepared")
return self.cache.select(selector)

View File

@@ -1,15 +1,30 @@
from clan_cli.flake import FlakeCacheEntry
from clan_cli.flake import FlakeCacheEntry, Flake
from fixtures_flakes import ClanFlake
from helpers import cli
import pytest
def test_flake_caching(test_flake: ClanFlake) -> None:
@pytest.mark.with_core
def test_flake_caching(
flake: ClanFlake
) -> None:
testdict = {"x": {"y": [123, 345, 456], "z": "bla"}}
test_cache = FlakeCacheEntry(testdict, [])
assert test_cache["x"]["z"].value == "bla"
assert test_cache.is_cached(["x", "z"])
assert test_cache.select(["x", "y", 0]) == 123
assert not test_cache.is_cached(["x", "z", 1])
# TODO check this, but test_flake is not a real clan flake (no clan-core, no clanInternals)
# cmd.run(["nix", "flake", "lock"], cmd.RunOpts(cwd=test_flake.path))
# flake = Flake(str(test_flake.path))
# hostnames = flake.select("nixosConfigurations.*.config.networking.hostName")
# TODO check this, but flake is not a real clan flake (no clan-core, no clanInternals)
# cli.run(["nix", "flake", "lock"], cmd.RunOpts(cwd=flake.path))
m1 = flake.machines["machine1"]
m1["nixpkgs"]["hostPlatform"] = "x86_64-linux"
# flake.machines["machine2"] = m1.copy()
# flake.machines["machine3"] = m1.copy()
flake.refresh()
flake_ = Flake(str(flake.path))
import os
os.environ["HOME"] = "/home/grmpf"
hostnames = flake_.select("nixosConfigurations.*.config.networking.hostName")
# assert hostnames == {"machine1": "machine1", "machine2": "machine2", "machine3": "machine3"}
assert hostnames == {"machine1": "machine1"}