nixos-config/quick-switch.sh

65 lines
1.2 KiB
Bash
Raw Normal View History

2024-08-24 15:36:28 +00:00
#!/usr/bin/env bash
2023-06-17 17:51:24 +00:00
set -euo pipefail
2023-06-16 23:32:12 +00:00
2024-07-28 15:29:56 +00:00
commitchanges="false"
while getopts 'c' OPTION; do
case "$OPTION" in
2024-08-24 15:36:28 +00:00
c) commitchanges="true" ;;
*) exit 1 ;;
2024-07-28 15:29:56 +00:00
esac
done
2024-08-24 15:36:28 +00:00
# Stolen from https://stackoverflow.com/a/29436423
2024-07-26 22:27:47 +00:00
function yes_or_no {
2024-08-24 15:36:28 +00:00
while true; do
read -r -p "$* [y/n]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*)
echo "Aborted"
return 1
;;
esac
done
2024-07-26 22:27:47 +00:00
}
2024-07-28 15:29:56 +00:00
function reset_bspwm() {
pkill -USR1 -x sxhkd
2024-07-28 21:13:50 +00:00
pkill -x picom
pkill polybar
2024-08-24 15:36:28 +00:00
2024-07-28 15:29:56 +00:00
sxhkd &
bspc wm -r
}
2024-07-26 22:27:47 +00:00
function apply() {
2023-06-16 23:56:23 +00:00
git push --force-with-lease
2023-06-17 17:51:24 +00:00
rev=$(git rev-parse HEAD)
2023-06-17 00:30:33 +00:00
sudo nixos-rebuild switch \
2024-02-04 19:17:33 +00:00
--flake "git+https://git.atauno.com/atau/nixos-config.git?ref=main&rev=$rev"
2024-07-28 15:29:56 +00:00
reset_bspwm
2024-07-26 22:27:47 +00:00
}
function commit_and_apply() {
2024-08-24 15:36:28 +00:00
read -r -p "Git working directory is unclean. Commit message: " msg
2024-07-26 22:27:47 +00:00
git add .
git commit -m "$(hostname): $msg"
apply
}
2024-08-24 15:36:28 +00:00
if [ -z "$(git status --porcelain)" ]; then
2024-07-26 22:27:47 +00:00
apply
2023-06-16 23:32:12 +00:00
else
2024-08-24 15:36:28 +00:00
if [ "$commitchanges" == "false" ]; then
echo "Using local flake..."
sudo nixos-rebuild switch --show-trace --flake .
reset_bspwm
else
echo "Committing flake and pushing..."
commit_and_apply
2024-07-28 15:29:56 +00:00
fi
2024-08-24 15:36:28 +00:00
# shellcheck disable=SC2028
2024-07-28 17:16:30 +00:00
echo "\nOperation completed."
2023-06-16 23:32:12 +00:00
fi