133 lines
4.2 KiB
Nix
133 lines
4.2 KiB
Nix
# ncli/commands/rebuild.nix — Rebuild + Update logic
|
|
lib:
|
|
|
|
let
|
|
inherit (lib) host project handle_backups handle_build_error;
|
|
in
|
|
{
|
|
# Shared rebuild snippet used by both `rebuild` and `update`
|
|
rebuild_logic = ''
|
|
handle_backups
|
|
geno=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
|
|
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
|
|
|
|
current=""
|
|
if [ -f /etc/nixos-tags ]; then
|
|
current=$(cat /etc/nixos-tags)
|
|
fi
|
|
'';
|
|
|
|
rebuild_case = ''
|
|
${rebuild_logic}
|
|
echo -e "Starting NixOS rebuild for current host: ${host} on generation: $YELLOW$geno$NOCOLOR"
|
|
|
|
sudo nixos-rebuild switch --flake . ; _rebuild_exit=$?
|
|
if [ "$_rebuild_exit" -eq 0 ]; then
|
|
echo "✓ Rebuild finished successfully for ${host}"
|
|
|
|
if [ -n "$current" ]; then
|
|
sudo /run/current-system/specialisation/"$current"/bin/switch-to-configuration test
|
|
else
|
|
echo "No specialization tag found, staying on default system."
|
|
fi
|
|
|
|
genn=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
|
|
echo -e "Running on new generation: $YELLOW $geno $NOCOLOR-> $GREEN$genn$NOCOLOR"
|
|
else
|
|
( exit "$_rebuild_exit" ); handle_build_error
|
|
echo "✗ Rebuild failed for ${host}" >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
'';
|
|
|
|
update_case = ''
|
|
${rebuild_logic}
|
|
echo -e "Updating flake and rebuilding system for current host: ${host} on generation: $YELLOW$geno$NOCOLOR"
|
|
|
|
# --- Selective flake update ---
|
|
read -rp "Update [a]ll inputs or [s]elect manually? (a/s): " choice
|
|
|
|
case "$choice" in
|
|
a|A)
|
|
echo "Updating all inputs..."
|
|
if nix flake update --flake .; then
|
|
echo "✓ Flake updated successfully"
|
|
else
|
|
echo "✗ Flake update failed" >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
s|S)
|
|
echo "Fetching available updates (this may take a moment)..."
|
|
TEMP_LOCK=$(mktemp)
|
|
trap 'rm -f "$TEMP_LOCK"' EXIT
|
|
|
|
nix flake update --output-lock-file "$TEMP_LOCK" --flake . 2>/dev/null
|
|
|
|
outdated=$(jq -r --slurpfile new "$TEMP_LOCK" '
|
|
.nodes as $old |
|
|
$new[0].nodes as $newn |
|
|
($old | keys[]) |
|
|
select(. != "root") |
|
|
select(
|
|
($old[.].locked.lastModified // 0) !=
|
|
($newn[.].locked.lastModified // 0)
|
|
)
|
|
' flake.lock)
|
|
|
|
if [[ -z "$outdated" ]]; then
|
|
echo "✓ All inputs are already up to date, skipping flake update."
|
|
else
|
|
echo
|
|
echo "Updates available for:"
|
|
printf '%s\n' "$outdated"
|
|
echo
|
|
echo "Tab to select, Enter to update, Esc to cancel."
|
|
selected=$(printf '%s\n' "$outdated" | fzf --multi) || {
|
|
echo "No inputs selected, skipping flake update."
|
|
selected=""
|
|
}
|
|
if [[ -n "$selected" ]]; then
|
|
if nix flake update --flake . $selected; then
|
|
echo "✓ Flake updated successfully"
|
|
else
|
|
echo "✗ Flake update failed" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
fi
|
|
;;
|
|
*)
|
|
echo "Invalid choice, skipping flake update."
|
|
;;
|
|
esac
|
|
# --- End selective flake update ---
|
|
|
|
if [ -n "$current" ]; then
|
|
echo "Rebuilding system... Current specialization: $current"
|
|
else
|
|
echo "Rebuilding system... Staying on current specialization"
|
|
fi
|
|
|
|
sudo nixos-rebuild switch --flake . ; _rebuild_exit=$?
|
|
if [ "$_rebuild_exit" -eq 0 ]; then
|
|
echo "✓ Update and rebuild finished successfully for ${host}"
|
|
|
|
if [ -n "$current" ]; then
|
|
sudo /run/current-system/specialisation/"$current"/bin/switch-to-configuration test
|
|
else
|
|
echo "No specialization tag found, staying on default system."
|
|
fi
|
|
|
|
genn=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
|
|
echo -e "Running on new generation: $YELLOW $geno $NOCOLOR-> $GREEN$genn$NOCOLOR"
|
|
else
|
|
( exit "$_rebuild_exit" ); handle_build_error
|
|
echo "✗ Update and rebuild failed for ${host}" >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
'';
|
|
}
|