added --vm flag to rebuild

This commit is contained in:
Leyla Becker 2025-10-31 17:08:28 -05:00
parent e1a5ddde95
commit bfe774b74c
2 changed files with 53 additions and 18 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ result
.direnv .direnv
.vscode/* .vscode/*
!.vscode/settings.json !.vscode/settings.json
nixos.qcow2

View file

@ -18,6 +18,7 @@ else
fi fi
show_trace=false show_trace=false
clean_vm=false
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
@ -50,6 +51,9 @@ while [ $# -gt 0 ]; do
--show-trace) --show-trace)
show_trace=true show_trace=true
;; ;;
--clean-vm)
clean_vm=true
;;
--help|-h) --help|-h)
echo "--help -h: print this message" echo "--help -h: print this message"
echo "--target -t: defaults to the current system" echo "--target -t: defaults to the current system"
@ -58,6 +62,8 @@ while [ $# -gt 0 ]; do
echo " currently: ${target:-$default_target}" echo " currently: ${target:-$default_target}"
echo "--mode -m: defaults to 'switch', but 'test' on non-main branches" echo "--mode -m: defaults to 'switch', but 'test' on non-main branches"
echo " currently would be: $default_mode" 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 "--user -u: defaults to the current user"
echo " currently: $default_user" echo " currently: $default_user"
echo "--host: defaults to building on the current machine" 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 "--preserve-result: do not remove the generated result folder after building"
echo "--no-preserve-result: remove any result folder after building" echo "--no-preserve-result: remove any result folder after building"
echo "--show-trace: show trace on builds" echo "--show-trace: show trace on builds"
echo "--clean-vm: remove existing VM disk (nixos.qcow2) before building"
echo "" echo ""
echo "Branch-aware behavior:" echo "Branch-aware behavior:"
echo " - On non-main branches: defaults to test mode with warning" echo " - On non-main branches: defaults to test mode with warning"
@ -90,6 +97,20 @@ flake=${flake:-$target}
mode=${mode:-$default_mode} mode=${mode:-$default_mode}
user=${user:-$default_user} 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 # Branch-aware warnings and behavior
if [[ "$current_branch" != "main" ]] && [[ "$mode" == "test" ]]; then if [[ "$current_branch" != "main" ]] && [[ "$mode" == "test" ]]; then
echo "⚠️ WARNING: You are on branch '$current_branch' (not main)" 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" echo " git checkout -b feature/your-feature-name"
fi 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 [[ $host ]]; if [[ "$show_trace" = true ]]; then
then command="$command --show-trace"
command="$command --build-host $host" 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 [[ "$target" != "$(hostname)" ]]; then
command="$command --target-host $user@$target"
fi
if [[ "$show_trace" = true ]]; then
command="$command --show-trace"
fi
echo $command
$command
fi 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" ]; if [ -d "result" ];
then then
if [[ "$preserve_result" == "false" ]]; if [[ "$preserve_result" == "false" ]];