Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR to make purge script more robust for MacOS #18192

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions nix/scripts/purge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ 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}"
sudo launchctl unload "${NIX_SERVICE}" || true
sudo launchctl remove "${NIX_SERVICE}" || true
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't "robust", this is just ignoring failures.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to understand why unload would fail.

done
}

Expand All @@ -41,15 +41,39 @@ nix_purge_darwin_multi_user_users() {
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

# Attempt to delete the volume
if ! sudo diskutil apfs deleteVolume /nix; then
echo "Failed to unmount /nix because it is in use."

# Identify the process using the volume
local pid=$(lsof +D /nix | awk 'NR==2{print $2}')
if [ -n "$pid" ]; then
echo "The volume /nix is in use by process ID $pid."

# Ask the user whether to terminate the process
read -p "Do you want to terminate this process? (y/n): " choice
if [[ $choice == "y" ]]; then
sudo kill $pid
echo "Process $pid terminated."
else
echo "Process not terminated. Please close it manually and retry."
return 1
fi
Comment on lines +52 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing should be a function. Or even a separate scripts/kill_proc_prompt.sh.

else
echo "No process found using /nix. Manual intervention required."
return 1
fi
fi

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
nix_purge_darwin_multi_user_service || true
nix_purge_darwin_multi_user_users || true
nix_purge_darwin_multi_user_volumes || true
Comment on lines +74 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this is not "robust", it just ignores failures and continues.

else
nix_purge_linux_multi_user_service
nix_purge_linux_multi_user_users
Expand Down