diff --git a/apps/rebuild/rebuild.sh b/apps/rebuild/rebuild.sh index ffe4a36..cb78314 100755 --- a/apps/rebuild/rebuild.sh +++ b/apps/rebuild/rebuild.sh @@ -11,6 +11,11 @@ UPDATE_INPUTS="" # Default list of inputs to update. Empty means ROLLBACK=0 # Default to not rollback SHOW_TRACE=0 # Default to not show detailed error messages +# Output functions +_status() { echo -e "\033[0;34m> $1\033[0m"; } +success() { echo -e "\033[0;32m$1\033[0m"; } +error() { echo -e "\033[0;31mError: $1\033[0m" >&2; exit 1; } + # Function to display the help message Help() { echo "Wrapper script for 'nixos-rebuild switch' and 'home-manager switch' commands." @@ -37,12 +42,6 @@ Help() { echo " -u, --user Specify the username (as in 'homeConfigurations.@'). Default: $HOME_USER" } -# Function to handle errors -error() { - echo "Error: $1" - exit 1 -} - # Function to rebuild NixOS configuration Rebuild_nixos() { local FLAKE="$FLAKE_PATH#$NIXOS_HOST" @@ -56,7 +55,7 @@ Rebuild_nixos() { [ -n "$BUILD_HOST" ] && CMD+=("--build-host" "$BUILD_HOST") if [ "$NIXOS_HOST" != "$(hostname)" ] && [ -z "$TARGET_HOST" ]; then TARGET_HOST="$NIXOS_HOST" - echo "Using '$TARGET_HOST' as target host." + _status "Using '$TARGET_HOST' as target host." fi [ -n "$TARGET_HOST" ] && CMD+=("--target-host" "$TARGET_HOST") @@ -64,21 +63,23 @@ Rebuild_nixos() { local BUILD_CMD=("nixos-rebuild" "build" "--flake" "$FLAKE") [ "$SHOW_TRACE" = 1 ] && BUILD_CMD+=("--show-trace") [ -n "$BUILD_HOST" ] && BUILD_CMD+=("--build-host" "$BUILD_HOST") + if [ "$ROLLBACK" = 0 ]; then - echo "Building NixOS configuration '$FLAKE'..." - echo "Executing command: ${BUILD_CMD[*]}" + _status "Building NixOS configuration '$FLAKE'..." + _status "Executing command: ${BUILD_CMD[*]}" "${BUILD_CMD[@]}" |&nom || error "NixOS build failed" - echo "Package diff:" + _status "Package diff:" dix /run/current-system result rm result else - echo "Rolling back to last NixOS generation..." + _status "Rolling back to last NixOS generation..." fi - echo "Switching to new NixOS configuration" + + _status "Switching to new NixOS configuration" sudo -v - echo "Executing command: ${CMD[*]}" + _status "Executing command: ${CMD[*]}" "${CMD[@]}" |& nom || error "NixOS rebuild failed" - echo "NixOS rebuild completed successfully." + success "NixOS rebuild completed successfully." } # Function to rebuild Home Manager configuration @@ -104,24 +105,25 @@ Rebuild_home() { if [ "$ROLLBACK" = 0 ]; then local BUILD_CMD=("home-manager" "build" "--flake" "$FLAKE") [ "$SHOW_TRACE" = 1 ] && BUILD_CMD+=("--show-trace") - echo "Building Home Manager configuration '$FLAKE'..." - echo "Executing command: ${BUILD_CMD[*]}" + _status "Building Home Manager configuration '$FLAKE'..." + _status "Executing command: ${BUILD_CMD[*]}" "${BUILD_CMD[@]}" |& nom || error "Home Manager build failed" - echo "Package diff:" + _status "Package diff:" dix /run/current-system result rm result else - echo "Rolling back to last Home Manager generation..." + _status "Rolling back to last Home Manager generation..." fi - echo "Switching to new Home Manager configuration" - echo "Executing command: ${CMD[*]}" + + _status "Switching to new Home Manager configuration" + _status "Executing command: ${CMD[*]}" "${CMD[@]}" |& nom || error "Home Manager rebuild failed" - echo "Home Manager rebuild completed successfully." + success "Home Manager rebuild completed successfully." } -# Function to Update flake repositories +# Function to update flake repositories Update() { - echo "Updating flake inputs..." + _status "Updating flake inputs..." # Construct update command as an array local CMD=("nix" "flake" "update" "--flake" "$FLAKE_PATH") @@ -133,17 +135,18 @@ Update() { done fi - echo "Executing command: ${CMD[*]}" + _status "Executing command: ${CMD[*]}" "${CMD[@]}" || error "Failed to update flake repositories" - echo "Flake repositories updated successfully." + success "Flake repositories updated successfully." } # Parse command-line options if [[ -z "${1:-}" ]]; then - echo "Error: No command specified. Printing help page." + echo -e "\033[0;31mError: No command specified. Printing help page.\033[0m" >&2 Help exit 1 fi + COMMAND=$1 shift @@ -214,9 +217,7 @@ while [ $# -gt 0 ]; do fi ;; *) - echo "Error: Unknown option '$1'" - Help - exit 1 + error "Unknown option '$1'" ;; esac done @@ -254,9 +255,6 @@ case "$COMMAND" in Rebuild_home ;; *) - echo "Error: Unknown command '$COMMAND'" - echo "Printing help page:" - Help - exit 1 + error "Unknown command '$COMMAND'" ;; esac