Uninstalling should change directory if in nix (#526)

* Change directory during uninstall if run from /nix

* Include log message
This commit is contained in:
Ana Hobden 2023-06-23 11:42:18 -07:00 committed by GitHub
parent 122a4bfdf0
commit 0b6d723db3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -1,10 +1,9 @@
use nix::unistd::Group;
use tracing::{span, Span}; use tracing::{span, Span};
use super::{CreateNixTree, DeleteUsersInGroup}; use super::CreateNixTree;
use crate::{ use crate::{
action::{ action::{
base::{CreateGroup, FetchAndUnpackNix, MoveUnpackedNix}, base::{FetchAndUnpackNix, MoveUnpackedNix},
Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction, Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction,
}, },
settings::{CommonSettings, SCRATCH_DIR}, settings::{CommonSettings, SCRATCH_DIR},

View file

@ -54,6 +54,18 @@ impl CommandExecute for Uninstall {
ensure_root()?; 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` // 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, // 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. // well, we have a problem, since the binary would delete itself.