diff --git a/apps/rebuild/default.nix b/apps/rebuild/default.nix index 21242c7..72503a3 100644 --- a/apps/rebuild/default.nix +++ b/apps/rebuild/default.nix @@ -7,6 +7,8 @@ hostname, nix, nixos-rebuild-ng, + nix-output-monitor, + dix, ... }: @@ -26,5 +28,7 @@ writeShellApplication { hostname nix nixos-rebuild-ng + nix-output-monitor + dix ]; } diff --git a/apps/rebuild/rebuild.sh b/apps/rebuild/rebuild.sh index d04ed21..cb78314 100755 --- a/apps/rebuild/rebuild.sh +++ b/apps/rebuild/rebuild.sh @@ -1,15 +1,15 @@ # NixOS and standalone Home Manager rebuild script # Defaults -FLAKE_PATH="${REBUILD_FLAKE_PATH:-${FLAKE_PATH:-$HOME/.config/nixos}}" # Default flake path -USER="${REBUILD_USER:-$(whoami)}" # Default username -HOST="${REBUILD_HOST:-$(hostname)}" # Default hostname -BUILD_HOST="${REBUILD_BUILD_HOST:-}" # Default build host -TARGET_HOST="${REBUILD_TARGET_HOST:-}" # Default target host -UPDATE="${REBUILD_UPDATE:-0}" # Default to not update -UPDATE_INPUTS="${REBUILD_UPDATE_INPUTS:-}" # Default list of inputs -ROLLBACK="${REBUILD_ROLLBACK:-0}" # Default to not rollback -SHOW_TRACE="${REBUILD_SHOW_TRACE:-0}" # Default to not show trace +FLAKE_PATH="$HOME/.config/nixos" # Default flake path +HOME_USER="$(whoami)" # Default username. Used to identify the Home Manager configuration +NIXOS_HOST="$(hostname)" # Default hostname. Used to identify the NixOS and Home Manager configuration +BUILD_HOST="" # Default build host. Empty means localhost +TARGET_HOST="" # Default target host. Empty means localhost +UPDATE=0 # Default to not update flake repositories +UPDATE_INPUTS="" # Default list of inputs to update. Empty means all +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"; } @@ -28,7 +28,7 @@ Help() { echo " help Show this help message" echo echo "Options (for NixOS and Home Manager):" - echo " -H, --host Specify the hostname (as in 'nixosConfiguraions.'). Default: $HOST" + echo " -H, --host Specify the hostname (as in 'nixosConfiguraions.'). Default: $NIXOS_HOST" echo " -p, --path Set the path to the flake directory. Default: $FLAKE_PATH" echo " -U, --update [inputs] Update all flake inputs. Optionally provide comma-separated list of inputs to update instead." echo " -r, --rollback Don't build the new configuration, but use the previous generation instead" @@ -39,25 +39,25 @@ Help() { echo " -T, --target-host Deploy the configuration to a remote host via SSH. If '--host' is specified, it will be used as the target host." echo echo "Home Manager only options:" - echo " -u, --user Specify the username (as in 'homeConfigurations.@'). Default: $USER" + echo " -u, --user Specify the username (as in 'homeConfigurations.@'). Default: $HOME_USER" } # Function to rebuild NixOS configuration Rebuild_nixos() { - local FLAKE="$FLAKE_PATH#$HOST" + local FLAKE="$FLAKE_PATH#$NIXOS_HOST" # Construct rebuild command local CMD=("nixos-rebuild" "switch" "--sudo") + [[ -n "$TARGET_HOST" || -n "$BUILD_HOST" ]] && CMD+=("--ask-sudo-password") CMD+=("--flake" "$FLAKE") [ "$ROLLBACK" = 1 ] && CMD+=("--rollback") [ "$SHOW_TRACE" = 1 ] && CMD+=("--show-trace") [ -n "$BUILD_HOST" ] && CMD+=("--build-host" "$BUILD_HOST") - if [ "$HOST" != "$(hostname)" ] && [ -z "$TARGET_HOST" ]; then - TARGET_HOST="$HOST" + if [ "$NIXOS_HOST" != "$(hostname)" ] && [ -z "$TARGET_HOST" ]; then + TARGET_HOST="$NIXOS_HOST" _status "Using '$TARGET_HOST' as target host." fi [ -n "$TARGET_HOST" ] && CMD+=("--target-host" "$TARGET_HOST") - [[ -n "$TARGET_HOST" || -n "$BUILD_HOST" ]] && CMD+=("--ask-sudo-password") # Build config first so we can diff it local BUILD_CMD=("nixos-rebuild" "build" "--flake" "$FLAKE") @@ -67,22 +67,24 @@ Rebuild_nixos() { if [ "$ROLLBACK" = 0 ]; then _status "Building NixOS configuration '$FLAKE'..." _status "Executing command: ${BUILD_CMD[*]}" - "${BUILD_CMD[@]}" || error "NixOS build failed" - - _status "Switching to new NixOS configuration" + "${BUILD_CMD[@]}" |&nom || error "NixOS build failed" + _status "Package diff:" + dix /run/current-system result + rm result else - _status "Rolling back to last NixOS generation" + _status "Rolling back to last NixOS generation..." fi + _status "Switching to new NixOS configuration" sudo -v _status "Executing command: ${CMD[*]}" - "${CMD[@]}" || error "NixOS rebuild failed" + "${CMD[@]}" |& nom || error "NixOS rebuild failed" success "NixOS rebuild completed successfully." } # Function to rebuild Home Manager configuration Rebuild_home() { - local FLAKE="$FLAKE_PATH#$USER@$HOST" + local FLAKE="$FLAKE_PATH#$HOME_USER@$NIXOS_HOST" if [ -n "$BUILD_HOST" ] || [ -n "$TARGET_HOST" ]; then error "Remote building is not supported for Home Manager." @@ -105,15 +107,17 @@ Rebuild_home() { [ "$SHOW_TRACE" = 1 ] && BUILD_CMD+=("--show-trace") _status "Building Home Manager configuration '$FLAKE'..." _status "Executing command: ${BUILD_CMD[*]}" - "${BUILD_CMD[@]}" || error "Home Manager build failed" - - _status "Switching to new Home Manager configuration" + "${BUILD_CMD[@]}" |& nom || error "Home Manager build failed" + _status "Package diff:" + dix /run/current-system result + rm result else - _status "Rolling back to last Home Manager generation" + _status "Rolling back to last Home Manager generation..." fi + _status "Switching to new Home Manager configuration" _status "Executing command: ${CMD[*]}" - "${CMD[@]}" || error "Home Manager rebuild failed" + "${CMD[@]}" |& nom || error "Home Manager rebuild failed" success "Home Manager rebuild completed successfully." } @@ -156,7 +160,7 @@ while [ $# -gt 0 ]; do case "${1:-}" in -H|--host) if [ -n "${2:-}" ]; then - HOST="$2" + NIXOS_HOST="$2" shift 2 else error "-H|--host option requires an argument" @@ -164,7 +168,7 @@ while [ $# -gt 0 ]; do ;; -u|--user) if [ -n "${2:-}" ]; then - USER="$2" + HOME_USER="$2" shift 2 else error "-u|--user option requires an argument"