feat: Enable Nix 2.33 APIs for pre-release testing/validation

Pre-release versions (2.33.0pre) now enable 2.33 APIs, allowing development
and integration testing before the stable release. Tests remain disabled for
2.33 until stable to avoid blocking on unrelated pre-release issues.
This commit is contained in:
Robert Hensing 2025-12-09 22:14:02 +01:00
parent 00fafd48ec
commit 53641a179b
6 changed files with 118 additions and 46 deletions

View file

@ -20,3 +20,5 @@ tempfile = "3.10"
[build-dependencies]
pkg-config = "0.3"
# Needed for version parsing in build.rs
nix-bindings-util = { path = "../nix-bindings-util" }

View file

@ -1,39 +1,6 @@
use nix_bindings_util::nix_version::emit_version_cfg;
fn main() {
// Get nix version
let nix_version = pkg_config::probe_library("nix-store-c").unwrap().version;
// Generate version flags
// Unfortunately, Rust doesn't give us a "greater than" operator in conditional
// compilation, so we pre-evaluate the version comparisons here, making use
// of the multi-valued nature of Rust cfgs.
let relevant_versions = vec!["2.26", "2.33"];
let versions = relevant_versions
.iter()
.map(|v| format!("\"{}\"", v))
.collect::<Vec<_>>()
.join(",");
// Declare the known versions, so that Rust can warn about unknown versions
// that aren't part of `relevant_versions` yet - feel free to add entries.
println!(
"cargo:rustc-check-cfg=cfg(nix_at_least,values({}))",
versions
);
let nix_version = nix_version.split('.').collect::<Vec<&str>>();
let nix_version = (
nix_version[0].parse::<u32>().unwrap(),
nix_version[1].parse::<u32>().unwrap(),
);
for version_str in relevant_versions {
let version = version_str.split('.').collect::<Vec<&str>>();
let version = (
version[0].parse::<u32>().unwrap(),
version[1].parse::<u32>().unwrap(),
);
if nix_version >= version {
println!("cargo:rustc-cfg=nix_at_least=\"{}\"", version_str);
}
}
emit_version_cfg(&nix_version, &["2.26", "2.33.0pre", "2.33"]);
}

View file

@ -1,4 +1,4 @@
#![cfg(nix_at_least = "2.33")]
#![cfg(nix_at_least = "2.33.0pre")]
use nix_bindings_bindgen_raw as raw;
use std::ptr::NonNull;

View file

@ -6,7 +6,7 @@ use nix_bindings_util::string_return::{
callback_get_result_string, callback_get_result_string_data,
};
use nix_bindings_util::{check_call, result_string_init};
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::ffi::{c_char, CString};
@ -14,7 +14,7 @@ use std::ptr::null_mut;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, Weak};
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
use crate::derivation::Derivation;
use crate::path::StorePath;
@ -71,7 +71,7 @@ lazy_static! {
static ref STORE_CACHE: Arc<Mutex<StoreCacheMap>> = Arc::new(Mutex::new(HashMap::new()));
}
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
unsafe extern "C" fn callback_get_result_store_path_set(
_context: *mut raw::c_context,
user_data: *mut std::os::raw::c_void,
@ -88,7 +88,7 @@ unsafe extern "C" fn callback_get_result_store_path_set(
ret.push(store_path);
}
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
fn callback_get_result_store_path_set_data(vec: &mut Vec<StorePath>) -> *mut std::os::raw::c_void {
vec as *mut Vec<StorePath> as *mut std::os::raw::c_void
}
@ -274,7 +274,7 @@ impl Store {
/// # Returns
/// A [`Derivation`] object if parsing succeeds, or an error if the JSON is invalid
/// or malformed.
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
#[doc(alias = "nix_derivation_from_json")]
pub fn derivation_from_json(&mut self, json: &str) -> Result<Derivation> {
let json_cstr = CString::new(json)?;
@ -302,7 +302,7 @@ impl Store {
///
/// # Returns
/// The store path of the derivation (ending in `.drv`).
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
#[doc(alias = "nix_add_derivation")]
pub fn add_derivation(&mut self, drv: &Derivation) -> Result<StorePath> {
unsafe {
@ -331,7 +331,7 @@ impl Store {
/// # Returns
/// A [`BTreeMap`] mapping output names (e.g., "out", "dev", "doc") to their store paths.
/// The map is ordered alphabetically by output name for deterministic iteration.
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
#[doc(alias = "nix_store_realise")]
pub fn realise(&mut self, path: &StorePath) -> Result<BTreeMap<String, StorePath>> {
let mut outputs = BTreeMap::new();
@ -388,7 +388,7 @@ impl Store {
///
/// # Returns
/// A vector of store paths in the closure, in no particular order.
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
#[doc(alias = "nix_store_get_fs_closure")]
pub fn get_fs_closure(
&mut self,
@ -544,6 +544,7 @@ mod tests {
assert!(weak.inner.upgrade().is_none());
}
#[cfg(nix_at_least = "2.33.0pre")]
fn create_temp_store() -> (Store, tempfile::TempDir) {
let temp_dir = tempfile::tempdir().unwrap();
@ -616,7 +617,7 @@ mod tests {
}
#[test]
#[cfg(nix_at_least = "2.33")]
#[cfg(nix_at_least = "2.33.0pre")]
fn derivation_from_invalid_json() {
let (mut store, temp_dir) = create_temp_store();
let result = store.derivation_from_json("not valid json");

View file

@ -2,3 +2,4 @@ pub mod context;
pub mod settings;
#[macro_use]
pub mod string_return;
pub mod nix_version;

View file

@ -0,0 +1,101 @@
//! Nix version parsing and conditional compilation support.
/// Emit [`cargo:rustc-cfg`] directives for Nix version-based conditional compilation.
///
/// Call from build.rs with the Nix version and desired version gates.
///
/// [`cargo:rustc-cfg`]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-cfg
///
/// # Example
///
/// ```
/// use nix_bindings_util::nix_version::emit_version_cfg;
/// # // Stub pkg_config so that we can render a full usage example
/// # mod pkg_config { pub fn probe_library(_: &str) -> Result<Library, ()> { Ok(Library { version: "2.33.0pre".into() }) }
/// # pub struct Library { pub version: String } }
///
/// let nix_version = pkg_config::probe_library("nix-store-c").unwrap().version;
/// emit_version_cfg(&nix_version, &["2.26", "2.33.0pre", "2.33"]);
/// ```
///
/// Emits `nix_at_least="2.26"` and `nix_at_least="2.33.0pre"` for version 2.33.0pre,
/// usable as `#[cfg(nix_at_least = "2.26")]`.
pub fn emit_version_cfg(nix_version: &str, relevant_versions: &[&str]) {
// Declare the known versions for cargo check-cfg
let versions = relevant_versions
.iter()
.map(|v| format!("\"{}\"", v))
.collect::<Vec<_>>()
.join(",");
println!(
"cargo:rustc-check-cfg=cfg(nix_at_least,values({}))",
versions
);
let nix_version = parse_version(nix_version);
for version_str in relevant_versions {
let version = parse_version(version_str);
if nix_version >= version {
println!("cargo:rustc-cfg=nix_at_least=\"{}\"", version_str);
}
}
}
/// Parse a Nix version string into a comparable tuple `(major, minor, patch)`.
///
/// Pre-release versions (containing `"pre"`) get patch = -1, sorting before stable releases.
/// Omitted patch defaults to 0.
///
/// # Examples
///
/// ```
/// use nix_bindings_util::nix_version::parse_version;
///
/// assert_eq!(parse_version("2.26"), (2, 26, 0));
/// assert_eq!(parse_version("2.33.0pre"), (2, 33, -1));
/// assert_eq!(parse_version("2.33"), (2, 33, 0));
/// assert_eq!(parse_version("2.33.1"), (2, 33, 1));
///
/// // Pre-release versions sort before stable
/// assert!(parse_version("2.33.0pre") < parse_version("2.33"));
/// ```
pub fn parse_version(version_str: &str) -> (u32, u32, i32) {
let parts = version_str.split('.').collect::<Vec<&str>>();
let major = parts[0].parse::<u32>().unwrap();
let minor = parts[1].parse::<u32>().unwrap();
let patch = if parts.get(2).map_or(false, |s| s.contains("pre")) {
-1i32
} else {
parts
.get(2)
.and_then(|s| s.parse::<i32>().ok())
.unwrap_or(0)
};
(major, minor, patch)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_version() {
assert_eq!(parse_version("2.26"), (2, 26, 0));
assert_eq!(parse_version("2.33.0pre"), (2, 33, -1));
assert_eq!(parse_version("2.33"), (2, 33, 0));
assert_eq!(parse_version("2.33.1"), (2, 33, 1));
}
#[test]
fn test_version_ordering() {
// Pre-release versions should sort before stable
assert!(parse_version("2.33.0pre") < parse_version("2.33"));
assert!(parse_version("2.33.0pre") < parse_version("2.33.0"));
// Normal version ordering
assert!(parse_version("2.26") < parse_version("2.33"));
assert!(parse_version("2.33") < parse_version("2.33.1"));
}
}