installplan should not copy self binary (#161)

* Make binary copy self, not library

* lints
This commit is contained in:
Ana Hobden 2023-01-09 08:26:08 -08:00 committed by GitHub
parent b60c4c84d0
commit 844faa0d20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View file

@ -1,4 +1,5 @@
use std::{ use std::{
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::ExitCode, process::ExitCode,
}; };
@ -160,6 +161,9 @@ impl CommandExecute for Install {
match install_plan.install(rx1).await { match install_plan.install(rx1).await {
Err(err) => { Err(err) => {
if !no_confirm { if !no_confirm {
// Attempt to copy self to the store if possible, but since the install failed, this might not work, that's ok.
copy_self_to_nix_store().await.ok();
let mut was_expected = false; let mut was_expected = false;
if let Some(expected) = err.expected() { if let Some(expected) = err.expected() {
was_expected = true; was_expected = true;
@ -210,6 +214,9 @@ impl CommandExecute for Install {
} }
}, },
Ok(_) => { Ok(_) => {
copy_self_to_nix_store()
.await
.wrap_err("Copying `nix-installer` to `/nix/nix-installer`")?;
println!( println!(
"\ "\
{success}\n\ {success}\n\
@ -229,3 +236,11 @@ impl CommandExecute for Install {
Ok(ExitCode::SUCCESS) Ok(ExitCode::SUCCESS)
} }
} }
#[tracing::instrument(level = "debug")]
async fn copy_self_to_nix_store() -> Result<(), std::io::Error> {
let path = std::env::current_exe()?;
tokio::fs::copy(path, "/nix/nix-installer").await?;
tokio::fs::set_permissions("/nix/nix-installer", PermissionsExt::from_mode(0o0755)).await?;
Ok(())
}

View file

@ -144,9 +144,7 @@ impl InstallPlan {
} }
write_receipt(self.clone()).await?; write_receipt(self.clone()).await?;
copy_self_to_nix_store()
.await
.map_err(|e| NixInstallerError::CopyingSelf(e))?;
Ok(()) Ok(())
} }
@ -288,13 +286,6 @@ fn ensure_version<'de, D: Deserializer<'de>>(d: D) -> Result<Version, D::Error>
} }
} }
#[tracing::instrument(level = "debug")]
async fn copy_self_to_nix_store() -> Result<(), std::io::Error> {
let path = std::env::current_exe()?;
tokio::fs::copy(path, "/nix/nix-installer").await?;
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use semver::Version; use semver::Version;