1
0
Fork 0
nix-configuration/hosts/common/nixos-upgrade.sh

63 lines
1.6 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Update NixOS system while printing out the different packages to install.
# Inspiration: https://blog.tjll.net/previewing-nixos-system-updates/
set -e
OPERATION="boot" # Which update method to use. Defaults to "boot", which applies updates on reboot.
2024-04-01 13:36:52 +00:00
AUTOACCEPT=false # Whether to automatically apply the update or ask for permission.
2024-04-01 13:36:52 +00:00
function usage() {
echo "Usage: nixos-upgrade.sh [ -y | --auto-accept ] [-o | --operation]"
echo "Options:"
echo " -h | --help Show this help screen."
echo " -y | --auto-accept Automatically approve pending changes."
echo " -o | --operation Which update operation to perform (switch, boot, etc.). Defaults to boot."
exit 2
}
while [[ $# -gt 0 ]]; do
case "$1" in
-y|-Y|--auto-accept)
AUTOACCEPT=true
shift
;;
-o|-O|--operation)
OPERATION=$2
shift
;;
-h|--help)
usage
2024-04-02 13:23:28 +00:00
exit
2024-04-01 13:36:52 +00:00
;;
*)
2024-04-02 13:23:28 +00:00
echo "Unknown argument $1"
exit 1
2024-04-01 13:36:52 +00:00
;;
esac
done
echo "Using installation mode: $OPERATION"
cd ~/Development/nix-configuration
nix flake update
nixos-rebuild build --flake .
2024-04-02 13:23:28 +00:00
UPDATES=$(nix store diff-closures /run/current-system ./result | awk '/[0-9] →|→ [0-9]/ && !/nixos/' || echo)
2024-04-05 00:45:20 +00:00
if [ "$UPDATES" ]; then
2024-04-02 13:23:28 +00:00
echo "NixOS updates to apply: "
echo $UPDATES
if [ $AUTOACCEPT == false ]; then
read -p "Continue with upgrade (y/n) ? " choice
case "$choice" in
y|Y|yes ) echo "Running nixos-rebuild $OPERATION :";;
n|N|no ) echo "Upgrade cancelled." && exit;;
* ) echo "Invalid option. Upgrade cancelled." && exit;;
esac
fi
sudo nixos-rebuild $OPERATION --flake .
else
echo "No NixOS updates found."
2024-04-01 13:36:52 +00:00
fi
echo "Updating Flatpaks:"
flatpak update