From 84a48c18511a5a6dc02439d81ba2db9f5e04f34a Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Mon, 23 Feb 2026 15:40:09 -0800 Subject: [PATCH] Add store and util versions getter, add util init --- nix-bindings-store/src/store.rs | 23 ++++++++++++++++++ nix-bindings-util/src/lib.rs | 39 +++++++++++++++++++++++++++++++ nix-bindings-util/src/settings.rs | 7 +----- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/nix-bindings-store/src/store.rs b/nix-bindings-store/src/store.rs index 213c47d6..4b0d24a4 100644 --- a/nix-bindings-store/src/store.rs +++ b/nix-bindings-store/src/store.rs @@ -272,6 +272,20 @@ impl Store { r } + #[doc(alias = "nix_store_get_version")] + pub fn get_version(&mut self) -> Result { + let mut r = result_string_init!(); + unsafe { + check_call!(raw::store_get_version( + &mut self.context, + self.inner.ptr(), + Some(callback_get_result_string), + callback_get_result_string_data(&mut r) + )) + }?; + r + } + #[doc(alias = "nix_store_parse_path")] pub fn parse_store_path(&mut self, path: &str) -> Result { let path = CString::new(path)?; @@ -612,6 +626,15 @@ mod tests { } } + #[test] + fn version() { + let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap(); + assert_eq!(store.get_version().unwrap(), String::new()); + + let mut store = crate::store::Store::open(None, []).unwrap(); + assert_ne!(store.get_version().unwrap(), String::new()); + } + #[test] fn weak_ref() { let mut store = Store::open(None, HashMap::new()).unwrap(); diff --git a/nix-bindings-util/src/lib.rs b/nix-bindings-util/src/lib.rs index f626d96e..3b068bb0 100644 --- a/nix-bindings-util/src/lib.rs +++ b/nix-bindings-util/src/lib.rs @@ -1,3 +1,7 @@ +use crate::raw_sys as raw; +use anyhow::Result; +use std::ffi::{c_char, CStr}; + pub mod context; pub mod settings; #[macro_use] @@ -6,3 +10,38 @@ pub mod nix_version; // Re-export for use in macros pub use nix_bindings_util_sys as raw_sys; + +#[doc(alias = "nix_libutil_init")] +pub fn init() -> Result<()> { + let mut ctx = context::Context::new(); + unsafe { + check_call!(raw::libutil_init(&mut ctx))?; + } + Ok(()) +} + +#[doc(alias = "nix_version_get")] +pub fn get_version() -> Result<&'static str> { + let c_str = unsafe { + let ptr = raw::version_get(); + CStr::from_ptr(ptr as *const c_char) + }; + + Ok(c_str.to_str()?) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::nix_version::parse_version; + + #[test] + fn init() { + super::init().unwrap(); + } + + #[test] + fn version() { + assert!(parse_version(get_version().unwrap()) > (0, 0, 0)); + } +} diff --git a/nix-bindings-util/src/settings.rs b/nix-bindings-util/src/settings.rs index 8c213aa2..45c741bd 100644 --- a/nix-bindings-util/src/settings.rs +++ b/nix-bindings-util/src/settings.rs @@ -66,16 +66,11 @@ pub fn get(key: &str) -> Result { #[cfg(test)] mod tests { - use crate::check_call; - use super::*; #[ctor::ctor] fn setup() { - let mut ctx = context::Context::new(); - unsafe { - check_call!(nix_bindings_util_sys::libutil_init(&mut ctx)).unwrap(); - } + crate::init().unwrap(); } #[test]