Skip to content

Instantly share code, notes, and snippets.

@dzogrim
Created June 25, 2025 11:22
Show Gist options
  • Save dzogrim/72eeac8a844c6ee9bca0e4451994ce65 to your computer and use it in GitHub Desktop.
Save dzogrim/72eeac8a844c6ee9bca0e4451994ce65 to your computer and use it in GitHub Desktop.
Script to activate or rollback Nix Home Manager profile on macOS 15
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025 Sébastien L.
# Description: Script to activate or rollback Nix Home Manager profile on macOS.
# Script d’administration pour activer ou restaurer un environnement Nix Home Manager sous macOS.
# Il vérifie l’utilisateur, la présence du flake et sécurise les opérations avec gestion des sauvegardes.
# Version: 0.1 (2025-06-25)
# Author: Sebastien L.
set -euo pipefail
EXPECTED_USER="user.local"
FLAKE_DIR="$HOME/.config/home-manager"
if [ "$(whoami)" != "$EXPECTED_USER" ]; then
echo "❌ Error: This script must be run as user '$EXPECTED_USER'."
exit 1
fi
if [ ! -f "$FLAKE_DIR/flake.nix" ]; then
echo "❌ Error: No flake.nix found in $FLAKE_DIR."
exit 1
fi
ACTION="${1:-}"
confirm() {
echo -n "Are you sure you want to proceed? (yes/no): "
read -r response
if [[ "$response" != "yes" ]]; then
echo "❌ Operation cancelled by user."
exit 0
fi
}
activate() {
confirm
echo "==> Activating Nix Home Manager profile..."
cd "$FLAKE_DIR"
nix run github:nix-community/home-manager -- switch -b backup --flake .#$EXPECTED_USER
echo -e "\n✅ Nix environment activated."
echo "💡 Please open a new terminal session to apply the changes."
}
rollback() {
if [ ! -f ~/.bashrc.backup ] || [ ! -f ~/.profile.backup ]; then
echo "❌ No backup files found. Rollback is not possible."
exit 1
fi
confirm
echo "==> Rolling back to pre-Nix state..."
mv -v ~/.bashrc.backup ~/.bashrc
mv -v ~/.profile.backup ~/.profile
rm -v ~/.bash_profile || true
rm -v ~/.bashrc.d/custom.bash || true
echo -e "\n✅ Rollback complete. You can restart your terminal."
}
case "$ACTION" in
activate)
activate
;;
rollback)
rollback
;;
*)
echo "Usage: $0 {activate|rollback}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment