Add a host for a virtual machine for agent coding.
This commit is contained in:
81
nix/configuration/roles/robot/default.nix
Normal file
81
nix/configuration/roles/robot/default.nix
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
self,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
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 = self.packages.${pkgs.system}.robot.vm_iso;
|
||||
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
|
||||
|
||||
{
|
||||
imports = [ ];
|
||||
|
||||
options.me = {
|
||||
robot.enable = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
example = true;
|
||||
description = "Whether we want to install robot.";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf config.me.robot.enable {
|
||||
environment.systemPackages = with pkgs; [
|
||||
launch_robot_script_wrapped
|
||||
];
|
||||
environment.persistence."/persist" = lib.mkIf (config.me.mountPersistence) {
|
||||
hideMounts = true;
|
||||
directories = [
|
||||
{
|
||||
directory = "/robot";
|
||||
user = "talexander";
|
||||
group = "talexander";
|
||||
mode = "0700";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
85
nix/configuration/roles/robot/files/robot/launch_robot.bash
Normal file
85
nix/configuration/roles/robot/files/robot/launch_robot.bash
Normal file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Launch the robot virtual machine
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
: ${CPU_CORES:="4"}
|
||||
: ${MEMORY:="8G"}
|
||||
: ${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"}
|
||||
|
||||
############## 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+=(-fsdev "local,id=fsdev0,path=${mount_path},security_model=mapped-xattr" -device "virtio-9p-pci,fsdev=fsdev0,mount_tag=host")
|
||||
|
||||
mkdir -p "${mount_path}/.persist" "${mount_path}/.state"
|
||||
|
||||
if [ "$VNC_ENABLE" = "YES" ]; then
|
||||
additional_args+=(-vnc "${VNC_LISTEN},power-control=on")
|
||||
elif [ "$GTK_ENABLE" = "YES" ]; then
|
||||
additional_args+=(
|
||||
-device 'virtio-gpu-gl,hostmem=8G,blob=true,venus=true'
|
||||
-display 'gtk,gl=on'
|
||||
-vga virtio
|
||||
)
|
||||
else
|
||||
additional_args+=(-nographic)
|
||||
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 "${@}"
|
||||
Reference in New Issue
Block a user