2020-04-21 10:29:54 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-01-05 13:32:02 +00:00
|
|
|
# This script removes all Nix files.
|
2020-04-21 10:29:54 +00:00
|
|
|
|
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
2022-01-05 13:32:02 +00:00
|
|
|
source "${GIT_ROOT}/nix/scripts/lib.sh"
|
2020-04-21 10:29:54 +00:00
|
|
|
source "${GIT_ROOT}/scripts/colors.sh"
|
|
|
|
|
2022-04-12 09:54:03 +00:00
|
|
|
nix_purge_linux_multi_user_service() {
|
|
|
|
NIX_SERVICES=(nix-daemon.service nix-daemon.socket)
|
|
|
|
for NIX_SERVICE in "${NIX_SERVICES[@]}"; do
|
|
|
|
sudo systemctl stop "${NIX_SERVICE}"
|
|
|
|
sudo systemctl disable "${NIX_SERVICE}"
|
|
|
|
done
|
2022-01-05 13:32:02 +00:00
|
|
|
sudo systemctl daemon-reload
|
2022-04-12 09:54:03 +00:00
|
|
|
}
|
2022-01-05 13:32:02 +00:00
|
|
|
|
2022-04-12 09:54:03 +00:00
|
|
|
nix_purge_linux_multi_user_users() {
|
2022-01-05 13:32:02 +00:00
|
|
|
for NIX_USER in $(awk -F: '/nixbld/{print $1}' /etc/passwd); do
|
|
|
|
sudo userdel "${NIX_USER}"
|
|
|
|
done
|
|
|
|
sudo groupdel nixbld
|
2022-04-12 09:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nix_purge_darwin_multi_user_service() {
|
|
|
|
cd /Library/LaunchDaemons
|
|
|
|
NIX_SERVICES=(org.nixos.darwin-store.plist org.nixos.nix-daemon.plist)
|
|
|
|
for NIX_SERVICE in "${NIX_SERVICES[@]}"; do
|
|
|
|
sudo launchctl unload "${NIX_SERVICE}"
|
|
|
|
sudo launchctl remove "${NIX_SERVICE}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
nix_purge_darwin_multi_user_users() {
|
|
|
|
for NIX_USER in $(dscl . list /Users | grep nixbld); do
|
|
|
|
sudo dscl . -delete "/Users/${NIX_USER}"
|
|
|
|
done
|
|
|
|
sudo dscl . -delete /Groups/nixbld
|
|
|
|
}
|
|
|
|
|
|
|
|
# This still leaves an empty /nix, which will disappear after reboot.
|
|
|
|
nix_purge_darwin_multi_user_volumes() {
|
|
|
|
sudo sed -i.bkp '/nix/d' /etc/synthetic.conf
|
|
|
|
sudo sed -i.bkp '/nix/d' /etc/fstab
|
|
|
|
sudo diskutil apfs deleteVolume /nix
|
|
|
|
echo -e "${YLW}You will need to reboot your system!${RST}" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
nix_purge_multi_user() {
|
|
|
|
if [[ $(uname -s) == "Darwin" ]]; then
|
|
|
|
nix_purge_darwin_multi_user_service
|
|
|
|
nix_purge_darwin_multi_user_users
|
|
|
|
nix_purge_darwin_multi_user_volumes
|
|
|
|
else
|
|
|
|
nix_purge_linux_multi_user_service
|
|
|
|
nix_purge_linux_multi_user_users
|
|
|
|
fi
|
|
|
|
|
|
|
|
sudo rm -fr /etc/nix
|
|
|
|
sudo rm -f /etc/profile.d/nix.sh*
|
2022-01-05 13:32:02 +00:00
|
|
|
|
|
|
|
# Restore old shell profiles
|
|
|
|
NIX_PROFILE_FILES=(
|
|
|
|
/etc/bash.bashrc /etc/bashrc /etc/bash/bashrc
|
|
|
|
/etc/zsh.zshhrc /etc/zshrc /etc/zsh/zshrc
|
|
|
|
)
|
|
|
|
for NIX_FILE in "${NIX_PROFILE_FILES[@]}"; do
|
|
|
|
if [[ -f "${NIX_FILE}.backup-before-nix" ]]; then
|
|
|
|
sudo mv -f "${NIX_FILE}.backup-before-nix" "${NIX_FILE}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
nix_purge_user_profile() {
|
|
|
|
sudo rm -rf \
|
|
|
|
~/.nix-* \
|
|
|
|
~/.cache/nix \
|
|
|
|
~/.config/nixpkgs \
|
2022-07-25 20:38:41 +00:00
|
|
|
"${GIT_ROOT}/.nix-gcroots"
|
2022-01-05 13:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nix_purge_root() {
|
2022-04-12 09:54:03 +00:00
|
|
|
NIX_ROOT=$(nix_root)
|
2022-01-05 13:32:02 +00:00
|
|
|
if [[ -z "${NIX_ROOT}" ]]; then
|
|
|
|
echo -e "${RED}Unable to identify Nix root!${RST}" >&2
|
|
|
|
exit 1
|
2020-04-21 10:29:54 +00:00
|
|
|
fi
|
2022-01-05 13:32:02 +00:00
|
|
|
sudo rm -fr "${NIX_ROOT}"
|
|
|
|
}
|
|
|
|
|
2022-04-12 09:54:03 +00:00
|
|
|
# Don't run anything if script is just sourced.
|
|
|
|
if (return 0 2>/dev/null); then
|
|
|
|
echo -e "${YLW}Script sourced, not running purge.${RST}"
|
|
|
|
return
|
|
|
|
fi
|
2022-01-05 13:32:02 +00:00
|
|
|
|
2022-04-12 09:54:03 +00:00
|
|
|
# Confirm user decission, unless --force is used.
|
|
|
|
if [[ "${1}" != "--force" ]]; then
|
|
|
|
echo -e "${YLW}Are you sure you want to purge Nix?${RST}" >&2
|
|
|
|
read -p "[y/n]: " -n 1 -r
|
|
|
|
echo
|
|
|
|
if [[ $REPLY =~ ^[^Yy]$ ]]; then
|
|
|
|
echo -e "${GRN}Aborting Nix purge!${RST}" >&2
|
|
|
|
exit 0
|
|
|
|
fi
|
2020-04-21 10:29:54 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-12 09:54:03 +00:00
|
|
|
NIX_INSTALL_TYPE=$(nix_install_type)
|
|
|
|
# Purging /nix on NixOS would be disasterous.
|
2022-01-05 13:32:02 +00:00
|
|
|
if [[ "${NIX_INSTALL_TYPE}" == "nixos" ]]; then
|
|
|
|
echo -e "${RED}You should not purge Nix files on NixOS!${RST}" >&2
|
|
|
|
exit
|
2022-04-12 09:54:03 +00:00
|
|
|
elif [[ "${NIX_INSTALL_TYPE}" == "none" ]] && [[ "${1}" != "--force" ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${YLW}Nothing to remove, Nix not installed.${RST}" >&2
|
|
|
|
exit
|
2022-04-12 09:54:03 +00:00
|
|
|
elif [[ "${NIX_INSTALL_TYPE}" == "multi" ]] || [[ "${1}" == "--force" ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${YLW}Detected multi-user Nix installation.${RST}" >&2
|
|
|
|
nix_purge_multi_user
|
2022-04-12 09:54:03 +00:00
|
|
|
elif [[ "${NIX_INSTALL_TYPE}" == "single" ]] || [[ "${1}" == "--force" ]]; then
|
2022-01-05 13:32:02 +00:00
|
|
|
echo -e "${YLW}Detected single-user Nix installation.${RST}" >&2
|
|
|
|
nix_purge_user_profile
|
2020-04-21 10:29:54 +00:00
|
|
|
fi
|
2022-01-05 13:32:02 +00:00
|
|
|
nix_purge_root
|
2020-04-21 10:29:54 +00:00
|
|
|
|
|
|
|
echo -e "${GRN}Purged all Nix files from your system.${RST}" >&2
|