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

flake: Follow package Nix version in dev shell #174

Merged
merged 3 commits into from
Sep 11, 2024
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
137 changes: 70 additions & 67 deletions attic/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,85 @@
//!
//! We link against libnixstore to perform actions on the Nix Store.

use cc::Build;
use version_compare::Version;

struct NixDependency {
version: String,
fn main() {
#[cfg(feature = "nix_store")]
nix_store::build_bridge();
}

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");
#[cfg(feature = "nix_store")]
mod nix_store {
use cc::Build;
use version_compare::Version;

struct NixDependency {
version: String,
}

Self {
version: library.version,
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();
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");
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();
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();
pkg_config::Config::new()
.atleast_version("2.4")
.probe("nix-main")
.unwrap();
}
}
}

fn main() {
#[cfg(feature = "nix_store")]
build_bridge();
}

#[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");
std::fs::write(dir.path().join("uds-remote-store.md"), "\"\"").unwrap();
dir
};

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")
.flag("-include")
.flag("nix/config.h")
.flag("-idirafter")
.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"));

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
nix_dep.emit_cargo_metadata();
pub 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");
std::fs::write(dir.path().join("uds-remote-store.md"), "\"\"").unwrap();
dir
};

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")
.flag("-include")
.flag("nix/config.h")
.flag("-idirafter")
.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"));

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
nix_dep.emit_cargo_metadata();
}
}
4 changes: 4 additions & 0 deletions crane.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ let
maintainers = with maintainers; [ zhaofengli ];
platforms = platforms.linux ++ platforms.darwin;
};

passthru = {
inherit nix;
};
} // args // extraArgs);

attic = mkAttic {
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake/devshells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ in
NIX_PATH = "nixpkgs=${pkgs.path}";

# See comment in `attic/build.rs`
NIX_INCLUDE_PATH = "${lib.getDev pkgs.nixVersions.nix_2_24}/include";
NIX_INCLUDE_PATH = "${lib.getDev self'.packages.attic.passthru.nix}/include";

# Used by `just with-nix` to build/test with alternative Nix versions.
NIX_VERSIONS = config.attic.nix-versions.manifestFile;
Expand Down