nixos/update.sh

37 lines
871 B
Bash
Raw Permalink Normal View History

2024-05-14 09:29:16 -05:00
#!/usr/bin/env bash
# Change into script dir.
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit
nixosDir=$(pwd)
# Get short hostname to work with host specific configurations.
host=$(hostname -s)
if [[ -n $nixHostOverride ]]; then
host=$nixHostOverride
fi
if (( EUID==0 )); then
sudoCmd="sudo -u grmrgecko"
fi
2024-05-14 09:29:16 -05:00
# Confirm host configuration is available; If not, we should not continue.
if ! grep -q "nixosConfigurations.$host " flake.nix; then
2024-05-14 09:29:16 -05:00
host="default"
fi
# Update nixpkgs.
if ! $sudoCmd nix flake update "$nixosDir"; then
2024-05-14 09:29:16 -05:00
echo "Update failed"
exit 1
fi
# Add updated lock file to git staging for rebuild below.
$sudoCmd git add flake.lock
2024-05-14 09:29:16 -05:00
# Commit update.
$sudoCmd git commit -m "Flake update $(date)"
2024-05-14 09:29:16 -05:00
# Rebuild and switch.
# shellcheck disable=SC2068
sudo nixos-rebuild switch --impure --flake "path:$nixosDir/#$host" $@