From 3054b9c77dd6b369e9dcbcbca88ebcde550be0c2 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 1/3] 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( From d7a9c1bc96257b6aa9972403025358256b0e1bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 8 Dec 2023 16:17:58 +0100 Subject: [PATCH 2/3] switch to initrd.systemd by default --- nixosModules/clanCore/vm.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nixosModules/clanCore/vm.nix b/nixosModules/clanCore/vm.nix index 181e3b188..0c171e1c8 100644 --- a/nixosModules/clanCore/vm.nix +++ b/nixosModules/clanCore/vm.nix @@ -11,6 +11,7 @@ let neededForBoot = true; options = [ "trans=virtio" "version=9p2000.L" "cache=loose" ]; }; + boot.initrd.systemd.enable = true; } ]; }; From 821e5e0a7bc6e8258243c90255eccf5876f772b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 8 Dec 2023 16:19:09 +0100 Subject: [PATCH 3/3] disable tty0 when using wayland we don't vga output and use our serial console instead --- pkgs/clan-cli/clan_cli/vms/run.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_cli/vms/run.py b/pkgs/clan-cli/clan_cli/vms/run.py index b28bf1ebe..9ac0e8c84 100644 --- a/pkgs/clan-cli/clan_cli/vms/run.py +++ b/pkgs/clan-cli/clan_cli/vms/run.py @@ -59,14 +59,16 @@ def qemu_command( xchg_dir: Path, secrets_dir: Path, disk_img: Path, + wayland: bool, ) -> list[str]: kernel_cmdline = [ (Path(nixos_config["toplevel"]) / "kernel-params").read_text(), f'init={nixos_config["toplevel"]}/init', f'regInfo={nixos_config["regInfo"]}/registration', "console=ttyS0,115200n8", - "console=tty0", ] + if not wayland: + kernel_cmdline.append("console=tty0") # fmt: off command = [ "qemu-kvm", @@ -244,6 +246,7 @@ def run_vm( xchg_dir=xchg_dir, secrets_dir=secrets_dir, disk_img=disk_img, + wayland=vm.wayland, ) if vm.wayland: