nixos-config/quick-switch.sh

63 lines
1.2 KiB
Bash
Raw Normal View History

2023-06-16 23:32:12 +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
c) commitchanges="true" ;;
esac
done
2024-07-26 22:29:28 +00:00
# Stolen from https://stackoverflow.com/a/29436423
2024-07-26 22:27:47 +00:00
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
}
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-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-07-28 15:29:56 +00:00
read -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
}
if [ -z "$(git status --porcelain)" ]
then
apply
2023-06-16 23:32:12 +00:00
else
2024-07-28 15:29:56 +00:00
if [ "$commitchanges" == "false" ]
then
echo "Using local flake..."
2024-08-02 16:49:54 +00:00
sudo nixos-rebuild switch --show-trace --flake .
2024-07-28 15:29:56 +00:00
reset_bspwm
else
echo "Committing flake and pushing..."
commit_and_apply
fi
2024-07-28 17:16:30 +00:00
echo "\nOperation completed."
2023-06-16 23:32:12 +00:00
fi