This commit is contained in:
commit
95a533c876
451 changed files with 18255 additions and 0 deletions
135
apps/create/create.sh
Normal file
135
apps/create/create.sh
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Default values
|
||||
GIT_NAME=""
|
||||
GIT_EMAIL=""
|
||||
FLAKE="$HOME/.config/nixos"
|
||||
TEMPLATE=""
|
||||
USERNAME=""
|
||||
HOSTNAME=""
|
||||
|
||||
# Templates with Home Manager configurations
|
||||
HM_CONFIGS=("hyprland")
|
||||
|
||||
# This will get overwritten by the derivation
|
||||
TEMPLATES_DIR=""
|
||||
|
||||
# Print usage information
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $0 -t|--template TEMPLATE -u|--user USERNAME -H|--host HOSTNAME [-f|--flake PATH/TO/YOUR/NIX-CONFIG] [--git-name GIT_NAME] [--git-email GIT_EMAIL]
|
||||
|
||||
Options:
|
||||
-t, --template TEMPLATE Configuration template to use (mandatory)
|
||||
-u, --user USERNAME Specify the username (mandatory)
|
||||
-H, --host HOSTNAME Specify the hostname (mandatory)
|
||||
-f, --flake FLAKE Path to your flake directory (optional, default: $FLAKE)
|
||||
--git-name GIT_NAME Specify the git name (optional, default: USERNAME)
|
||||
--git-email GIT_EMAIL Specify the git email (optional, default: USERNAME@HOSTNAME)
|
||||
-h, --help Show this help message
|
||||
|
||||
Available configuration templates:
|
||||
hetzner-amd
|
||||
hyprland
|
||||
pi4
|
||||
server
|
||||
vm-uefi
|
||||
EOF
|
||||
}
|
||||
|
||||
# Replace placeholder strings in files
|
||||
recursive_replace() {
|
||||
local search=$1
|
||||
local replace=$2
|
||||
local dir=$3
|
||||
|
||||
find "$dir" -type f -exec sed -i "s/$search/$replace/g" {} +
|
||||
}
|
||||
|
||||
# mv wrapper
|
||||
rename_files() {
|
||||
local from=$1
|
||||
local to=$2
|
||||
|
||||
if [[ -d "$from" ]]; then
|
||||
mv "$from" "$to"
|
||||
else
|
||||
echo "Error: Directory $from not found."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Returns true if template uses Home Manager
|
||||
has_hm() {
|
||||
local template="$1"
|
||||
|
||||
for hm_config in "${HM_CONFIGS[@]}"; do
|
||||
if [[ "$template" == "$hm_config" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# Parse command-line arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-u|--user)
|
||||
USERNAME=$2
|
||||
shift; shift ;;
|
||||
-H|--host)
|
||||
HOSTNAME=$2
|
||||
shift; shift ;;
|
||||
-f|--flake)
|
||||
FLAKE=$2
|
||||
shift; shift ;;
|
||||
-t|--template)
|
||||
TEMPLATE=$2
|
||||
shift; shift ;;
|
||||
--git-name)
|
||||
GIT_NAME=$2
|
||||
shift; shift ;;
|
||||
--git-email)
|
||||
GIT_EMAIL=$2
|
||||
shift; shift ;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0 ;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
usage
|
||||
exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Validate mandatory arguments
|
||||
if [[ -z $USERNAME ]] || [[ -z $HOSTNAME ]] || [[ -z $TEMPLATE ]]; then
|
||||
echo "Error: Missing mandatory arguments."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Assign default values for optional arguments
|
||||
GIT_NAME=${GIT_NAME:-$USERNAME}
|
||||
GIT_EMAIL=${GIT_EMAIL:-"$USERNAME@$HOSTNAME"}
|
||||
|
||||
# Apply template to flake directory
|
||||
mkdir -p "$FLAKE"
|
||||
cd "$FLAKE" || { echo "Error: Cannot change directory to $FLAKE"; exit 1; }
|
||||
nix flake init -t "git+https://git.sid.ovh/sid/synix#templates.$TEMPLATE"
|
||||
|
||||
# Move generated files
|
||||
rename_files "$FLAKE/hosts/HOSTNAME" "$FLAKE/hosts/$HOSTNAME"
|
||||
rename_files "$FLAKE/users/USERNAME" "$FLAKE/users/$USERNAME"
|
||||
|
||||
# Only check for HM config if the template has one
|
||||
has_hm "$TEMPLATE" && rename_files "$FLAKE/users/$USERNAME/home/hosts/HOSTNAME" "$FLAKE/users/$USERNAME/home/hosts/$HOSTNAME"
|
||||
|
||||
# Replace placeholders recursively
|
||||
recursive_replace "USERNAME" "$USERNAME" "$FLAKE"
|
||||
recursive_replace "HOSTNAME" "$HOSTNAME" "$FLAKE"
|
||||
recursive_replace "GIT_NAME" "$GIT_NAME" "$FLAKE"
|
||||
recursive_replace "GIT_EMAIL" "$GIT_EMAIL" "$FLAKE"
|
||||
|
||||
echo "Template $TEMPLATE successfully applied to $FLAKE."
|
||||
24
apps/create/default.nix
Normal file
24
apps/create/default.nix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
stdenv,
|
||||
coreutils,
|
||||
...
|
||||
}:
|
||||
|
||||
stdenv.mkDerivation rec {
|
||||
pname = "create";
|
||||
version = "2.0";
|
||||
|
||||
src = ./.;
|
||||
|
||||
nativeBuildInputs = [ coreutils ];
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
mkdir -p $out/share
|
||||
|
||||
cp create.sh $out/bin/${pname}
|
||||
chmod +x $out/bin/${pname}
|
||||
'';
|
||||
|
||||
meta.mainProgram = "create";
|
||||
}
|
||||
18
apps/deploy/default.nix
Normal file
18
apps/deploy/default.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
writeShellApplication,
|
||||
jq,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
name = "deploy";
|
||||
text = builtins.readFile ./${name}.sh;
|
||||
in
|
||||
writeShellApplication {
|
||||
inherit name text;
|
||||
meta.mainProgram = name;
|
||||
|
||||
runtimeInputs = [
|
||||
jq
|
||||
];
|
||||
}
|
||||
84
apps/deploy/deploy.sh
Normal file
84
apps/deploy/deploy.sh
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# defaults
|
||||
FLAKE_URI="."
|
||||
CONFIG_FILE="./deploy.json"
|
||||
ACTION="switch"
|
||||
USE_SUDO=true
|
||||
DO_BUILD=true
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $(basename "$0") [OPTIONS] [ACTION]
|
||||
|
||||
Arguments:
|
||||
ACTION switch | boot | test (Default: $ACTION)
|
||||
|
||||
Options:
|
||||
-f, --flake URI URI of the flake (Default: $FLAKE_URI)
|
||||
-c, --config FILE Deployment config file (Default: $CONFIG_FILE)
|
||||
--no-sudo Do not pass sudo-related flags to nixos-rebuild.
|
||||
--skip-build Skip the explicit 'build' step before deployment.
|
||||
-h, --help Show this help.
|
||||
EOF
|
||||
}
|
||||
|
||||
_status() { echo -e "\033[0;34m> $1\033[0m"; }
|
||||
success() { echo -e "\033[0;32m$1\033[0m"; }
|
||||
error() { echo -e "\033[0;31mError: $1\033[0m" >&2; exit 1; }
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
switch|boot|test) ACTION="$1"; shift ;;
|
||||
-f|--flake) FLAKE_URI="$2"; shift 2 ;;
|
||||
-c|--config) CONFIG_FILE="$2"; shift 2 ;;
|
||||
--no-sudo) USE_SUDO=false; shift ;;
|
||||
--skip-build) DO_BUILD=false; shift ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) error "Invalid argument '$1'" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
command -v jq &> /dev/null || error "jq is not installed."
|
||||
[ -f "$CONFIG_FILE" ] || error "Config '$CONFIG_FILE' not found."
|
||||
|
||||
BUILD_HOST=$(jq -r '.buildHost // "localhost"' "$CONFIG_FILE")
|
||||
[[ "$BUILD_HOST" =~ ^(127\.0\.0\.1|::1)$ ]] && BUILD_HOST="localhost"
|
||||
|
||||
mapfile -t HOST_ENTRIES < <(jq -r '.hosts[] | "\(.name) \(.address)"' "$CONFIG_FILE")
|
||||
[ ${#HOST_ENTRIES[@]} -eq 0 ] && error "No hosts defined in $CONFIG_FILE"
|
||||
|
||||
echo "Action: $ACTION"
|
||||
echo "Flake: $FLAKE_URI"
|
||||
echo "Builder: $BUILD_HOST"
|
||||
|
||||
if [ "$DO_BUILD" = true ]; then
|
||||
_status "Building configurations..."
|
||||
for entry in "${HOST_ENTRIES[@]}"; do
|
||||
read -r name address <<< "$entry"
|
||||
echo "------------------------------------------------"
|
||||
echo "Building host '$name':"
|
||||
|
||||
CMD=("nixos-rebuild" "build" "--flake" "${FLAKE_URI}#${name}")
|
||||
[[ "$BUILD_HOST" != "localhost" ]] && CMD+=("--build-host" "$BUILD_HOST")
|
||||
|
||||
"${CMD[@]}" || error "Build failed for $name"
|
||||
success "Build for host '$name' successful."
|
||||
done
|
||||
fi
|
||||
|
||||
_status "Deploying to targets..."
|
||||
for entry in "${HOST_ENTRIES[@]}"; do
|
||||
read -r name address <<< "$entry"
|
||||
echo "------------------------------------------------"
|
||||
echo "Deploying to host '$name' ($address):"
|
||||
|
||||
CMD=("nixos-rebuild" "$ACTION" "--flake" "${FLAKE_URI}#${name}" "--target-host" "$address")
|
||||
[[ "$BUILD_HOST" != "localhost" ]] && CMD+=("--build-host" "$BUILD_HOST")
|
||||
[[ "$USE_SUDO" = true ]] && CMD+=("--sudo" "--ask-sudo-password")
|
||||
|
||||
"${CMD[@]}" || error "Activation failed for $name"
|
||||
success "Host '$name' updated."
|
||||
done
|
||||
|
||||
success "Deployment complete."
|
||||
18
apps/install/default.nix
Normal file
18
apps/install/default.nix
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
writeShellApplication,
|
||||
git,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
name = "install";
|
||||
text = builtins.readFile ./${name}.sh;
|
||||
in
|
||||
writeShellApplication {
|
||||
inherit name text;
|
||||
meta.mainProgram = name;
|
||||
|
||||
runtimeInputs = [
|
||||
git
|
||||
];
|
||||
}
|
||||
173
apps/install/install.sh
Executable file
173
apps/install/install.sh
Executable file
|
|
@ -0,0 +1,173 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# NixOS install script
|
||||
|
||||
|
||||
### VARIABLES ###
|
||||
|
||||
ASK_VERIFICATION=1 # Default to ask for verification
|
||||
CONFIG_DIR="/tmp/nixos" # Directory to copy flake to / clone flake into
|
||||
GIT_BRANCH="master" # Default Git branch
|
||||
GIT_REPO="" # Git repository URL
|
||||
HOSTNAME="" # Hostname
|
||||
MNT="/mnt" # root mount point
|
||||
SEPARATOR="________________________________________" # line separator
|
||||
|
||||
### FUNCTIONS ###
|
||||
|
||||
# Function to display help information
|
||||
Show_help() {
|
||||
echo "Usage: $0 [-r REPO] [-n HOSTNAME] [-b BRANCH] [-y] [-h]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -r, --repo REPO Your NixOS configuration Git repository URL"
|
||||
echo " -n, --hostname HOSTNAME Specify the hostname for the NixOS configuration"
|
||||
echo " -b, --branch BRANCH Specify the Git branch to use (default: $GIT_BRANCH)"
|
||||
echo " -y, --yes Do not ask for user verification before proceeding"
|
||||
echo " -h, --help Show this help message and exit"
|
||||
}
|
||||
|
||||
# Function to format, partition, and mount disks for $HOSTNAME using disko
|
||||
Run_disko() {
|
||||
echo "$SEPARATOR"
|
||||
echo "Running disko..."
|
||||
nix --experimental-features "nix-command flakes" run github:nix-community/disko/latest -- --mode disko "$CONFIG_DIR"/hosts/"$HOSTNAME"/disks.nix
|
||||
}
|
||||
|
||||
# Function to format, partition, and mount disks for $HOSTNAME using a partitioning script
|
||||
Run_script() {
|
||||
echo "$SEPARATOR"
|
||||
echo "Running partitioning script..."
|
||||
bash "$CONFIG_DIR"/hosts/"$HOSTNAME"/disks.sh
|
||||
}
|
||||
|
||||
# Function to check mount points and partitioning
|
||||
Check_partitioning() {
|
||||
echo "$SEPARATOR"
|
||||
echo "Printing mount points and partitioning..."
|
||||
mount | grep "$MNT"
|
||||
lsblk -f
|
||||
[[ "$ASK_VERIFICATION" == 1 ]] && read -rp "Verify the mount points and partitioning. Press Ctrl+c to cancel or Enter to continue..."
|
||||
}
|
||||
|
||||
# Function to generate hardware configuration
|
||||
Generate_hardware_config() {
|
||||
[[ "$ASK_VERIFICATION" == 1 ]] && read -rp "No hardware configuration found. Press Ctrl+c to cancel or Enter to generate one..."
|
||||
|
||||
echo "$SEPARATOR"
|
||||
echo "Generating hardware configuration..."
|
||||
nixos-generate-config --root "$MNT" --show-hardware-config > "$CONFIG_DIR"/hosts/"$HOSTNAME"/hardware.nix
|
||||
|
||||
# Check if hardware configuration has been generated
|
||||
if [[ ! -f "$CONFIG_DIR"/hosts/"$HOSTNAME"/hardware.nix ]]; then
|
||||
echo "Error: Hardware configuration cannot be generated."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Add configuration to git
|
||||
# TODO: get rid of cd
|
||||
cd "$CONFIG_DIR"/hosts/"$HOSTNAME" || exit 1
|
||||
git add "$CONFIG_DIR"/hosts/"$HOSTNAME"/hardware.nix
|
||||
cd || exit 1
|
||||
|
||||
echo "Hardware configuration generated successfully."
|
||||
};
|
||||
|
||||
# Function to install configuration for $HOSTNAME
|
||||
Install() {
|
||||
# Check if hardware configuration exists
|
||||
[[ ! -f "$CONFIG_DIR"/hosts/"$HOSTNAME"/hardware.nix ]] && Generate_hardware_config
|
||||
|
||||
echo "$SEPARATOR"
|
||||
echo "Installing NixOS..."
|
||||
nixos-install --root "$MNT" --no-root-password --flake "$CONFIG_DIR"#"$HOSTNAME" && echo "You can reboot the system now."
|
||||
}
|
||||
|
||||
### PARSE ARGUMENTS ###
|
||||
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case $1 in
|
||||
-r|--repo) GIT_REPO="$2"; shift ;;
|
||||
-b|--branch) GIT_BRANCH="$2"; shift ;;
|
||||
-y|--yes) ASK_VERIFICATION=0 ;;
|
||||
-h|--help) Show_help; exit 0 ;;
|
||||
-n|--hostname) HOSTNAME="$2"; shift ;;
|
||||
*) echo "Unknown option: $1"; Show_help; exit 1 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
### PREREQUISITES ###
|
||||
|
||||
echo "$SEPARATOR"
|
||||
mkdir -p "$CONFIG_DIR"
|
||||
|
||||
# Clone NixOS configuration from $GIT_REPO if provided
|
||||
if [[ ! -z "$GIT_REPO" ]]; then
|
||||
# Install git if not already installed
|
||||
if ! command -v git &> /dev/null; then
|
||||
echo "Git is not installed. Installing..."
|
||||
nix-env -iA nixos.git
|
||||
fi
|
||||
|
||||
# Clone Git repo if directory is empty
|
||||
if [[ -z "$(ls -A "$CONFIG_DIR" 2>/dev/null)" ]]; then
|
||||
echo "Cloning NixOS configuration repo..."
|
||||
git clone --depth 1 -b "$GIT_BRANCH" "$GIT_REPO" "$CONFIG_DIR"
|
||||
|
||||
# Check if git repository has been cloned
|
||||
if [[ ! -d "$CONFIG_DIR"/.git ]]; then
|
||||
echo "Error: Git repository could not be cloned."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "$CONFIG_DIR is not empty. Skip cloning $GIT_REPO."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ ! -f "$CONFIG_DIR"/flake.nix ]]; then
|
||||
echo "Error: $CONFIG_DIR does not contain 'flake.nix'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
### CHOOSE CONFIG ###
|
||||
|
||||
# If hostname is not provided via options, prompt the user
|
||||
if [[ -z "$HOSTNAME" ]]; then
|
||||
# Get list of available hostnames
|
||||
HOSTNAMES=$(ls "$CONFIG_DIR"/hosts)
|
||||
|
||||
echo "$SEPARATOR"
|
||||
echo "Please choose a hostname to install its NixOS configuration."
|
||||
echo "$HOSTNAMES"
|
||||
read -rp "Enter hostname: " HOSTNAME
|
||||
|
||||
# Check if hostname is empty
|
||||
if [[ -z "$HOSTNAME" ]]; then
|
||||
echo "Error: Hostname cannot be empty."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
### INSTALLATION ###
|
||||
|
||||
# Check if NixOS configuration exists
|
||||
if [[ -d "$CONFIG_DIR"/hosts/"$HOSTNAME" ]]; then
|
||||
|
||||
# Check for existing disko configuration
|
||||
if [[ -f "$CONFIG_DIR"/hosts/"$HOSTNAME"/disks.nix ]]; then
|
||||
Run_disko || ( echo "Error: disko failed." && exit 1 )
|
||||
# Check for partitioning script
|
||||
elif [[ -f "$CONFIG_DIR"/hosts/"$HOSTNAME"/disks.sh ]]; then
|
||||
Run_script || ( echo "Error: Partitioning script failed." && exit 1 )
|
||||
else
|
||||
echo "Error: No disko configuration (disks.nix) or partitioning script (disks.sh) found for host '$HOSTNAME'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Check_partitioning
|
||||
Install || ( echo "Error: Installation failed." && exit 1 )
|
||||
else
|
||||
echo "Error: Configuration for host '$HOSTNAME' does not exist."
|
||||
exit 1
|
||||
fi
|
||||
30
apps/rebuild/default.nix
Normal file
30
apps/rebuild/default.nix
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
writeShellApplication,
|
||||
coreutils,
|
||||
gnugrep,
|
||||
gnused,
|
||||
home-manager,
|
||||
hostname,
|
||||
nix,
|
||||
nixos-rebuild,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
name = "rebuild";
|
||||
text = builtins.readFile ./${name}.sh;
|
||||
in
|
||||
writeShellApplication {
|
||||
inherit name text;
|
||||
meta.mainProgram = name;
|
||||
|
||||
runtimeInputs = [
|
||||
coreutils
|
||||
gnugrep
|
||||
gnused
|
||||
home-manager
|
||||
hostname
|
||||
nix
|
||||
nixos-rebuild
|
||||
];
|
||||
}
|
||||
246
apps/rebuild/rebuild.sh
Executable file
246
apps/rebuild/rebuild.sh
Executable file
|
|
@ -0,0 +1,246 @@
|
|||
# NixOS and standalone Home Manager rebuild script
|
||||
|
||||
# Defaults
|
||||
FLAKE_PATH="$HOME/.config/nixos" # Default flake path
|
||||
HOME_USER="$(whoami)" # Default username. Used to identify the Home Manager configuration
|
||||
NIXOS_HOST="$(hostname)" # Default hostname. Used to identify the NixOS and Home Manager configuration
|
||||
BUILD_HOST="" # Default build host. Empty means localhost
|
||||
TARGET_HOST="" # Default target host. Empty means localhost
|
||||
UPDATE=0 # Default to not update flake repositories
|
||||
UPDATE_INPUTS="" # Default list of inputs to update. Empty means all
|
||||
ROLLBACK=0 # Default to not rollback
|
||||
SHOW_TRACE=0 # Default to not show detailed error messages
|
||||
|
||||
# Function to display the help message
|
||||
Help() {
|
||||
echo "Wrapper script for 'nixos-rebuild switch' and 'home-manager switch' commands."
|
||||
echo "Usage: rebuild <command> [OPTIONS]"
|
||||
echo
|
||||
echo "Commands:"
|
||||
echo " nixos Rebuild NixOS configuration"
|
||||
echo " home Rebuild Home Manager configuration"
|
||||
echo " all Rebuild both NixOS and Home Manager configurations"
|
||||
echo " help Show this help message"
|
||||
echo
|
||||
echo "Options (for NixOS and Home Manager):"
|
||||
echo " -H, --host <host> Specify the hostname (as in 'nixosConfiguraions.<host>'). Default: $NIXOS_HOST"
|
||||
echo " -p, --path <path> Set the path to the flake directory. Default: $FLAKE_PATH"
|
||||
echo " -U, --update [inputs] Update all flake inputs. Optionally provide comma-separated list of inputs to update instead."
|
||||
echo " -r, --rollback Don't build the new configuration, but use the previous generation instead"
|
||||
echo " -t, --show-trace Show detailed error messages"
|
||||
echo
|
||||
echo "NixOS only options:"
|
||||
echo " -B, --build-host <user@example.com> Use a remote host for building the configuration via SSH"
|
||||
echo " -T, --target-host <user@example.com> Deploy the configuration to a remote host via SSH. If '--host' is specified, it will be used as the target host."
|
||||
echo
|
||||
echo "Home Manager only options:"
|
||||
echo " -u, --user <user> Specify the username (as in 'homeConfigurations.<user>@<host>'). Default: $HOME_USER"
|
||||
}
|
||||
|
||||
# Function to handle errors
|
||||
error() {
|
||||
echo "Error: $1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Function to rebuild NixOS configuration
|
||||
Rebuild_nixos() {
|
||||
local FLAKE="$FLAKE_PATH#$NIXOS_HOST"
|
||||
|
||||
# Construct rebuild command
|
||||
local CMD=("nixos-rebuild" "switch" "--sudo")
|
||||
[[ -n "$TARGET_HOST" || -n "$BUILD_HOST" ]] && CMD+=("--ask-sudo-password")
|
||||
CMD+=("--flake" "$FLAKE")
|
||||
[ "$ROLLBACK" = 1 ] && CMD+=("--rollback")
|
||||
[ "$SHOW_TRACE" = 1 ] && CMD+=("--show-trace")
|
||||
[ -n "$BUILD_HOST" ] && CMD+=("--build-host" "$BUILD_HOST")
|
||||
if [ "$NIXOS_HOST" != "$(hostname)" ] && [ -z "$TARGET_HOST" ]; then
|
||||
TARGET_HOST="$NIXOS_HOST"
|
||||
echo "Using '$TARGET_HOST' as target host."
|
||||
fi
|
||||
[ -n "$TARGET_HOST" ] && CMD+=("--target-host" "$TARGET_HOST")
|
||||
|
||||
# Rebuild NixOS configuration
|
||||
if [ "$ROLLBACK" = 0 ]; then
|
||||
echo "Rebuilding NixOS configuration '$FLAKE'..."
|
||||
else
|
||||
echo "Rolling back to last NixOS generation..."
|
||||
fi
|
||||
|
||||
echo "Executing command: ${CMD[*]}"
|
||||
"${CMD[@]}" || error "NixOS rebuild failed"
|
||||
echo "NixOS rebuild completed successfully."
|
||||
}
|
||||
|
||||
# Function to rebuild Home Manager configuration
|
||||
Rebuild_home() {
|
||||
local FLAKE="$FLAKE_PATH#$HOME_USER@$NIXOS_HOST"
|
||||
|
||||
if [ -n "$BUILD_HOST" ] || [ -n "$TARGET_HOST" ]; then
|
||||
error "Remote building is not supported for Home Manager."
|
||||
fi
|
||||
|
||||
# Construct rebuild command
|
||||
local CMD=()
|
||||
if [ "$ROLLBACK" = 1 ]; then
|
||||
local rollback_path
|
||||
rollback_path=$(home-manager generations | sed -n '2p' | grep -o '/nix/store[^ ]*')
|
||||
CMD+=("$rollback_path/activate")
|
||||
else
|
||||
CMD=("home-manager" "switch" "--flake" "$FLAKE")
|
||||
[ "$SHOW_TRACE" = 1 ] && CMD+=("--show-trace")
|
||||
fi
|
||||
|
||||
# Rebuild Home Manager configuration
|
||||
if [ "$ROLLBACK" = 0 ]; then
|
||||
echo "Rebuilding Home Manager configuration '$FLAKE'..."
|
||||
else
|
||||
echo "Rolling back to last Home Manager generation..."
|
||||
fi
|
||||
|
||||
echo "Executing command: ${CMD[*]}"
|
||||
"${CMD[@]}" || error "Home Manager rebuild failed"
|
||||
echo "Home Manager rebuild completed successfully."
|
||||
}
|
||||
|
||||
# Function to Update flake repositories
|
||||
Update() {
|
||||
echo "Updating flake inputs..."
|
||||
|
||||
# Construct update command as an array
|
||||
local CMD=("nix" "flake" "update" "--flake" "$FLAKE_PATH")
|
||||
if [ -n "$UPDATE_INPUTS" ]; then
|
||||
# Split comma-separated inputs and pass them to nix flake update
|
||||
IFS=',' read -ra INPUTS <<< "$UPDATE_INPUTS"
|
||||
for input in "${INPUTS[@]}"; do
|
||||
CMD+=("$input")
|
||||
done
|
||||
fi
|
||||
|
||||
echo "Executing command: ${CMD[*]}"
|
||||
"${CMD[@]}" || error "Failed to update flake repositories"
|
||||
echo "Flake repositories updated successfully."
|
||||
}
|
||||
|
||||
# Parse command-line options
|
||||
if [[ -z "${1:-}" ]]; then
|
||||
echo "Error: No command specified. Printing help page."
|
||||
Help
|
||||
exit 1
|
||||
fi
|
||||
COMMAND=$1
|
||||
shift
|
||||
|
||||
# Handle help command early
|
||||
if [ "$COMMAND" = "help" ] || [ "$COMMAND" = "--help" ] || [ "$COMMAND" = "-h" ]; then
|
||||
Help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "${1:-}" in
|
||||
-H|--host)
|
||||
if [ -n "${2:-}" ]; then
|
||||
NIXOS_HOST="$2"
|
||||
shift 2
|
||||
else
|
||||
error "-H|--host option requires an argument"
|
||||
fi
|
||||
;;
|
||||
-u|--user)
|
||||
if [ -n "${2:-}" ]; then
|
||||
HOME_USER="$2"
|
||||
shift 2
|
||||
else
|
||||
error "-u|--user option requires an argument"
|
||||
fi
|
||||
;;
|
||||
-p|--path)
|
||||
if [ -n "${2:-}" ]; then
|
||||
FLAKE_PATH="$2"
|
||||
shift 2
|
||||
else
|
||||
error "-p|--path option requires an argument"
|
||||
fi
|
||||
;;
|
||||
-U|--update)
|
||||
UPDATE=1
|
||||
# Check if next argument is a non-option
|
||||
if [ $# -gt 1 ] && [ "${2#-}" = "${2:-}" ]; then
|
||||
UPDATE_INPUTS="$2"
|
||||
shift 2
|
||||
else
|
||||
shift
|
||||
fi
|
||||
;;
|
||||
-r|--rollback)
|
||||
ROLLBACK=1
|
||||
shift
|
||||
;;
|
||||
-t|--show-trace)
|
||||
SHOW_TRACE=1
|
||||
shift
|
||||
;;
|
||||
-B|--build-host)
|
||||
if [ -n "${2:-}" ]; then
|
||||
BUILD_HOST="$2"
|
||||
shift 2
|
||||
else
|
||||
error "-B|--build-host option requires an argument"
|
||||
fi
|
||||
;;
|
||||
-T|--target-host)
|
||||
if [ -n "${2:-}" ]; then
|
||||
TARGET_HOST="$2"
|
||||
shift 2
|
||||
else
|
||||
error "-T|--target-host option requires an argument"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Error: Unknown option '$1'"
|
||||
Help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check if script is run with sudo
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
error "Do not run this script with sudo."
|
||||
fi
|
||||
|
||||
# Check if flake path exists
|
||||
if [ ! -d "$FLAKE_PATH" ]; then
|
||||
error "Flake path '$FLAKE_PATH' does not exist"
|
||||
fi
|
||||
|
||||
# Ignore trailing slash in flake path
|
||||
FLAKE_PATH="${FLAKE_PATH%/}"
|
||||
|
||||
# Check if flake.nix exists
|
||||
if [ ! -f "$FLAKE_PATH/flake.nix" ]; then
|
||||
error "flake.nix does not exist in '$FLAKE_PATH'"
|
||||
fi
|
||||
|
||||
# Execute updates and rebuilds based on the command
|
||||
[ "$UPDATE" = 1 ] && Update
|
||||
|
||||
case "$COMMAND" in
|
||||
nixos)
|
||||
Rebuild_nixos
|
||||
;;
|
||||
home)
|
||||
Rebuild_home
|
||||
;;
|
||||
all)
|
||||
Rebuild_nixos
|
||||
Rebuild_home
|
||||
;;
|
||||
*)
|
||||
echo "Error: Unknown command '$COMMAND'"
|
||||
echo "Printing help page:"
|
||||
Help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
20
apps/update-packages/default.nix
Normal file
20
apps/update-packages/default.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
writeShellApplication,
|
||||
jq,
|
||||
nix-update,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
name = "update-packages";
|
||||
text = builtins.readFile ./${name}.sh;
|
||||
in
|
||||
writeShellApplication {
|
||||
inherit name text;
|
||||
meta.mainProgram = name;
|
||||
|
||||
runtimeInputs = [
|
||||
jq
|
||||
nix-update
|
||||
];
|
||||
}
|
||||
65
apps/update-packages/update-packages.sh
Normal file
65
apps/update-packages/update-packages.sh
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
SYSTEM="x86_64-linux"
|
||||
IGNORE_PACKAGES=(
|
||||
"pyman"
|
||||
"synapse_change_display_name"
|
||||
)
|
||||
|
||||
error() {
|
||||
echo "Error: $1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [[ "$#" -gt 0 ]]; then
|
||||
error "This script does not accept arguments."
|
||||
fi
|
||||
|
||||
TEMP_PACKAGE_LIST="/tmp/nix_flake_packages.$$"
|
||||
|
||||
nix eval .#packages."$SYSTEM" --apply 'pkgs: builtins.attrNames pkgs' --json > "$TEMP_PACKAGE_LIST" 2>/dev/null || \
|
||||
error "Could not determine flake package attributes."
|
||||
|
||||
PACKAGES=$(jq -r '.[]' "$TEMP_PACKAGE_LIST")
|
||||
|
||||
if [ -z "$PACKAGES" ]; then
|
||||
echo "No packages found in the flake outputs. Exiting."
|
||||
rm -f "$TEMP_PACKAGE_LIST"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
IGNORE_PATTERNS=$(printf "%s\n" "${IGNORE_PACKAGES[@]}")
|
||||
PACKAGES=$(echo "$PACKAGES" | grep -v -F -f <(echo "$IGNORE_PATTERNS"))
|
||||
|
||||
echo "Found the following packages to consider for update:"
|
||||
echo "$PACKAGES"
|
||||
|
||||
UPDATED_COUNT=0
|
||||
FAILED_UPDATES=()
|
||||
for PACKAGE_NAME in $PACKAGES; do
|
||||
echo "Attempting to update package: $PACKAGE_NAME"
|
||||
|
||||
if nix-update "$PACKAGE_NAME" --flake --format; then
|
||||
echo "Successfully updated $PACKAGE_NAME."
|
||||
UPDATED_COUNT=$((UPDATED_COUNT + 1))
|
||||
else
|
||||
echo "Failed to update $PACKAGE_NAME." >&2
|
||||
FAILED_UPDATES+=("$PACKAGE_NAME")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -f "$TEMP_PACKAGE_LIST" ]; then
|
||||
rm "$TEMP_PACKAGE_LIST"
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "Summary:"
|
||||
echo "Packages scanned: $(echo "$PACKAGES" | wc -l)"
|
||||
echo "Packages updated: $UPDATED_COUNT"
|
||||
|
||||
if [ ${#FAILED_UPDATES[@]} -gt 0 ]; then
|
||||
echo "Packages that failed to update:" >&2
|
||||
echo "${FAILED_UPDATES[@]}"
|
||||
exit 1
|
||||
else
|
||||
echo "All packages processed successfully."
|
||||
exit 0
|
||||
fi
|
||||
20
apps/wake-host/default.nix
Normal file
20
apps/wake-host/default.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
writeShellApplication,
|
||||
iputils,
|
||||
wakeonlan,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
name = "wake-host";
|
||||
text = builtins.readFile ./${name}.sh;
|
||||
in
|
||||
writeShellApplication {
|
||||
inherit name text;
|
||||
meta.mainProgram = name;
|
||||
|
||||
runtimeInputs = [
|
||||
iputils
|
||||
wakeonlan
|
||||
];
|
||||
}
|
||||
28
apps/wake-host/wake-host.sh
Normal file
28
apps/wake-host/wake-host.sh
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
if [ "$#" -ne 2 ]; then
|
||||
echo "Usage: wake-host <MAC> <IP>"
|
||||
echo "Example: wake-host AA:BB:CC:DD:EE:FF 100.64.0.10"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TARGET_MAC=$1
|
||||
TARGET_IP=$2
|
||||
|
||||
echo "Sending Magic Packet to $TARGET_MAC..."
|
||||
wakeonlan "$TARGET_MAC"
|
||||
|
||||
echo "Waiting for $TARGET_IP to wake up..."
|
||||
MAX_RETRIES=24
|
||||
COUNT=0
|
||||
until ping -c 1 -W 2 "$TARGET_IP" > /dev/null 2>&1; do
|
||||
COUNT=$((COUNT + 1))
|
||||
if [ $COUNT -ge $MAX_RETRIES ]; then
|
||||
echo "Error: Host failed to wake up after $MAX_RETRIES pings."
|
||||
exit 1
|
||||
fi
|
||||
echo "[$COUNT/$MAX_RETRIES] Host is still sleeping..."
|
||||
sleep 5
|
||||
done
|
||||
|
||||
echo "Success: $TARGET_IP is awake."
|
||||
Loading…
Add table
Add a link
Reference in a new issue