From e3d06c24e7a9e9582c70851f48468cb184e71012 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Fri, 10 Feb 2023 14:44:39 -0800 Subject: [PATCH] Offer users better error if fstab entries exist (#241) * Offer users better error if fstab entries exist * Split up errors * Remove a naughty bracket * last nits --- src/action/macos/create_fstab_entry.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/action/macos/create_fstab_entry.rs b/src/action/macos/create_fstab_entry.rs index e0980ab..106e87a 100644 --- a/src/action/macos/create_fstab_entry.rs +++ b/src/action/macos/create_fstab_entry.rs @@ -37,9 +37,18 @@ impl CreateFstabEntry { .await .map_err(|e| ActionError::Read(fstab_path.to_path_buf(), e))?; let prelude_comment = fstab_prelude_comment(&apfs_volume_label); + + // See if the user already has a `/nix` related entry, if so, invite them to remove it. + if fstab_buf.split(&[' ', '\t']).any(|chunk| chunk == "/nix") { + return Err(ActionError::Custom(Box::new( + CreateFstabEntryError::NixEntryExists, + ))); + } + + // See if a previous install from this crate exists, if so, invite the user to remove it (we may need to change it) if fstab_buf.contains(&prelude_comment) { return Err(ActionError::Custom(Box::new( - CreateFstabEntryError::EntryExists(apfs_volume_label.clone()), + CreateFstabEntryError::VolumeEntryExists(apfs_volume_label.clone()), ))); } } @@ -193,8 +202,10 @@ fn fstab_entry(uuid: &Uuid, apfs_volume_label: &str) -> String { #[derive(thiserror::Error, Debug)] pub enum CreateFstabEntryError { - #[error("An `/etc/fstab` entry for the volume labelled `{0}` already exists. If a Nix Store already exists it may need to be deleted with `diskutil apfs deleteVolume \"{0}\") and should be removed from `/etc/fstab`")] - EntryExists(String), + #[error("An `/etc/fstab` entry for the `/nix` path already exists, consider removing the entry for `/nix`d from `/etc/fstab`")] + NixEntryExists, + #[error("An `/etc/fstab` entry created by `nix-installer` already exists. If a volume named `{0}` already exists, it may need to be deleted with `diskutil apfs deleteVolume \"{0}\" and the entry for `/nix` should be removed from `/etc/fstab`")] + VolumeEntryExists(String), } #[derive(Deserialize, Clone, Debug)]