move clanName into nixos machine configuration

This commit is contained in:
Jörg Thalheim 2023-12-08 16:02:54 +01:00 committed by Mic92
parent c22280b864
commit 4aa60317fa
5 changed files with 13 additions and 25 deletions

View File

@ -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;

View File

@ -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 = ''

View File

@ -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 {

View File

@ -11,6 +11,7 @@ from ..nix import nix_config, nix_eval
@dataclass
class VmConfig:
clan_name: str
flake_url: str | Path
flake_attr: str

View File

@ -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(