#!/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" 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() { # check if already in directory [[ "$INITIAL_PWD" = "$PROJECT" ]] && printf '>>> already in directory\n' && exit 0 cd "$PROJECT" || exit 1 printf '\n' printf '>>> opening a new \"%s\" shell\n' "$(basename "$SHELL")" printf ' cwd: %s\n' "$(pwd)" "$SHELL" exit 0 } ask_for_rebuild() { printf ">>> Do you want to rebuild NixOS? [y/N]\n" printf "<<< " && read -r YESNO if ! [[ "${YESNO,,}" == "y" ]]; then exit_with_new_shell 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" git add "$GIT_PATTERN" rebuild gen=$(nixos-rebuild list-generations | grep current) printf ">>> commiting to git\n" git commit -am "$gen" exit_with_new_shell } update() { nix flake update ask_for_rebuild 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 quick_edit elif [[ "$1" = "--edit" || "$1" = "-e" ]]; then "$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 exit_with_new_shell elif [[ "$1" = "--help" || "$1" = "-h" ]]; then help_msg else help_msg printf "=================\n" printf "didn't recognise: %s\n" "$1" exit 1 fi