1
0
Fork 0
mirror of https://github.com/zhaofengli/attic.git synced 2024-12-14 11:57:30 +00:00

Fix build for Nix 2.20+

Perform version detection to keep supporting older versions
(and in the future, alternative implementations).

Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
This commit is contained in:
Zhaofeng Li 2024-08-19 12:16:33 -04:00
parent aecca91fad
commit 40c0bb406e
4 changed files with 79 additions and 16 deletions

20
Cargo.lock generated
View file

@ -230,6 +230,7 @@ dependencies = [
"async-stream",
"base64 0.22.1",
"bytes",
"cc",
"cxx",
"cxx-build",
"digest",
@ -250,6 +251,7 @@ dependencies = [
"tempfile",
"tokio",
"tokio-test",
"version-compare",
"wildmatch",
"xdg",
]
@ -1050,13 +1052,13 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.98"
version = "1.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f"
checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48"
dependencies = [
"jobserver",
"libc",
"once_cell",
"shlex",
]
[[package]]
@ -3960,6 +3962,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
@ -4891,6 +4899,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version-compare"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -43,9 +43,11 @@ serde_json = "1.0.96"
tokio-test = "0.4.2"
[build-dependencies]
cc = "1.1.13"
cxx-build = { version = "1.0", optional = true }
pkg-config = "0.3.27"
tempfile = "3"
version-compare = "0.2.0"
[features]
default = [ "nix_store", "tokio" ]

View file

@ -2,6 +2,47 @@
//!
//! We link against libnixstore to perform actions on the Nix Store.
use cc::Build;
use version_compare::Version;
struct NixDependency {
version: String,
}
impl NixDependency {
fn detect() -> Self {
let library = pkg_config::Config::new()
.cargo_metadata(false)
.atleast_version("2.4")
.probe("nix-main")
.expect("Failed to find nix-main >=2.4 through pkg-config");
Self {
version: library.version,
}
}
fn apply_version_flags(&self, build: &mut Build) {
let version = Version::from(&self.version).unwrap();
if version >= Version::from("2.20").unwrap() {
build.flag("-DATTIC_NIX_2_20");
}
}
fn emit_cargo_metadata(&self) {
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-store")
.unwrap();
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-main")
.unwrap();
}
}
fn main() {
#[cfg(feature = "nix_store")]
build_bridge();
@ -9,6 +50,8 @@ fn main() {
#[cfg(feature = "nix_store")]
fn build_bridge() {
let nix_dep = NixDependency::detect();
// Temporary workaround for issue in <https://github.com/NixOS/nix/pull/8484>
let hacky_include = {
let dir = tempfile::tempdir().expect("Failed to create temporary directory for workaround");
@ -16,7 +59,8 @@ fn build_bridge() {
dir
};
cxx_build::bridge("src/nix_store/bindings/mod.rs")
let mut build = cxx_build::bridge("src/nix_store/bindings/mod.rs");
build
.file("src/nix_store/bindings/nix.cpp")
.flag("-std=c++2a")
.flag("-O2")
@ -26,19 +70,14 @@ fn build_bridge() {
.flag(hacky_include.path().to_str().unwrap())
// In Nix 2.19+, nix/args/root.hh depends on being able to #include "args.hh" (which is in its parent directory), for some reason
.flag("-I")
.flag(concat!(env!("NIX_INCLUDE_PATH"), "/nix"))
.compile("nixbinding");
.flag(concat!(env!("NIX_INCLUDE_PATH"), "/nix"));
nix_dep.apply_version_flags(&mut build);
build.compile("nixbinding");
println!("cargo:rerun-if-changed=src/nix_store/bindings");
// the -l flags must be after -lnixbinding
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-store")
.unwrap();
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-main")
.unwrap();
nix_dep.emit_cargo_metadata();
}

View file

@ -18,6 +18,14 @@ static nix::StorePath store_path_from_rust(RBasePathSlice base_name) {
return nix::StorePath(sv);
}
static bool hash_is_sha256(const nix::Hash &hash) {
#ifdef ATTIC_NIX_2_20
return hash.algo == nix::HashAlgorithm::SHA256;
#else
return hash.type == nix::htSHA256;
#endif
}
// ========
// RustSink
// ========
@ -44,7 +52,7 @@ CPathInfo::CPathInfo(nix::ref<const nix::ValidPathInfo> pi) : pi(pi) {}
RHashSlice CPathInfo::nar_sha256_hash() {
auto &hash = this->pi->narHash;
if (hash.type != nix::htSHA256) {
if (!hash_is_sha256(hash)) {
throw nix::Error("Only SHA-256 hashes are supported at the moment");
}