Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically patch binaries for nixos #48

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum SolcVmError {
ChecksumMismatch(String),
#[error("Install step for solc version {0} timed out after {1} seconds")]
Timeout(String, u64),
#[error("Unable to patch solc binary for nixos. stdout: {0}. stderr: {1}")]
CouldNotPatchForNixOs(String, String),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
Expand Down
34 changes: 32 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use once_cell::sync::Lazy;
use semver::Version;
use semver::{Version, VersionReq};
use sha2::Digest;

use std::{
ffi::OsString,
fs,
io::{Cursor, Write},
path::PathBuf,
process::Command,
};

use std::time::Duration;
Expand Down Expand Up @@ -43,6 +44,9 @@ pub static SVM_HOME: Lazy<PathBuf> = Lazy::new(|| {
/// The timeout to use for requests to the source
const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);

/// Version beyond which solc binaries are not fully static, hence need to be patched for NixOS.
static NIXOS_PATCH_REQ: Lazy<VersionReq> = Lazy::new(|| VersionReq::parse(">=0.8.0").unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails with solc 0.7.6, which seems to also be dynamically linked. Maybe we should patch every file in NixOS? statically linked ones should be noop


// Installer type that copies binary data to the appropriate solc binary file:
// 1. create target file to copy binary data
// 2. copy data
Expand All @@ -68,7 +72,33 @@ impl Installer {
let mut content = Cursor::new(&self.binbytes);
std::io::copy(&mut content, &mut f)?;

Ok(solc_path)
if platform::is_nixos() && NIXOS_PATCH_REQ.matches(&self.version) {
patch_for_nixos(solc_path)
} else {
Ok(solc_path)
}
}
}

/// Patch the given binary to use the dynamic linker provided by nixos
pub fn patch_for_nixos(bin: PathBuf) -> Result<PathBuf, SolcVmError> {
let output = Command::new("nix-shell")
.arg("-p")
.arg("patchelf")
.arg("--run")
.arg(format!(
"patchelf --set-interpreter \"$(cat $NIX_CC/nix-support/dynamic-linker)\" {}",
bin.display()
))
.output()
.expect("Failed to execute command");

match output.status.success() {
true => Ok(bin),
false => Err(SolcVmError::CouldNotPatchForNixOs(
String::from_utf8(output.stdout).expect("Found invalid UTF-8 when parsing stdout"),
String::from_utf8(output.stderr).expect("Found invalid UTF-8 when parsing stderr"),
)),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl FromStr for Platform {
}
}

pub fn is_nixos() -> bool {
std::path::Path::new("/etc/NIXOS").exists()
}

/// Read the current machine's platform.
pub fn platform() -> Platform {
match (env::consts::OS, env::consts::ARCH) {
Expand Down