From 78230661470e492bb0f813bc9d8631c2aff7e5af Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 24 Oct 2026 22:22:41 -0400 Subject: [PATCH] Add a script to launch the robot vm. --- .../files/robot/launch_robot.bash | 88 +++++++++++++++++++ nix/configuration/flake.nix | 62 +++++++++++-- 2 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 nix/configuration/files/robot/launch_robot.bash diff --git a/nix/configuration/files/robot/launch_robot.bash b/nix/configuration/files/robot/launch_robot.bash new file mode 100644 index 0000000..2b92281 --- /dev/null +++ b/nix/configuration/files/robot/launch_robot.bash @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# +# Launch the robot virtual machine +set -euo pipefail +IFS=$'\n\t' +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +: ${CPU_CORES:="1"} +: ${MEMORY:="1G"} +: ${GTK_ENABLE:="NO"} # Only enable one, either GTK or VNC +: ${VNC_ENABLE:="NO"} # Only enable one, either GTK or VNC +: ${VNC_LISTEN:="127.0.0.1:0"} +: ${VNC_WIDTH:="1920"} +: ${VNC_HEIGHT:="1080"} +: ${AUDIO_ENABLE:="NO"} + +############## Setup ######################### + + +function cleanup { + sync + + for p in "${pids[@]}"; do + log "Killing $p" + kill "$p" + log "Killed $p" + done +} +pids=() +trap "set +e; cleanup" EXIT + +function die { + local status_code="$1" + shift + (>&2 echo "${@}") + exit "$status_code" +} + +function log { + (>&2 echo "${@}") +} + +############## Program ######################### + + +function main { + local mount_path="$1" + shift 1 + + + local additional_args=() + + additional_args+=(-drive "file=@mountiso@,media=cdrom,id=drive-cd1,readonly=on,if=none" + -device "ahci,id=achi0" + -device "ide-cd,bus=achi0.0,drive=drive-cd1,id=cd1,bootindex=1") + + additional_args+=(-device "virtio-9p-pci,fsdev=${mount_path},mount_tag=host") + + if [ "$VNC_ENABLE" = "YES" ]; then + additional_args+=(-vnc "${VNC_LISTEN},power-control=on") + fi + + if [ "$AUDIO_ENABLE" = "YES" ]; then + additional_args+=(-audio "driver=pa,model=virtio,server=/run/user/11235/pulse/native") + fi + + if [ "$GTK_ENABLE" = "YES" ]; then + additional_args+=( + -device 'virtio-gpu-gl,hostmem=8G,blob=true,venus=true' + -display 'gtk,gl=on' + -vga virtio + ) + fi + + exec qemu-system-x86_64 \ + -name robot \ + -accel kvm \ + -cpu host \ + -smp cores="$CPU_CORES" \ + -m "$MEMORY" \ + -rtc base=localtime \ + -device virtio-rng-pci \ + -drive "file=@OVMFfd@/FV/OVMF_CODE.fd,if=pflash,format=raw,readonly=on" \ + -nic 'user,hostfwd=tcp::60022-:22' \ + "${additional_args[@]}" +} + +main "${@}" diff --git a/nix/configuration/flake.nix b/nix/configuration/flake.nix index d0e7d77..92618ed 100644 --- a/nix/configuration/flake.nix +++ b/nix/configuration/flake.nix @@ -126,12 +126,62 @@ packages = ( forAllSystems ( system: - (builtins.mapAttrs (hostname: nodeConfig: { - iso = (nixosConfigs."${hostname}" "iso").config.system.build.isoImage; - vm_iso = (nixosConfigs."${hostname}" "vm_iso").config.system.build.isoImage; - sd = (nixosConfigs."${hostname}" "sd").config.system.build.sdImage; - installer = (installerConfig hostname nodes."${hostname}").config.system.build.isoImage; - }) (nixpkgs.lib.attrsets.filterAttrs (hostname: nodeConfig: nodeConfig.system == system) nodes)) + ( + (builtins.mapAttrs (hostname: nodeConfig: { + iso = (nixosConfigs."${hostname}" "iso").config.system.build.isoImage; + vm_iso = (nixosConfigs."${hostname}" "vm_iso").config.system.build.isoImage; + sd = (nixosConfigs."${hostname}" "sd").config.system.build.sdImage; + installer = (installerConfig hostname nodes."${hostname}").config.system.build.isoImage; + }) (nixpkgs.lib.attrsets.filterAttrs (hostname: nodeConfig: nodeConfig.system == system) nodes)) + // { + launch_robot = + let + pkgs = (nixosConfigs."robot" "vm_iso").pkgs; + patchScriptBin = + filename: contents: + ((pkgs.writeScriptBin filename contents).overrideAttrs (old: { + buildCommand = "${old.buildCommand}\n patchShebangs $out"; + })); + launch_robot_script = ( + patchScriptBin "launch_robot" ( + builtins.readFile ( + pkgs.replaceVars ./files/robot/launch_robot.bash ( + let + robot_image_package = (nixosConfigs."robot" "vm_iso").config.system.build.isoImage; + robot_image = "${robot_image_package}/iso/${robot_image_package.isoName}"; + in + { + "OVMFfd" = "${pkgs.OVMF.fd}"; + "mountiso" = robot_image; + } + ) + ) + ) + ); + launch_robot_script_wrapped = + (pkgs.writeScriptBin "launch_robot" '' + #!/usr/bin/env bash + export "PATH=${ + pkgs.lib.makeBinPath ( + with pkgs; + [ + qemu + ] + ) + }:''${PATH}" + exec ${launch_robot_script}/bin/launch_robot "''${@}" + '').overrideAttrs + (old: { + buildCommand = '' + ${old.buildCommand} + patchShebangs $out + ''; + }); + + in + launch_robot_script_wrapped; + } + ) ) ); };