Skip to content

Commit

Permalink
Uninstalling should change directory if in nix (#526)
Browse files Browse the repository at this point in the history
* Change directory during uninstall if run from /nix

* Include log message
  • Loading branch information
Hoverbear authored Jun 23, 2023
1 parent 122a4bf commit 0b6d723
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/action/common/provision_nix.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
12 changes: 12 additions & 0 deletions src/cli/subcommand/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0b6d723

Please sign in to comment.