github.com/status-im/status-go@v1.1.0/nix/scripts/purge.sh (about)

     1  #!/usr/bin/env bash
     2  # This script removes all Nix files.
     3  
     4  GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
     5  source "${GIT_ROOT}/nix/scripts/lib.sh"
     6  source "${GIT_ROOT}/scripts/colors.sh"
     7  
     8  nix_purge_linux_multi_user_service() {
     9      NIX_SERVICES=(nix-daemon.service nix-daemon.socket)
    10      for NIX_SERVICE in "${NIX_SERVICES[@]}"; do
    11          sudo systemctl stop "${NIX_SERVICE}"
    12          sudo systemctl disable "${NIX_SERVICE}"
    13      done
    14      sudo systemctl daemon-reload
    15  }
    16  
    17  nix_purge_linux_multi_user_users() {
    18      for NIX_USER in $(awk -F: '/nixbld/{print $1}' /etc/passwd); do
    19          sudo userdel "${NIX_USER}"
    20      done
    21      sudo groupdel nixbld
    22  }
    23  
    24  nix_purge_darwin_multi_user_service() {
    25      cd /Library/LaunchDaemons
    26      NIX_SERVICES=(org.nixos.darwin-store.plist org.nixos.nix-daemon.plist)
    27      for NIX_SERVICE in "${NIX_SERVICES[@]}"; do
    28          sudo launchctl unload "${NIX_SERVICE}"
    29          sudo launchctl remove "${NIX_SERVICE}"
    30      done
    31  }
    32  
    33  nix_purge_darwin_multi_user_users() {
    34      for NIX_USER in $(dscl . list /Users | grep nixbld); do
    35          sudo dscl . -delete "/Users/${NIX_USER}"
    36      done
    37      sudo dscl . -delete /Groups/nixbld
    38  }
    39  
    40  # This still leaves an empty /nix, which will disappear after reboot.
    41  nix_purge_darwin_multi_user_volumes() {
    42      sudo sed -i.bkp '/nix/d' /etc/synthetic.conf
    43      sudo sed -i.bkp '/nix/d' /etc/fstab
    44      sudo diskutil apfs deleteVolume /nix
    45      echo -e "${YLW}You will need to reboot your system!${RST}" >&2
    46  }
    47  
    48  nix_purge_multi_user() {
    49      if [[ $(uname -s) == "Darwin" ]]; then
    50          nix_purge_darwin_multi_user_service
    51          nix_purge_darwin_multi_user_users
    52          nix_purge_darwin_multi_user_volumes
    53      else
    54          nix_purge_linux_multi_user_service
    55          nix_purge_linux_multi_user_users
    56      fi
    57  
    58      sudo rm -fr /etc/nix
    59      sudo rm -f /etc/profile.d/nix.sh*
    60  
    61      # Restore old shell profiles
    62      NIX_PROFILE_FILES=(
    63          /etc/bash.bashrc /etc/bashrc /etc/bash/bashrc
    64          /etc/zsh.zshhrc /etc/zshrc /etc/zsh/zshrc
    65      )
    66      for NIX_FILE in "${NIX_PROFILE_FILES[@]}"; do
    67          if [[ -f "${NIX_FILE}.backup-before-nix" ]]; then
    68              sudo mv -f "${NIX_FILE}.backup-before-nix" "${NIX_FILE}"
    69          fi
    70      done
    71  }
    72  
    73  nix_purge_user_profile() {
    74      sudo rm -rf \
    75          ~/.nix-* \
    76          ~/.cache/nix \
    77          ~/.config/nixpkgs \
    78          "${GIT_ROOT}/.nix-gcroots"
    79  }
    80  
    81  nix_purge_root() {
    82      NIX_ROOT=$(nix_root)
    83      if [[ -z "${NIX_ROOT}" ]]; then
    84          echo -e "${RED}Unable to identify Nix root!${RST}" >&2
    85          exit 1
    86      fi
    87      sudo rm -fr "${NIX_ROOT}"
    88  }
    89  
    90  # Don't run anything if script is just sourced.
    91  if (return 0 2>/dev/null); then
    92      echo -e "${YLW}Script sourced, not running purge.${RST}"
    93      return
    94  fi
    95  
    96  # Confirm user decission, unless --force is used.
    97  if [[ "${1}" != "--force" ]]; then
    98      echo -e "${YLW}Are you sure you want to purge Nix?${RST}" >&2
    99      read -p "[y/n]: " -n 1 -r
   100      echo
   101      if [[ $REPLY =~ ^[^Yy]$ ]]; then
   102          echo -e "${GRN}Aborting Nix purge!${RST}" >&2
   103          exit 0
   104      fi
   105  fi
   106  
   107  NIX_INSTALL_TYPE=$(nix_install_type)
   108  # Purging /nix on NixOS would be disasterous.
   109  if [[ "${NIX_INSTALL_TYPE}" == "nixos" ]]; then
   110      echo -e "${RED}You should not purge Nix files on NixOS!${RST}" >&2
   111      exit
   112  elif [[ "${NIX_INSTALL_TYPE}" == "none" ]] && [[ "${1}" != "--force" ]]; then
   113      echo -e "${YLW}Nothing to remove, Nix not installed.${RST}" >&2
   114      exit
   115  elif [[ "${NIX_INSTALL_TYPE}" == "multi" ]] || [[ "${1}" == "--force" ]]; then
   116      echo -e "${YLW}Detected multi-user Nix installation.${RST}" >&2
   117      nix_purge_multi_user
   118  elif [[ "${NIX_INSTALL_TYPE}" == "single" ]] || [[ "${1}" == "--force" ]]; then
   119      echo -e "${YLW}Detected single-user Nix installation.${RST}" >&2
   120      nix_purge_user_profile
   121  fi
   122  nix_purge_root
   123  
   124  echo -e "${GRN}Purged all Nix files from your system.${RST}" >&2