nixos-config/quick-switch.sh

36 lines
775 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Stolen from https://stackoverflow.com/a/29436423
function yes_or_no {
while true; do
read -p "$* [y/n]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) echo "Aborted" ; return 1 ;;
esac
done
}
function apply() {
git push --force-with-lease
rev=$(git rev-parse HEAD)
sudo nixos-rebuild switch \
--flake "git+https://git.atauno.com/atau/nixos-config.git?ref=main&rev=$rev"
}
function commit_and_apply() {
read -p "Enter a commit message: " msg
git add .
git commit -m "$(hostname): $msg"
apply
}
if [ -z "$(git status --porcelain)" ]
then
apply
else
yes_or_no "Git working directory is unclean, would you like to commit changes?" && commit_and_apply
fi