Merge pull request 'Chore(complete_machines): simplify by using inventoryStore' (#3929) from hsjobeki/cleanups into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3929
This commit is contained in:
@@ -194,7 +194,8 @@ in
|
|||||||
visible = false;
|
visible = false;
|
||||||
# ClanInternals
|
# ClanInternals
|
||||||
type = types.submodule {
|
type = types.submodule {
|
||||||
freeformType = types.attrsOf types.raw;
|
# Uncomment this if you want to add more stuff while debugging
|
||||||
|
# freeformType = types.attrsOf types.raw;
|
||||||
options = {
|
options = {
|
||||||
# Those options are interfaced by the CLI
|
# Those options are interfaced by the CLI
|
||||||
# We don't specify the type here, for better performance.
|
# We don't specify the type here, for better performance.
|
||||||
|
|||||||
@@ -242,14 +242,14 @@ in
|
|||||||
|
|
||||||
clanInternals = {
|
clanInternals = {
|
||||||
inherit inventoryClass;
|
inherit inventoryClass;
|
||||||
|
# TODO: remove this after a month or so
|
||||||
|
# This is here for backwards compatibility for older CLI versions
|
||||||
inventory = config.inventory;
|
inventory = config.inventory;
|
||||||
|
|
||||||
# TODO: unify this interface
|
# TODO: unify this interface
|
||||||
# We should have only clan.modules. (consistent with clan.templates)
|
# We should have only clan.modules. (consistent with clan.templates)
|
||||||
inherit (clan-core) clanModules;
|
inherit (clan-core) clanModules;
|
||||||
|
|
||||||
templates = config.templates;
|
|
||||||
|
|
||||||
secrets = config.secrets;
|
secrets = config.secrets;
|
||||||
|
|
||||||
# machine specifics
|
# machine specifics
|
||||||
|
|||||||
@@ -49,19 +49,10 @@ def complete_machines(
|
|||||||
flake = clan_dir_result
|
flake = clan_dir_result
|
||||||
else:
|
else:
|
||||||
flake = "."
|
flake = "."
|
||||||
services_result = json.loads(
|
|
||||||
run(
|
|
||||||
nix_eval(
|
|
||||||
flags=[
|
|
||||||
f"{flake}#clanInternals.machines.x86_64-linux",
|
|
||||||
"--apply",
|
|
||||||
"builtins.attrNames",
|
|
||||||
],
|
|
||||||
),
|
|
||||||
).stdout.strip()
|
|
||||||
)
|
|
||||||
|
|
||||||
machines.extend(services_result)
|
inventory = InventoryStore(Flake(str(flake))).read()
|
||||||
|
machines.extend(inventory.get("machines", {}).keys())
|
||||||
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -792,62 +792,6 @@ class Flake:
|
|||||||
if self.flake_cache_path:
|
if self.flake_cache_path:
|
||||||
self._cache.save_to_file(self.flake_cache_path)
|
self._cache.save_to_file(self.flake_cache_path)
|
||||||
|
|
||||||
def uncached_nix_eval_with_args(
|
|
||||||
self,
|
|
||||||
attr_path: str,
|
|
||||||
f_args: dict[str, str],
|
|
||||||
nix_options: list[str] | None = None,
|
|
||||||
) -> str:
|
|
||||||
"""
|
|
||||||
Calls a nix function with the provided arguments 'f_args'
|
|
||||||
The argument must be an attribute set.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
attr_path (str): The attribute path to the nix function
|
|
||||||
f_args (dict[str, nix_expr]): A python dictionary mapping from the name of the argument to a raw nix expression.
|
|
||||||
|
|
||||||
Example
|
|
||||||
|
|
||||||
flake.uncached_nix_eval_with_args(
|
|
||||||
"clanInternals.evalServiceSchema",
|
|
||||||
{ "moduleSpec": "{ name = \"hello-world\"; input = null; }" }
|
|
||||||
)
|
|
||||||
> '{ ...JSONSchema... }'
|
|
||||||
|
|
||||||
"""
|
|
||||||
from clan_lib.cmd import Log, RunOpts, run
|
|
||||||
from clan_lib.nix import (
|
|
||||||
nix_eval,
|
|
||||||
nix_test_store,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Always prefetch, so we don't get any stale information
|
|
||||||
self.prefetch()
|
|
||||||
|
|
||||||
if nix_options is None:
|
|
||||||
nix_options = []
|
|
||||||
|
|
||||||
arg_expr = "{"
|
|
||||||
for arg_name, arg_value in f_args.items():
|
|
||||||
arg_expr += f" {arg_name} = {arg_value}; "
|
|
||||||
arg_expr += "}"
|
|
||||||
|
|
||||||
nix_code = f"""
|
|
||||||
let
|
|
||||||
flake = builtins.getFlake "path:{self.store_path}?narHash={self.hash}";
|
|
||||||
in
|
|
||||||
flake.{attr_path} {arg_expr}
|
|
||||||
"""
|
|
||||||
if tmp_store := nix_test_store():
|
|
||||||
nix_options += ["--store", str(tmp_store)]
|
|
||||||
nix_options.append("--impure")
|
|
||||||
|
|
||||||
output = run(
|
|
||||||
nix_eval(["--expr", nix_code, *nix_options]), RunOpts(log=Log.NONE)
|
|
||||||
).stdout.strip()
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
||||||
def precache(
|
def precache(
|
||||||
self,
|
self,
|
||||||
selectors: list[str],
|
selectors: list[str],
|
||||||
|
|||||||
Reference in New Issue
Block a user