Chore: logic improvements for nixos-rebuild script
This commit is contained in:
parent
b34a43ed4c
commit
4294c03219
|
@ -4,23 +4,36 @@
|
||||||
# Configuration parameters
|
# Configuration parameters
|
||||||
operation="switch" # The nixos-rebuild operation to use
|
operation="switch" # The nixos-rebuild operation to use
|
||||||
hostname=$(hostname) # The name of the host to build
|
hostname=$(hostname) # The name of the host to build
|
||||||
flakeDir="." # Path to the flake file (and optionally the hostname)
|
flakeDir="${FLAKE_DIR}" # Path to the flake file (and optionally the hostname). Defaults to the FLAKE_DIR environment variable.
|
||||||
remainingArgs="" # All remaining arguments that haven't been processed
|
remainingArgs="" # All remaining arguments that haven't been processed
|
||||||
commit=true # Whether to update git (true by default)
|
update=true # Whether to update git (true by default)
|
||||||
|
user=$(whoami) # Which user account to use for git commands.
|
||||||
|
|
||||||
function usage() {
|
function usage() {
|
||||||
echo "Usage: nixos-upgrade-script.sh [-o|--operation operation] [-f|--flake path-to-flake-file] [extra nixos-rebuild parameters]"
|
echo "nixos-rebuild Operations Script (NOS) updates your system and your flake.lock file by pulling the latest versions."
|
||||||
|
echo ""
|
||||||
|
echo "Running the script with no parameters performs the following operations:"
|
||||||
|
echo " 1. Pull the latest version of the config"
|
||||||
|
echo " 2. Update your flake.lock file"
|
||||||
|
echo " 3. Commit any changes back to the repository"
|
||||||
|
echo " 4. Run 'nixos-rebuild switch'."
|
||||||
|
echo ""
|
||||||
|
echo "Advanced usage: nixos-upgrade-script.sh [-o|--operation operation] [-f|--flake path-to-flake] [extra nixos-rebuild parameters]"
|
||||||
echo "Options:"
|
echo "Options:"
|
||||||
echo " -h | --help Show this help screen."
|
echo " -h, --help Show this help screen."
|
||||||
echo " -o | --operation The nixos-rebuild operation to perform."
|
echo " -o, --operation The nixos-rebuild operation to perform."
|
||||||
echo " -f | --flake <path> The path to the flake file."
|
echo " -f, --flake <path> The path to your flake.nix file (and optionally, the hostname to build)."
|
||||||
echo " -n | --no-commit Don't update and commit the lock file."
|
echo " -n, --no-update Don't update and commit the lock file."
|
||||||
|
echo " -u, --user Which user account to run git commands under."
|
||||||
|
echo ""
|
||||||
exit 2
|
exit 2
|
||||||
}
|
}
|
||||||
|
|
||||||
function run_operation {
|
function run_operation {
|
||||||
echo "Running this operation: nixos-rebuild $1 --flake $flakeDir $remainingArgs --use-remote-sudo"
|
options="--flake $flakeDir $remainingArgs --use-remote-sudo --log-format multiline-with-logs"
|
||||||
nixos-rebuild $operation --flake $flakeDir $remainingArgs --use-remote-sudo --log-format multiline-with-logs
|
|
||||||
|
echo "Running this operation: nixos-rebuild $1 $options"
|
||||||
|
nixos-rebuild $operation $options
|
||||||
}
|
}
|
||||||
|
|
||||||
# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
|
# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
|
||||||
|
@ -32,8 +45,8 @@ while [[ $# -gt 0 ]]; do
|
||||||
shift
|
shift
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--no-commit|-n)
|
--no-update|-n)
|
||||||
commit=false
|
update=false
|
||||||
shift
|
shift
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
@ -42,6 +55,11 @@ while [[ $# -gt 0 ]]; do
|
||||||
shift
|
shift
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
--user|-u)
|
||||||
|
user="$2"
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
;;
|
||||||
--help|-h)
|
--help|-h)
|
||||||
usage
|
usage
|
||||||
shift
|
shift
|
||||||
|
@ -55,22 +73,22 @@ done
|
||||||
remainingArgs=${POSITIONAL_ARGS[@]}
|
remainingArgs=${POSITIONAL_ARGS[@]}
|
||||||
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
||||||
|
|
||||||
if [ -z "${FLAKE_DIR}" ]; then
|
if [ -n "${flakeDir}" ]; then
|
||||||
echo "Flake directory not specified. Use '--flake [directory]' or set the $FLAKE_DIR environment variable."
|
echo "Flake directory not specified. Use '--flake <path>' or set \$FLAKE_DIR."
|
||||||
exit 1
|
exit 1
|
||||||
else
|
|
||||||
flakeDir=$FLAKE_DIR
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cd $flakeDir
|
cd $flakeDir
|
||||||
|
|
||||||
echo "Pulling the latest version of the repository..."
|
echo "Pulling the latest version of the repository..."
|
||||||
git pull
|
sudo -u $user git pull
|
||||||
|
|
||||||
if [ $commit = true ]; then
|
if [ $update = true ]; then
|
||||||
echo "Checking for updates..."
|
echo "Checking for updates..."
|
||||||
nix flake update --commit-lock-file
|
sudo -u $user nix flake update --commit-lock-file
|
||||||
git push
|
sudo -u $user git push
|
||||||
|
else
|
||||||
|
echo "Skipping 'nix flake update'..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
run_operation $operation
|
run_operation $operation
|
||||||
|
|
Loading…
Reference in a new issue