mytools/nswitch
2024-12-14 20:35:04 +01:00

38 lines
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
exit_with_new_shell() {
printf '\n'
printf '>>> opening a new \"%s\" shell\n' "$(basename "$SHELL")"
printf '>>> cwd: %s\n' "$(pwd)"
"$SHELL"
exit 0
}
cd ~/Projects/NixOS/ || exit 1
nvim
printf ">>> auto formating ..."
nix fmt . &> /dev/null
printf " DONE!\n"
[ -z "$(git diff -U0)" ] && echo ">>> no changes, exiting ..." && exit_with_new_shell
git diff -U0 # "*.nix" # 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
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