way more options

This commit is contained in:
Ranomier 2025-02-06 13:59:18 +01:00
parent 0093a34d48
commit 398c57c782

81
nswitch
View file

@ -8,6 +8,10 @@ export LC_ALL=C.UTF8
INITIAL_PWD="$(pwd)" INITIAL_PWD="$(pwd)"
PROJECT="$HOME/Projects/NixOS" PROJECT="$HOME/Projects/NixOS"
GIT_PATTERN="." # "*.nix" GIT_PATTERN="." # "*.nix"
SELF_EDITOR="$EDITOR" # here you can overwrite which editor should be used
# switch into the project for the whole script
cd "$PROJECT" || exit 1
exit_with_new_shell() { exit_with_new_shell() {
@ -23,33 +27,48 @@ exit_with_new_shell() {
exit 0 exit 0
} }
quick_edit() {
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
ask_for_rebuild() {
printf ">>> Do you want to rebuild NixOS? [y/N]\n" printf ">>> Do you want to rebuild NixOS? [y/N]\n"
printf "<<< " && read -r YESNO printf "<<< " && read -r YESNO
if ! [[ "${YESNO,,}" == "y" ]]; then if ! [[ "${YESNO,,}" == "y" ]]; then
exit_with_new_shell exit_with_new_shell
fi fi
}
rebuild() {
printf ">>> Rebuilding NixOS\n"
sudo nixos-rebuild switch | tee nixos-switch.log || (
grep --color error nixos-switch.log && false)
}
autoformat() {
printf ">>> auto formating ..."
nix fmt . &> /dev/null
printf " DONE!\n"
}
quick_edit() {
"$SELF_EDITOR"
# 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
autoformat
# and then show the user a diff
git diff -U0 "$GIT_PATTERN" # as a seperate command so we get nice colors and pager
ask_for_rebuild
printf ">>> Add everything to git staging so nix can find it\n" printf ">>> Add everything to git staging so nix can find it\n"
git add "$GIT_PATTERN" git add "$GIT_PATTERN"
printf ">>> Rebuilding NixOS\n" rebuild
sudo nixos-rebuild switch | tee nixos-switch.log || (
grep --color error nixos-switch.log && false)
gen=$(nixos-rebuild list-generations | grep current) gen=$(nixos-rebuild list-generations | grep current)
printf ">>> commiting to git\n" printf ">>> commiting to git\n"
@ -59,20 +78,36 @@ quick_edit() {
} }
help_msg() { update() {
printf -- ' without arguments start the main loop\n' nix flake update
printf -- '--help | -h -> show this message\n' ask_for_rebuild
printf -- '--new-shell-in-project | -s -> show this message\n' rebuild
} }
help_msg() {
printf -- '# only one option can be selected\n'
printf -- ' without arguments -> quick edit mode\n'
printf -- '--edit | -e -> only edit\n'
printf -- '--update | -u -> update the system\n'
printf -- '--autoformat | -f -> only autoformat\n'
printf -- '--new-shell-in-project | -s -> start new shell, with cwd in the project\n'
printf -- '--help | -h -> show this message\n'
}
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
quick_edit quick_edit
elif [[ "$1" = "--help" || "$1" = "-h" ]]; then elif [[ "$1" = "--edit" || "$1" = "-e" ]]; then
help_msg "$SELF_EDITOR"
elif [[ "$1" = "--update" || "$1" = "-u" ]]; then
update
elif [[ "$1" = "--autoformat" || "$1" = "-f" ]]; then
autoformat
elif [[ "$1" = "--new-shell-in-project" || "$1" = "-s" ]]; then elif [[ "$1" = "--new-shell-in-project" || "$1" = "-s" ]]; then
exit_with_new_shell exit_with_new_shell
elif [[ "$1" = "--help" || "$1" = "-h" ]]; then
help_msg
else else
help_msg help_msg
printf "=================\n" printf "=================\n"