-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathuninstall.rs
213 lines (193 loc) · 8.12 KB
/
uninstall.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::{
ffi::CString,
path::{Path, PathBuf},
process::ExitCode,
};
use crate::{
cli::{ensure_root, interaction::PromptChoice, signal_channel},
error::HasExpectedErrors,
plan::{current_version, RECEIPT_LOCATION},
InstallPlan, NixInstallerError,
};
use clap::{ArgAction, Parser};
use color_eyre::eyre::{eyre, WrapErr};
use owo_colors::OwoColorize;
use rand::Rng;
use crate::cli::{interaction, CommandExecute};
/// Uninstall a previously `nix-installer` installed Nix
#[derive(Debug, Parser)]
pub struct Uninstall {
#[clap(
long,
env = "NIX_INSTALLER_NO_CONFIRM",
action(ArgAction::SetTrue),
default_value = "false",
global = true
)]
pub no_confirm: bool,
#[clap(
long,
env = "NIX_INSTALLER_EXPLAIN",
action(ArgAction::SetTrue),
default_value = "false",
global = true
)]
pub explain: bool,
#[clap(default_value = RECEIPT_LOCATION)]
pub receipt: PathBuf,
}
#[async_trait::async_trait]
impl CommandExecute for Uninstall {
#[tracing::instrument(level = "debug", skip_all)]
async fn execute(self) -> eyre::Result<ExitCode> {
let Self {
no_confirm,
receipt,
explain,
} = self;
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.
// Instead, detect if we're in that location, if so, move the binary and `execv` it.
if let Ok(current_exe) = std::env::current_exe() {
if current_exe.as_path() == Path::new("/nix/nix-installer") {
tracing::debug!(
"Detected uninstall from `/nix/nix-installer`, moving executable and re-executing"
);
let temp = std::env::temp_dir();
let random_trailer: String = {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789";
const PASSWORD_LEN: usize = 16;
let mut rng = rand::thread_rng();
(0..PASSWORD_LEN)
.map(|_| {
let idx = rng.gen_range(0..CHARSET.len());
CHARSET[idx] as char
})
.collect()
};
let temp_exe = temp.join(&format!("nix-installer-{random_trailer}"));
tokio::fs::copy(¤t_exe, &temp_exe)
.await
.wrap_err("Copying nix-installer to tempdir")?;
let args = std::env::args();
let mut arg_vec_cstring = vec![];
for arg in args {
arg_vec_cstring.push(CString::new(arg).wrap_err("Making arg into C string")?);
}
let temp_exe_cstring = CString::new(temp_exe.to_string_lossy().into_owned())
.wrap_err("Making C string of executable path")?;
tracing::trace!("Execv'ing `{temp_exe_cstring:?} {arg_vec_cstring:?}`");
nix::unistd::execv(&temp_exe_cstring, &arg_vec_cstring)
.wrap_err("Executing copied `nix-installer`")?;
}
}
let install_receipt_string = tokio::fs::read_to_string(receipt)
.await
.wrap_err("Reading receipt")?;
let mut plan: InstallPlan = match serde_json::from_str(&install_receipt_string) {
Ok(plan) => plan,
Err(plan_err) => {
#[derive(serde::Deserialize)]
struct MinimalPlan {
version: semver::Version,
}
let minimal_plan: Result<MinimalPlan, _> =
serde_json::from_str(&install_receipt_string);
match minimal_plan {
Ok(minimal_plan) => {
return Err(plan_err).wrap_err_with(|| {
let plan_version = minimal_plan.version;
let current_version = current_version().map(|v| v.to_string()).unwrap_or_else(|_| env!("CARGO_PKG_VERSION").to_string());
format!(
"\
Unable to parse plan, this plan was created by `nix-installer` version `{plan_version}`, this is `nix-installer` version `{current_version}`\n\
To uninstall, either run `/nix/nix-installer uninstall` or `curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v{plan_version} | sh -s -- uninstall`\
").red().to_string()
});
},
Err(_minimal_plan_err) => return Err(plan_err)?,
}
},
};
if let Err(e) = plan.check_compatible() {
let version = plan.version;
eprintln!(
"{}",
format!("\
{e}\n\
\n\
Found existing plan in `{RECEIPT_LOCATION}` which was created by a version incompatible `nix-installer`.\n\
\n
To uninstall, either run `/nix/nix-installer uninstall` or `curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v${version} | sh -s -- uninstall`\n\
\n\
").red()
);
return Ok(ExitCode::FAILURE);
}
if let Err(err) = plan.pre_uninstall_check().await {
if let Some(expected) = err.expected() {
eprintln!("{}", expected.red());
return Ok(ExitCode::FAILURE);
}
Err(err)?
}
if !no_confirm {
let mut currently_explaining = explain;
loop {
match interaction::prompt(
plan.describe_uninstall(currently_explaining)
.await
.map_err(|e| eyre!(e))?,
PromptChoice::Yes,
currently_explaining,
)
.await?
{
PromptChoice::Yes => break,
PromptChoice::Explain => currently_explaining = true,
PromptChoice::No => {
interaction::clean_exit_with_message("Okay, didn't do anything! Bye!").await
},
}
}
}
let (_tx, rx) = signal_channel().await?;
let res = plan.uninstall(rx).await;
match res {
Err(err @ NixInstallerError::ActionRevert(_)) => {
tracing::error!("Uninstallation complete, some errors encountered");
return Err(err)?;
},
Err(err) => {
if let Some(expected) = err.expected() {
println!("{}", expected.red());
return Ok(ExitCode::FAILURE);
}
return Err(err)?;
},
_ => (),
}
println!(
"\
{success}\n\
",
success = "Nix was uninstalled successfully!".green().bold(),
);
Ok(ExitCode::SUCCESS)
}
}