2024-09-28 04:47:38 +00:00
|
|
|
#!/usr/bin/env bash
|
2024-10-15 14:22:53 +00:00
|
|
|
# The NixOS Operations Script (NOS) is a wrapper script for nixos-rebuild and Flake-based configurations.
|
|
|
|
# It handles pulling the latest version of your repository using Git, running system updates, and pushing changes back up.
|
2024-09-28 04:47:38 +00:00
|
|
|
|
|
|
|
# Configuration parameters
|
2024-10-02 18:02:58 +00:00
|
|
|
operation="switch" # The nixos-rebuild operation to use
|
|
|
|
hostname=$(/run/current-system/sw/bin/hostname) # The name of the host to build
|
2024-10-15 14:22:53 +00:00
|
|
|
flakeDir="${FLAKE_DIR}" # Path to the flake file (and optionally the hostname)
|
|
|
|
update=false # Whether to update and commmit flake.lock
|
|
|
|
user=$(/run/current-system/sw/bin/whoami) # Which user account to use for git commands
|
2024-10-02 18:02:58 +00:00
|
|
|
remainingArgs="" # All remaining arguments that haven't yet been processed (will be passed to nixos-rebuild)
|
2024-09-28 04:47:38 +00:00
|
|
|
|
|
|
|
function usage() {
|
2024-10-06 16:13:36 +00:00
|
|
|
echo "The NixOS Operations Script (NOS) is a nixos-rebuild wrapper for system maintenance."
|
2024-09-30 13:33:15 +00:00
|
|
|
echo ""
|
|
|
|
echo "Running the script with no parameters performs the following operations:"
|
2024-10-06 16:13:36 +00:00
|
|
|
echo " 1. Pull the latest version of your Nix config repository"
|
|
|
|
echo " 2. Run 'nixos-rebuild switch'."
|
2024-09-30 13:33:15 +00:00
|
|
|
echo ""
|
2024-10-15 14:22:53 +00:00
|
|
|
echo "Advanced usage: nixos-operations-script.sh [-h | --hostname hostname-to-build] [-o | --operation operation] [-f | --flake path-to-flake] [extra nixos-rebuild parameters]"
|
|
|
|
echo ""
|
2024-09-28 04:47:38 +00:00
|
|
|
echo "Options:"
|
2024-10-15 14:22:53 +00:00
|
|
|
echo " --help Show this help screen."
|
|
|
|
echo " -f, --flake [path] The path to your flake.nix file (defualts to the FLAKE_DIR environment variable)."
|
|
|
|
echo " -h, --hostname [hostname] The name of the host to build (defaults to the current system's hostname)."
|
|
|
|
echo " -o, --operation [operation] The nixos-rebuild operation to perform (defaults to 'switch')."
|
|
|
|
echo " -U, --update Update and commit the flake.lock file."
|
|
|
|
echo " -u, --user [username] Which user account to run git commands under (defaults to the user running this script)."
|
2024-09-30 13:33:15 +00:00
|
|
|
echo ""
|
2024-10-15 14:22:53 +00:00
|
|
|
exit 0
|
2024-09-28 04:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
|
|
|
|
POSITIONAL_ARGS=()
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
--flake|-f)
|
|
|
|
flakeDir="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-10-15 14:22:53 +00:00
|
|
|
--hostname|-h)
|
|
|
|
hostname="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-10-02 18:02:58 +00:00
|
|
|
--update|--upgrade|-U)
|
|
|
|
update=true
|
2024-09-28 04:47:38 +00:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--operation|-o)
|
|
|
|
operation="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-09-30 13:33:15 +00:00
|
|
|
--user|-u)
|
|
|
|
user="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
2024-10-15 14:22:53 +00:00
|
|
|
--help)
|
2024-09-28 04:47:38 +00:00
|
|
|
usage
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
2024-10-01 14:45:29 +00:00
|
|
|
shift
|
2024-09-28 04:47:38 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
remainingArgs=${POSITIONAL_ARGS[@]}
|
|
|
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
|
|
|
|
2024-09-30 13:33:15 +00:00
|
|
|
if [ -z "${flakeDir}" ]; then
|
|
|
|
echo "Flake directory not specified. Use '--flake <path>' or set \$FLAKE_DIR."
|
2024-09-29 20:06:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-09-28 04:47:38 +00:00
|
|
|
cd $flakeDir
|
2024-09-29 20:06:16 +00:00
|
|
|
|
|
|
|
echo "Pulling the latest version of the repository..."
|
2024-09-30 19:32:49 +00:00
|
|
|
/run/wrappers/bin/sudo -u $user git pull
|
2024-09-28 04:47:38 +00:00
|
|
|
|
2024-09-30 13:33:15 +00:00
|
|
|
if [ $update = true ]; then
|
2024-10-01 14:45:29 +00:00
|
|
|
echo "Updating flake.lock..."
|
|
|
|
/run/wrappers/bin/sudo -u $user nix flake update --commit-lock-file && /run/wrappers/bin/sudo -u $user git push
|
2024-09-30 13:33:15 +00:00
|
|
|
else
|
|
|
|
echo "Skipping 'nix flake update'..."
|
2024-09-28 04:47:38 +00:00
|
|
|
fi
|
|
|
|
|
2024-10-15 14:22:53 +00:00
|
|
|
options="--flake ${flakeDir}#${hostname} ${remainingArgs} --use-remote-sudo --log-format multiline-with-logs"
|
2024-09-30 19:32:49 +00:00
|
|
|
|
2024-10-15 14:22:53 +00:00
|
|
|
echo "Running this operation: nixos-rebuild ${operation} ${options}"
|
2024-10-18 19:22:21 +00:00
|
|
|
/run/current-system/sw/bin/nixos-rebuild $operation $options
|
2024-09-28 04:47:38 +00:00
|
|
|
|
|
|
|
exit 0
|