clan-cli machines: cache machines_func via store

This commit is contained in:
lassulus 2024-03-01 07:37:47 +01:00
parent 8c7db195ab
commit 108a37b0a3

View File

@ -166,6 +166,7 @@ class Machine:
config = nix_config() config = nix_config()
system = config["system"] system = config["system"]
file_info = dict()
with NamedTemporaryFile(mode="w") as config_json: with NamedTemporaryFile(mode="w") as config_json:
if extra_config is not None: if extra_config is not None:
json.dump(extra_config, config_json, indent=2) json.dump(extra_config, config_json, indent=2)
@ -173,66 +174,66 @@ class Machine:
json.dump({}, config_json) json.dump({}, config_json)
config_json.flush() config_json.flush()
nar_hash = json.loads( file_info = json.loads(
run( run(
nix_eval( nix_eval(
[ [
"--impure", "--impure",
"--expr", "--expr",
f'(builtins.fetchTree {{ type = "file"; url = "file://{config_json.name}"; }}).narHash', f'let x = (builtins.fetchTree {{ type = "file"; url = "file://{config_json.name}"; }}); in {{ narHash = x.narHash; path = x.outPath; }}',
] ]
) )
).stdout.strip() ).stdout.strip()
) )
args = [] args = []
# get git commit from flake # get git commit from flake
if extra_config is not None: if extra_config is not None:
metadata = nix_metadata(self.flake_dir) metadata = nix_metadata(self.flake_dir)
url = metadata["url"] url = metadata["url"]
if "dirtyRevision" in metadata: if "dirtyRevision" in metadata:
# if not impure: # if not impure:
# raise ClanError( # raise ClanError(
# "The machine has a dirty revision, and impure mode is not allowed" # "The machine has a dirty revision, and impure mode is not allowed"
# ) # )
# else: # else:
# args += ["--impure"] # args += ["--impure"]
args += ["--impure"] args += ["--impure"]
args += [ args += [
"--expr", "--expr",
f""" f"""
((builtins.getFlake "{url}").clanInternals.machinesFunc."{system}"."{self.name}" {{ ((builtins.getFlake "{url}").clanInternals.machinesFunc."{system}"."{self.name}" {{
extraConfig = builtins.fromJSON (builtins.readFile (builtins.fetchTree {{ extraConfig = builtins.fromJSON (builtins.readFile (builtins.fetchTree {{
type = "file"; type = "file";
url = if (builtins.compareVersions builtins.nixVersion "2.19") == -1 then "{config_json.name}" else "file:{config_json.name}"; url = if (builtins.compareVersions builtins.nixVersion "2.19") == -1 then "{file_info["path"]}" else "file:{file_info["path"]}";
narHash = "{nar_hash}"; narHash = "{file_info["narHash"]}";
}})); }}));
}}).{attr} }}).{attr}
""", """,
] ]
else: else:
if isinstance(self.flake, Path): if isinstance(self.flake, Path):
if (self.flake / ".git").exists(): if (self.flake / ".git").exists():
flake = f"git+file://{self.flake}" flake = f"git+file://{self.flake}"
else:
flake = f"path:{self.flake}"
else: else:
flake = self.flake flake = f"path:{self.flake}"
args += [
f'{flake}#clanInternals.machines."{system}".{self.name}.{attr}',
*nix_options,
]
if method == "eval":
output = run(nix_eval(args)).stdout.strip()
return output
elif method == "build":
outpath = run(nix_build(args)).stdout.strip()
return Path(outpath)
else: else:
raise ValueError(f"Unknown method {method}") flake = self.flake
args += [
f'{flake}#clanInternals.machines."{system}".{self.name}.{attr}',
*nix_options,
]
if method == "eval":
output = run(nix_eval(args)).stdout.strip()
return output
elif method == "build":
outpath = run(nix_build(args)).stdout.strip()
return Path(outpath)
else:
raise ValueError(f"Unknown method {method}")
def eval_nix( def eval_nix(
self, self,