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