From 4aa60317fab4be40af3cf8131f48e8bfbfc5a78f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 8 Dec 2023 16:02:54 +0100 Subject: [PATCH] move clanName into nixos machine configuration --- lib/build-clan/default.nix | 2 +- nixosModules/clanCore/flake-module.nix | 6 ++++++ nixosModules/clanCore/vm.nix | 3 ++- pkgs/clan-cli/clan_cli/vms/inspect.py | 1 + pkgs/clan-cli/clan_cli/vms/run.py | 26 +++----------------------- 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/lib/build-clan/default.nix b/lib/build-clan/default.nix index 00cd42766..45bd4a249 100644 --- a/lib/build-clan/default.nix +++ b/lib/build-clan/default.nix @@ -41,6 +41,7 @@ let (machines.${name} or { }) { clanCore.machineName = name; + clanCore.clanName = clanName; clanCore.clanDir = directory; nixpkgs.hostPlatform = lib.mkForce system; @@ -80,7 +81,6 @@ in clanInternals = { machines = configsPerSystem; - clanName = clanName; all-machines-json = lib.mapAttrs (system: configs: nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs)) configsPerSystem; diff --git a/nixosModules/clanCore/flake-module.nix b/nixosModules/clanCore/flake-module.nix index 60ea9f333..584aaa4b6 100644 --- a/nixosModules/clanCore/flake-module.nix +++ b/nixosModules/clanCore/flake-module.nix @@ -20,6 +20,12 @@ default = self.lib.jsonschema.parseOptions options.clan; }; options.clanCore = { + clanName = lib.mkOption { + type = lib.types.str; + description = '' + the name of the clan + ''; + }; clanDir = lib.mkOption { type = lib.types.either lib.types.path lib.types.str; description = '' diff --git a/nixosModules/clanCore/vm.nix b/nixosModules/clanCore/vm.nix index d9d4e3c39..181e3b188 100644 --- a/nixosModules/clanCore/vm.nix +++ b/nixosModules/clanCore/vm.nix @@ -52,8 +52,9 @@ in system.clan.vm = { # for clan vm inspect config = { - inherit (config.clan.virtualisation) cores graphics; + clan_name = config.clanCore.clanName; memory_size = config.clan.virtualisation.memorySize; + inherit (config.clan.virtualisation) cores graphics; }; # for clan vm create create = pkgs.writeText "vm.json" (builtins.toJSON { diff --git a/pkgs/clan-cli/clan_cli/vms/inspect.py b/pkgs/clan-cli/clan_cli/vms/inspect.py index 67c743fdf..ae0efb377 100644 --- a/pkgs/clan-cli/clan_cli/vms/inspect.py +++ b/pkgs/clan-cli/clan_cli/vms/inspect.py @@ -11,6 +11,7 @@ from ..nix import nix_config, nix_eval @dataclass class VmConfig: + clan_name: str flake_url: str | Path flake_attr: str diff --git a/pkgs/clan-cli/clan_cli/vms/run.py b/pkgs/clan-cli/clan_cli/vms/run.py index 1fdbb526c..b28bf1ebe 100644 --- a/pkgs/clan-cli/clan_cli/vms/run.py +++ b/pkgs/clan-cli/clan_cli/vms/run.py @@ -12,7 +12,7 @@ from typing import IO from ..dirs import module_root from ..errors import ClanError -from ..nix import nix_build, nix_config, nix_eval, nix_shell +from ..nix import nix_build, nix_config, nix_shell from .inspect import VmConfig, inspect_vm log = logging.getLogger(__name__) @@ -126,25 +126,8 @@ def get_vm_create_info(vm: VmConfig, nix_options: list[str]) -> dict[str, str]: raise ClanError(f"Failed to parse vm config: {e}") -def get_clan_name(vm: VmConfig, nix_options: list[str]) -> str: - clan_dir = vm.flake_url - cmd = nix_eval([f"{clan_dir}#clanInternals.clanName"]) + nix_options - proc = subprocess.run( - cmd, - stdout=subprocess.PIPE, - check=False, - text=True, - ) - if proc.returncode != 0: - raise ClanError( - f"Failed to get clan name: {shlex.join(cmd)} failed with: {proc.returncode}" - ) - return proc.stdout.strip().strip('"') - - def generate_secrets( vm: VmConfig, - clan_name: str, nixos_config: dict[str, str], tmpdir: Path, log_fd: IO[str] | None, @@ -162,7 +145,7 @@ def generate_secrets( if isinstance(vm.flake_url, Path) and vm.flake_url.is_dir(): if Path(vm.flake_url).is_dir(): subprocess.run( - [nixos_config["generateSecrets"], clan_name], + [nixos_config["generateSecrets"], vm.clan_name], env=env, check=False, stdout=log_fd, @@ -243,9 +226,6 @@ def run_vm( # TODO: We should get this from the vm argument nixos_config = get_vm_create_info(vm, nix_options) - clan_name = get_clan_name(vm, nix_options) - - log.debug(f"Building VM for clan name: {clan_name}") flake_dir = Path(vm.flake_url) flake_dir.mkdir(exist_ok=True) @@ -255,7 +235,7 @@ def run_vm( xchg_dir = tmpdir / "xchg" xchg_dir.mkdir(exist_ok=True) - secrets_dir = generate_secrets(vm, clan_name, nixos_config, tmpdir, log_fd) + secrets_dir = generate_secrets(vm, nixos_config, tmpdir, log_fd) disk_img = prepare_disk(tmpdir, log_fd) qemu_cmd = qemu_command(