mytools/nswitch
2025-01-01 15:19:15 +01:00

52 lines
1.3 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
INITIAL_PWD="$(pwd)"
PROJECT="$HOME/Projects/NixOS"
GIT_PATTERN="." # "*.nix"
exit_with_new_shell() {
# check if already in directory
[[ "$INITIAL_PWD" = "$PROJECT" ]] && printf '>>> already in directory\n' && exit 0
printf '\n'
printf '>>> opening a new \"%s\" shell\n' "$(basename "$SHELL")"
printf ' cwd: %s\n' "$(pwd)"
"$SHELL"
exit 0
}
cd "$PROJECT" || exit 1
nvim
# check if there are changes at all
[[ -z "$(git diff -U0 "$GIT_PATTERN")" ]] && echo ">>> no changes, exiting ..." && exit_with_new_shell
# only then do auto formating
printf ">>> auto formating ..."
nix fmt . &> /dev/null
printf " DONE!\n"
# and then show the user a diff
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