From 0b6d723db3140e79705ba982579e99b6b9268998 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Fri, 23 Jun 2023 11:42:18 -0700 Subject: [PATCH] Uninstalling should change directory if in nix (#526) * Change directory during uninstall if run from /nix * Include log message --- src/action/common/provision_nix.rs | 5 ++--- src/cli/subcommand/uninstall.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/action/common/provision_nix.rs b/src/action/common/provision_nix.rs index 0c28278..e05a209 100644 --- a/src/action/common/provision_nix.rs +++ b/src/action/common/provision_nix.rs @@ -1,10 +1,9 @@ -use nix::unistd::Group; use tracing::{span, Span}; -use super::{CreateNixTree, DeleteUsersInGroup}; +use super::CreateNixTree; use crate::{ action::{ - base::{CreateGroup, FetchAndUnpackNix, MoveUnpackedNix}, + base::{FetchAndUnpackNix, MoveUnpackedNix}, Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction, }, settings::{CommonSettings, SCRATCH_DIR}, diff --git a/src/cli/subcommand/uninstall.rs b/src/cli/subcommand/uninstall.rs index 2a7bd7e..1ff2e1b 100644 --- a/src/cli/subcommand/uninstall.rs +++ b/src/cli/subcommand/uninstall.rs @@ -54,6 +54,18 @@ impl CommandExecute for Uninstall { ensure_root()?; + if let Ok(current_dir) = std::env::current_dir() { + let mut components = current_dir.components(); + let should_be_root = components.next(); + let maybe_nix = components.next(); + if should_be_root == Some(std::path::Component::RootDir) + && maybe_nix == Some(std::path::Component::Normal(std::ffi::OsStr::new("nix"))) + { + tracing::debug!("Changing current directory to be outside of `/nix`"); + std::env::set_current_dir("/").wrap_err("Uninstall process was run from `/nix` folder, but could not change directory away from `/nix`, please change the current directory and try again.")?; + } + } + // During install, `nix-installer` will store a copy of itself in `/nix/nix-installer` // If the user opted to run that particular copy of `nix-installer` to do this uninstall, // well, we have a problem, since the binary would delete itself.