Skip to content

Instantly share code, notes, and snippets.

@permezel
Created February 22, 2022 02:37
Show Gist options
  • Save permezel/48253aa4432c88d9475467f1ac9f1316 to your computer and use it in GitHub Desktop.
Save permezel/48253aa4432c88d9475467f1ac9f1316 to your computer and use it in GitHub Desktop.
Really, almost completely, uninstall nix for maxOS
#!/bin/bash
#
#
# snarfed from https://gist.github.com/davejamesmiller/1965569#file-ask-sh
#
ask() {
local prompt default reply
if [[ ${2:-} = 'Y' ]]; then
prompt='Y/n'
default='Y'
elif [[ ${2:-} = 'N' ]]; then
prompt='y/N'
default='N'
else
prompt='y/n'
default=''
fi
while true; do
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r reply </dev/tty
# Default?
if [[ -z $reply ]]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
asky() {
ask "$1" Y
}
askn() {
ask "$1" N
}
yon() {
asky "execute '$1'?" && {
# echo "executing '$1'"
eval "$1"
}
}
# yon "echo test logic"
#
# snarfed from https://gist.github.com/expelledboy/c00aebb004b178cf78b2c9b344526ff6
#
# !!WARNING!!
# This will DELETE all efforts you have put into configuring nix
# Have a look through everything that gets deleted / copied over
cat <<EOF
!!WARNING!!
This will DELETE all efforts you have put into configuring nix
EOF
ask "continue?" || { exit 0; }
/usr/bin/which -s nix-env && {
nix-env -e '.*' --dry-run 2>&1 | grep -q '^uninstalling' && {
ask "Remove previously installed?" Y && {
nix-env -e '.*'
}
}
}
yonerm() {
local rm
rm="rm -rf"
[[ $1 == "sudo" ]] && {
rm="sudo $rm"
shift
}
[[ -e "$1" ]] && {
yon "${rm} $1"
}
}
#
# XXX: This will only clean up current user
#
for x in $HOME/.nix-*; do
yonerm "$x"
done
yonerm "$HOME/.config/nixpkgs"
yonerm "$HOME/.cache/nix"
yonerm "$HOME/.nixpkgs"
if [ -L $HOME/Applications ]; then
# XXX: dubios
yon "rm $HOME/Applications"
fi
yonerm "sudo" "/etc/nix"
if [ -f /Library/LaunchDaemons/org.nixos.nix-daemon.plist ]; then
yon "sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist"
yon "sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist"
fi
if [ -f /etc/profile.backup-before-nix ]; then
yon "sudo mv /etc/profile.backup-before-nix /etc/profile"
fi
if [ -f /etc/bashrc.backup-before-nix ]; then
yon "sudo mv /etc/bashrc.backup-before-nix /etc/bashrc"
fi
if [ -f /etc/zshrc.backup-before-nix ]; then
yon "sudo mv /etc/zshrc.backup-before-nix /etc/zshrc"
fi
USERS=$(dscl . list /Users | grep nixbld)
[[ -z "$USERS" ]] || {
asky "Cleanup nix build users?" && {
for USER in $USERS; do
sudo /usr/bin/dscl . -delete "/Users/$USER"
sudo /usr/bin/dscl . -delete /Groups/staff GroupMembership $USER;
done
sudo /usr/bin/dscl . -delete "/Groups/nixbld"
}
}
for x in /var/root/.nix-*; do
yonerm sudo "$x"
done
#
# cleanup the /nix entry in /etc/fstab
#
while $(grep -q /nix /etc/fstab); do
quit=1
asky "Remove fstab entry?" && {
echo "You will need to delete the /nix entry and then save the file."
sudo vifs
quit=0
}
[[ $quit ]] && break
done
grep -q /nix /etc/fstab || {
# once this has been deleted, cleanup other stuff
grep -q '^nix$' /etc/synthetic.conf && {
asky "Remove 'nix' from synthetic.conf?" && {
sudo perl -i.bkp -ne 'print unless /^nix$/' /etc/synthetic.conf
}
}
}
#
# remove the /nix volume
#
diskutil info "Nix Store" >/dev/null 2>&1 && {
node=$(diskutil info "Nix Store" | awk '/Device Node/ { print $3; }')
asky "Remove the NIX store volume ($node)?" && {
sudo diskutil unmount $node
sudo diskutil apfs deleteVolume $node
}
}
exit 0
# useful for finding hanging links
# $ find . -type l -maxdepth 5 ! -exec test -e {} \; -print 2>/dev/null | xargs -I {} sh -c 'file -b {} | grep nix && echo {}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment