diff --git a/src/toolchains.rs b/src/toolchains.rs index 16e68a0..5ce1c50 100644 --- a/src/toolchains.rs +++ b/src/toolchains.rs @@ -34,6 +34,8 @@ pub(crate) enum InstallError { TempDir(#[source] io::Error), #[error("Could not move tempdir into destination: {0}")] Move(#[source] io::Error), + #[error("Could not patchelf")] + Patchelf(#[source] io::Error), #[error("Could not run subcommand {cmd}: {err}")] Subcommand { cmd: String, @@ -208,7 +210,9 @@ impl Toolchain { })?; } - fs::rename(tmpdir.into_path(), dest).map_err(InstallError::Move) + fs::rename(tmpdir.into_path(), &dest).map_err(InstallError::Move)?; + nix_patchelf(dest).map_err(InstallError::Patchelf)?; + Ok(()) } pub(crate) fn remove(&self, dl_params: &DownloadParams) -> io::Result<()> { @@ -542,3 +546,46 @@ fn download_tarball( res => res, } } + +fn nix_patchelf(mut toolchain_path: PathBuf) -> Result<(), io::Error> { + toolchain_path.push("bin"); + + for entry in toolchain_path.read_dir()? { + let entry = entry?; + if !entry.file_type()?.is_file() { + continue; + } + + eprintln!( + "info: you seem to be running NixOS. Attempting to patch {}", + entry.path().to_str().unwrap() + ); + let _ = ::std::process::Command::new("@patchelf@/bin/patchelf") + .arg("--set-interpreter") + .arg("@dynamicLinker@") + .arg(entry.path()) + .output(); + } + + toolchain_path.pop(); + toolchain_path.push("lib"); + + for entry in toolchain_path.read_dir()? { + let entry = entry?; + if !entry.file_type()?.is_file() { + continue; + } + + eprintln!( + "info: you seem to be running NixOS. Attempting to patch {}", + entry.path().to_str().unwrap() + ); + let _ = ::std::process::Command::new("@patchelf@/bin/patchelf") + .arg("--set-rpath") + .arg("@libPath@") + .arg(entry.path()) + .output(); + } + + Ok(()) +}