nixos-config/switch.sh
2025-03-10 21:11:26 +04:00

28 lines
965 B
Bash
Executable File

#!/usr/bin/env bash
# Helper script to switch between NixOS configurations
set -e
# Default to the nixos configuration if no argument is provided
CONFIG=${1:-nixos}
# Use nix itself to list available configurations
AVAILABLE_CONFIGS=$(nix flake show --json | jq -r '.nixosConfigurations | keys[]' 2>/dev/null)
# If the above command fails, fall back to a simple grep method
if [ -z "$AVAILABLE_CONFIGS" ]; then
AVAILABLE_CONFIGS=$(grep -A 100 "nixosConfigurations = {" flake.nix | grep -E "^\s+[a-zA-Z0-9_-]+ = " | sed 's/^\s*\([a-zA-Z0-9_-]*\)\s*=.*/\1/')
fi
# Check if the configuration exists
if ! echo "$AVAILABLE_CONFIGS" | grep -q "^${CONFIG}$"; then
echo "Error: Configuration '$CONFIG' not found in flake.nix"
echo "Available configurations:"
echo "$AVAILABLE_CONFIGS" | sed 's/^/ - /'
exit 1
fi
echo "Switching to configuration: $CONFIG"
sudo nixos-rebuild switch --flake ".#$CONFIG"
echo "Successfully switched to $CONFIG configuration"