164 lines
4.7 KiB
Bash
Executable file
164 lines
4.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# Get current branch and git status for branch-aware behavior
|
|
current_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
|
|
git_status=$(git status --porcelain 2>/dev/null || echo "")
|
|
|
|
# Default values
|
|
default_target=$(hostname)
|
|
default_user="$USER"
|
|
default_host=$(hostname)
|
|
default_mode=$(if [[ "$current_branch" != "main" ]]; then echo "test"; else echo "switch"; fi)
|
|
|
|
if [ -d "result" ];
|
|
then
|
|
preserve_result=true
|
|
else
|
|
preserve_result=false
|
|
fi
|
|
|
|
show_trace=false
|
|
clean_vm=false
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--target*|-t*)
|
|
if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=`
|
|
target="${1#*=}"
|
|
;;
|
|
--flake*|-f*)
|
|
if [[ "$1" != *=* ]]; then shift; fi
|
|
flake="${1#*=}"
|
|
;;
|
|
--mode*|-m*)
|
|
if [[ "$1" != *=* ]]; then shift; fi
|
|
mode="${1#*=}"
|
|
;;
|
|
--user*|-u*)
|
|
if [[ "$1" != *=* ]]; then shift; fi
|
|
user="${1#*=}"
|
|
;;
|
|
--host*)
|
|
if [[ "$1" != *=* ]]; then shift; fi
|
|
host="${1#*=}"
|
|
;;
|
|
--preserve-result)
|
|
preserve_result=true
|
|
;;
|
|
--no-preserve-result)
|
|
preserve_result=false
|
|
;;
|
|
--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"
|
|
echo " currently: $default_target"
|
|
echo "--flake -f: defaults to same as target"
|
|
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"
|
|
echo " currently: $default_host"
|
|
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"
|
|
echo " - On main with uncommitted changes: shows warning about creating a branch"
|
|
echo " - Current branch: $current_branch"
|
|
if [[ -n "$git_status" ]]; then
|
|
echo " - Git status: uncommitted changes detected"
|
|
else
|
|
echo " - Git status: clean working tree"
|
|
fi
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Error: Invalid argument $1"
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
target=${target:-$default_target}
|
|
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)"
|
|
echo " Defaulting to test mode to prevent accidental system changes"
|
|
echo " Specify --mode=switch explicitly if you want to apply changes"
|
|
elif [[ "$current_branch" == "main" ]] && [[ -n "$git_status" ]] && [[ "$mode" != "test" ]]; then
|
|
echo "⚠️ WARNING: You are on main branch with uncommitted changes"
|
|
echo " Consider creating a feature branch for development:"
|
|
echo " git checkout -b feature/your-feature-name"
|
|
fi
|
|
|
|
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 [[ "$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 [ -d "result" ];
|
|
then
|
|
if [[ "$preserve_result" == "false" ]];
|
|
then
|
|
rm -r result
|
|
fi
|
|
fi
|