From bfe774b74ccf604029f9e2b9870b7eee096069b5 Mon Sep 17 00:00:00 2001 From: Leyla Becker Date: Fri, 31 Oct 2025 17:08:28 -0500 Subject: [PATCH] added --vm flag to rebuild --- .gitignore | 3 ++- rebuild.sh | 68 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ce2538f..2810727 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ result .direnv .vscode/* -!.vscode/settings.json \ No newline at end of file +!.vscode/settings.json +nixos.qcow2 diff --git a/rebuild.sh b/rebuild.sh index 6750450..48746d9 100755 --- a/rebuild.sh +++ b/rebuild.sh @@ -18,6 +18,7 @@ else fi show_trace=false +clean_vm=false while [ $# -gt 0 ]; do case "$1" in @@ -50,6 +51,9 @@ while [ $# -gt 0 ]; do --show-trace) show_trace=true ;; + --clean-vm) + clean_vm=true + ;; --help|-h) echo "--help -h: print this message" echo "--target -t: defaults to the current system" @@ -58,6 +62,8 @@ while [ $# -gt 0 ]; do echo " currently: ${target:-$default_target}" echo "--mode -m: defaults to 'switch', but 'test' on non-main branches" echo " currently would be: $default_mode" + echo " Available modes: switch, test, build, boot, vm" + echo " 'vm' mode builds and starts a virtual machine" echo "--user -u: defaults to the current user" echo " currently: $default_user" echo "--host: defaults to building on the current machine" @@ -65,6 +71,7 @@ while [ $# -gt 0 ]; do echo "--preserve-result: do not remove the generated result folder after building" echo "--no-preserve-result: remove any result folder after building" echo "--show-trace: show trace on builds" + echo "--clean-vm: remove existing VM disk (nixos.qcow2) before building" echo "" echo "Branch-aware behavior:" echo " - On non-main branches: defaults to test mode with warning" @@ -90,6 +97,20 @@ flake=${flake:-$target} mode=${mode:-$default_mode} user=${user:-$default_user} +# Validate mode +valid_modes="switch test build boot vm" +if [[ ! " $valid_modes " =~ " $mode " ]]; then + echo "Error: Invalid mode '$mode'" + echo "Valid modes are: $valid_modes" + exit 1 +fi + +# Clean VM disk if requested +if [[ "$clean_vm" = true ]] && [[ -f "nixos.qcow2" ]]; then + echo "Removing existing VM disk: nixos.qcow2" + rm nixos.qcow2 +fi + # Branch-aware warnings and behavior if [[ "$current_branch" != "main" ]] && [[ "$mode" == "test" ]]; then echo "⚠️ WARNING: You are on branch '$current_branch' (not main)" @@ -101,26 +122,39 @@ elif [[ "$current_branch" == "main" ]] && [[ -n "$git_status" ]] && [[ "$mode" ! echo " git checkout -b feature/your-feature-name" fi -command="nixos-rebuild $mode --sudo --ask-sudo-password --flake .#$flake" +if [[ "$mode" == "vm" ]]; then + command="nix build .#nixosConfigurations.$flake.config.system.build.vm" + + if [[ "$show_trace" = true ]]; then + command="$command --show-trace" + fi + + echo $command + $command + + if [[ $? -eq 0 ]] && [[ -d "result" ]]; then + echo "Starting VM..." + QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic; reset + fi +else + command="nixos-rebuild $mode --sudo --ask-sudo-password --flake .#$flake" + + if [[ $host ]]; then + command="$command --build-host $host" + fi -if [[ $host ]]; -then - command="$command --build-host $host" + if [[ "$target" != "$(hostname)" ]]; then + command="$command --target-host $user@$target" + fi + + if [[ "$show_trace" = true ]]; then + command="$command --show-trace" + fi + + echo $command + $command fi -if [[ "$target" != "$(hostname)" ]]; -then - command="$command --target-host $user@$target" -fi - -if [[ "$show_trace" = true ]]; -then - command="$command --show-trace" -fi - -echo $command -$command - if [ -d "result" ]; then if [[ "$preserve_result" == "false" ]];