mytools/nswitch

44 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# from https://github.com/0atman/noboilerplate/blob/main/scripts/38-nixos.md#dont-use-nix-env
# docstring=quick edit (or ling xD) for nixos config
set -E -o pipefail
shopt -s failglob
export LC_ALL=C.UTF8
PROJECT="$HOME/Projects/NixOS/"
GIT_PATTERN="." # "*.nix"
exit_with_new_shell() {
printf '\n'
printf '>>> opening a new \"%s\" shell\n' "$(basename "$SHELL")"
printf '>>> cwd: %s\n' "$(pwd)"
"$SHELL"
exit 0
}
cd "$PROJECT" || exit 1
nvim
printf ">>> auto formating ..."
nix fmt . &> /dev/null
printf " DONE!\n"
[ -z "$(git diff -U0 "$GIT_PATTERN")" ] && echo ">>> no changes, exiting ..." && exit_with_new_shell
git diff -U0 "$GIT_PATTERN" # as a seperate command so we get nice colors and pager
printf ">>> Do you want to rebuild NixOS? [y/N]\n"
printf "<<< " && read -r YESNO
if ! [[ "${YESNO,,}" == "y" ]]; then
exit_with_new_shell
fi
git add "$GIT_PATTERN"
printf ">>> Rebuilding NixOS\n"
sudo nixos-rebuild switch | tee nixos-switch.log || (
grep --color error nixos-switch.log && false)
gen=$(nixos-rebuild list-generations | grep current)
git commit -am "$gen"
exit_with_new_shell