support conditional compilation of Nix C APIs

This commit is contained in:
do butterflies cry? 2026-03-27 10:38:56 +10:00
parent 7756365941
commit 1e23515fc1
Signed by: cry
GPG key ID: F68745A836CA0412
12 changed files with 198 additions and 63 deletions

View file

@ -15,18 +15,16 @@ edition = "2024"
path = "src/lib.rs"
[features]
default = ["util"]
expr = []
fetchers = []
flakes = []
store = []
util = []
gc = []
default = []
store = ["nixide-sys/nix-store-c"]
expr = ["store", "nixide-sys/nix-expr-c"]
flake = ["store", "nixide-sys/nix-flake-c", "nixide-sys/nix-fetchers-c"]
[dependencies]
libc = "0.2.183"
stdext = "0.3.3"
nixide-sys = { path = "../nixide-sys", version = "0.1.0" }
ctor = "0.6.3"
nixide-sys = { path = "../nixide-sys", version = "0.1.0", features = ["nix-util-c", "nix-main-c"]}
[dev-dependencies]
serial_test = "3.4.0"

View file

@ -1,31 +1,93 @@
// #![warn(missing_docs)]
// #[allow(unused_extern_crates)]
pub extern crate libc;
pub extern crate nixide_sys as sys;
pub(crate) mod errors;
mod expr;
// mod flake;
mod stdext;
mod store;
pub(crate) mod util;
mod verbosity;
mod version;
#[cfg(feature = "expr")]
mod expr;
#[cfg(feature = "store")]
mod store;
#[cfg(feature = "flake")]
mod flake;
pub use errors::{NixError, NixideError, NixideResult};
pub use expr::{EvalState, EvalStateBuilder, Value, ValueType};
pub use store::{Store, StorePath};
pub use verbosity::NixVerbosity;
pub use version::NixVersion;
/// Sets the verbosity level
#[cfg(feature = "expr")]
pub use expr::{EvalState, EvalStateBuilder, Value, ValueType};
#[cfg(feature = "store")]
pub use store::{Store, StorePath};
use ctor::ctor;
use util::wrappers::AsInnerPtr as _;
pub(crate) static mut INIT_LIBUTIL_STATUS: Option<NixideResult<()>> = None;
#[cfg(feature = "store")]
pub(crate) static mut INIT_LIBSTORE_STATUS: Option<NixideResult<()>> = None;
#[cfg(feature = "expr")]
pub(crate) static mut INIT_LIBEXPR_STATUS: Option<NixideResult<()>> = None;
/// # Warning
///
/// # Arguments
///
/// * `context` - additional error context, used as an output
/// * `level` - verbosity level
pub fn set_verbosity() {
// nix_err nix_set_verbosity(nix_c_context * context, nix_verbosity level);
// XXX: TODO: (implement Context first)
/// > Rust's philosophy is that nothing happens before or after main and [ctor](https://github.com/mmastrac/rust-ctor)
/// > explicitly subverts that. The code that runs in `ctor` functions
/// > should be careful to limit itself to libc functions and code
/// > that does not rely on Rust's stdlib services.
/// > - Excerpt from the [github:mmastrac/rust-ctor README.md](https://github.com/mmastrac/rust-ctor?tab=readme-ov-file#warnings)
#[ctor]
fn init_libutil() {
unsafe {
INIT_LIBUTIL_STATUS = Some(util::wrap::nix_fn!(|ctx: &errors::ErrorContext| unsafe {
sys::nix_libutil_init(ctx.as_ptr());
}));
}
}
/// # TODO
/// **Only run this if the "store" feature flag was enabled**
///
/// # Warning
///
/// > Rust's philosophy is that nothing happens before or after main and [ctor](https://github.com/mmastrac/rust-ctor)
/// > explicitly subverts that. The code that runs in `ctor` functions
/// > should be careful to limit itself to libc functions and code
/// > that does not rely on Rust's stdlib services.
/// > - Excerpt from the [github:mmastrac/rust-ctor README.md](https://github.com/mmastrac/rust-ctor?tab=readme-ov-file#warnings)
#[ctor]
#[cfg(feature = "store")]
fn init_libstore() {
// XXX: TODO: how do I support `sys::nix_libstore_init_no_load_config(context)`?
unsafe {
INIT_LIBSTORE_STATUS = Some(util::wrap::nix_fn!(|ctx: &errors::ErrorContext| unsafe {
sys::nix_libutil_init(ctx.as_ptr());
}));
}
}
/// # TODO
/// **Only run this if the "expr" feature flag was enabled**
//
/// # Warning
///
/// > Rust's philosophy is that nothing happens before or after main and [ctor](https://github.com/mmastrac/rust-ctor)
/// > explicitly subverts that. The code that runs in `ctor` functions
/// > should be careful to limit itself to libc functions and code
/// > that does not rely on Rust's stdlib services.
/// > - Excerpt from the [github:mmastrac/rust-ctor README.md](https://github.com/mmastrac/rust-ctor?tab=readme-ov-file#warnings)
#[ctor]
#[cfg(feature = "expr")]
fn init_libexpr() {
unsafe {
INIT_LIBEXPR_STATUS = Some(util::wrap::nix_fn!(|ctx: &errors::ErrorContext| unsafe {
sys::nix_libexpr_init(ctx.as_ptr());
}));
}
}