move clanName into nixos machine configuration
This commit is contained in:
parent
1496f45fe2
commit
4b9635c706
@ -41,6 +41,7 @@ let
|
|||||||
(machines.${name} or { })
|
(machines.${name} or { })
|
||||||
{
|
{
|
||||||
clanCore.machineName = name;
|
clanCore.machineName = name;
|
||||||
|
clanCore.clanName = clanName;
|
||||||
clanCore.clanDir = directory;
|
clanCore.clanDir = directory;
|
||||||
nixpkgs.hostPlatform = lib.mkForce system;
|
nixpkgs.hostPlatform = lib.mkForce system;
|
||||||
|
|
||||||
@ -80,7 +81,6 @@ in
|
|||||||
|
|
||||||
clanInternals = {
|
clanInternals = {
|
||||||
machines = configsPerSystem;
|
machines = configsPerSystem;
|
||||||
clanName = clanName;
|
|
||||||
all-machines-json = lib.mapAttrs
|
all-machines-json = lib.mapAttrs
|
||||||
(system: configs: nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs))
|
(system: configs: nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs))
|
||||||
configsPerSystem;
|
configsPerSystem;
|
||||||
|
@ -20,6 +20,12 @@
|
|||||||
default = self.lib.jsonschema.parseOptions options.clan;
|
default = self.lib.jsonschema.parseOptions options.clan;
|
||||||
};
|
};
|
||||||
options.clanCore = {
|
options.clanCore = {
|
||||||
|
clanName = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
description = ''
|
||||||
|
the name of the clan
|
||||||
|
'';
|
||||||
|
};
|
||||||
clanDir = lib.mkOption {
|
clanDir = lib.mkOption {
|
||||||
type = lib.types.either lib.types.path lib.types.str;
|
type = lib.types.either lib.types.path lib.types.str;
|
||||||
description = ''
|
description = ''
|
||||||
|
@ -52,8 +52,9 @@ in
|
|||||||
system.clan.vm = {
|
system.clan.vm = {
|
||||||
# for clan vm inspect
|
# for clan vm inspect
|
||||||
config = {
|
config = {
|
||||||
inherit (config.clan.virtualisation) cores graphics;
|
clan_name = config.clanCore.clanName;
|
||||||
memory_size = config.clan.virtualisation.memorySize;
|
memory_size = config.clan.virtualisation.memorySize;
|
||||||
|
inherit (config.clan.virtualisation) cores graphics;
|
||||||
};
|
};
|
||||||
# for clan vm create
|
# for clan vm create
|
||||||
create = pkgs.writeText "vm.json" (builtins.toJSON {
|
create = pkgs.writeText "vm.json" (builtins.toJSON {
|
||||||
|
@ -11,6 +11,7 @@ from ..nix import nix_config, nix_eval
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class VmConfig:
|
class VmConfig:
|
||||||
|
clan_name: str
|
||||||
flake_url: str | Path
|
flake_url: str | Path
|
||||||
flake_attr: str
|
flake_attr: str
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from typing import IO
|
|||||||
|
|
||||||
from ..dirs import module_root
|
from ..dirs import module_root
|
||||||
from ..errors import ClanError
|
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
|
from .inspect import VmConfig, inspect_vm
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
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}")
|
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(
|
def generate_secrets(
|
||||||
vm: VmConfig,
|
vm: VmConfig,
|
||||||
clan_name: str,
|
|
||||||
nixos_config: dict[str, str],
|
nixos_config: dict[str, str],
|
||||||
tmpdir: Path,
|
tmpdir: Path,
|
||||||
log_fd: IO[str] | None,
|
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 isinstance(vm.flake_url, Path) and vm.flake_url.is_dir():
|
||||||
if Path(vm.flake_url).is_dir():
|
if Path(vm.flake_url).is_dir():
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[nixos_config["generateSecrets"], clan_name],
|
[nixos_config["generateSecrets"], vm.clan_name],
|
||||||
env=env,
|
env=env,
|
||||||
check=False,
|
check=False,
|
||||||
stdout=log_fd,
|
stdout=log_fd,
|
||||||
@ -243,9 +226,6 @@ def run_vm(
|
|||||||
|
|
||||||
# TODO: We should get this from the vm argument
|
# TODO: We should get this from the vm argument
|
||||||
nixos_config = get_vm_create_info(vm, nix_options)
|
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 = Path(vm.flake_url)
|
||||||
flake_dir.mkdir(exist_ok=True)
|
flake_dir.mkdir(exist_ok=True)
|
||||||
@ -255,7 +235,7 @@ def run_vm(
|
|||||||
xchg_dir = tmpdir / "xchg"
|
xchg_dir = tmpdir / "xchg"
|
||||||
xchg_dir.mkdir(exist_ok=True)
|
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)
|
disk_img = prepare_disk(tmpdir, log_fd)
|
||||||
|
|
||||||
qemu_cmd = qemu_command(
|
qemu_cmd = qemu_command(
|
||||||
|
Loading…
Reference in New Issue
Block a user