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

3
.gitignore vendored
View file

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

View file

@ -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" ]];