From 844faa0d2072bcb6c484287dbd8230cbf059e59d Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Mon, 9 Jan 2023 08:26:08 -0800 Subject: [PATCH] installplan should not copy self binary (#161) * Make binary copy self, not library * lints --- src/cli/subcommand/install.rs | 15 +++++++++++++++ src/plan.rs | 11 +---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/cli/subcommand/install.rs b/src/cli/subcommand/install.rs index 4046340..a472f5f 100644 --- a/src/cli/subcommand/install.rs +++ b/src/cli/subcommand/install.rs @@ -1,4 +1,5 @@ use std::{ + os::unix::prelude::PermissionsExt, path::{Path, PathBuf}, process::ExitCode, }; @@ -160,6 +161,9 @@ impl CommandExecute for Install { match install_plan.install(rx1).await { Err(err) => { 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; if let Some(expected) = err.expected() { was_expected = true; @@ -210,6 +214,9 @@ impl CommandExecute for Install { } }, Ok(_) => { + copy_self_to_nix_store() + .await + .wrap_err("Copying `nix-installer` to `/nix/nix-installer`")?; println!( "\ {success}\n\ @@ -229,3 +236,11 @@ impl CommandExecute for Install { 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(()) +} diff --git a/src/plan.rs b/src/plan.rs index fc27640..58184a4 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -144,9 +144,7 @@ impl InstallPlan { } write_receipt(self.clone()).await?; - copy_self_to_nix_store() - .await - .map_err(|e| NixInstallerError::CopyingSelf(e))?; + Ok(()) } @@ -288,13 +286,6 @@ fn ensure_version<'de, D: Deserializer<'de>>(d: D) -> Result } } -#[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)] mod test { use semver::Version;