nix-config/rebuild.sh

130 lines
3.6 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
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
;;
--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 "--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 ""
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}
# 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
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
if [ -d "result" ];
then
if [[ "$preserve_result" == "false" ]];
then
rm -r result
fi
fi