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

@ -19,14 +19,16 @@ targets = [ "x86_64-unknown-linux-gnu" ]
[lib]
path = "lib.rs"
# NOTE: `[features]` have a 1-1 correspondence to the
# NOTE: shared libraries produced by the Nix C API.
[features]
default = ["util"]
expr = []
fetchers = []
flakes = []
store = []
util = []
gc = []
default = ["nix-util-c"]
nix-util-c = []
nix-store-c = []
nix-expr-c = []
nix-fetchers-c = []
nix-flake-c = []
nix-main-c = []
[build-dependencies]
bindgen = { default-features = false, features = [ "logging", "runtime" ], version = "0.72.1" }

View file

@ -20,17 +20,26 @@ impl ParseCallbacks for DoxygenCallbacks {
fn main() {
// Invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=include/wrapper.h");
println!("cargo:rerun-if-changed=include/nix-util.h");
println!("cargo:rerun-if-changed=include/nix-store.h");
println!("cargo:rerun-if-changed=include/nix-expr.h");
println!("cargo:rerun-if-changed=include/nix-fetchers.h");
println!("cargo:rerun-if-changed=include/nix-flake.h");
println!("cargo:rerun-if-changed=include/nix-main.h");
// Use pkg-config to find nix-store include and link paths
// This NEEDS to be included, or otherwise `nix_api_store.h` cannot
// be found.
let libs = [
"nix-main-c",
"nix-expr-c",
"nix-store-c",
#[cfg(feature = "nix-util-c")]
"nix-util-c",
#[cfg(feature = "nix-store-c")]
"nix-store-c",
#[cfg(feature = "nix-expr-c")]
"nix-expr-c",
#[cfg(feature = "nix-fetchers-c")]
"nix-fetchers-c",
#[cfg(feature = "nix-flake-c")]
"nix-flake-c",
#[cfg(feature = "nix-main-c")]
"nix-main-c",
];
let lib_args: Vec<String> = libs
@ -50,17 +59,43 @@ fn main() {
.flatten()
.collect();
let bindings = bindgen::Builder::default()
let mut builder = bindgen::Builder::default()
.clang_args(lib_args)
// The input header we would like to generate bindings for
.header("include/wrapper.h")
// Invalidate the built crate when an included header file changes
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Add `doxygen_bindgen` callbacks
.parse_callbacks(Box::new(DoxygenCallbacks))
// Format generated bindings with rustfmt
.formatter(bindgen::Formatter::Rustfmt)
.rustfmt_configuration_file(std::fs::canonicalize(".rustfmt.toml").ok())
.rustfmt_configuration_file(std::fs::canonicalize(".rustfmt.toml").ok());
// The input headers we would like to generate bindings for
#[cfg(feature = "nix-util-c")]
{
builder = builder.header("include/nix-util.h")
}
#[cfg(feature = "nix-store-c")]
{
builder = builder.header("include/nix-store.h")
}
#[cfg(feature = "nix-expr-c")]
{
builder = builder.header("include/nix-expr.h")
}
#[cfg(feature = "nix-fetchers-c")]
{
builder = builder.header("include/nix-fetchers.h")
}
#[cfg(feature = "nix-flake-c")]
{
builder = builder.header("include/nix-flake.h")
}
#[cfg(feature = "nix-main-c")]
{
builder = builder.header("include/nix-main.h")
}
let bindings = builder
// Finish the builder and generate the bindings
.generate()
// Unwrap the Result and panic on failure

View file

@ -0,0 +1,10 @@
// Nix C API for the Nix expressions evaluator.
#include <nix_api_expr.h>
// Nix C API for value manipulation.
//
#include <nix_api_value.h>
// Nix C API for external values.
//
#include <nix_api_external.h>

View file

@ -0,0 +1,3 @@
// Nix C API for fetcher operations.
//
#include <nix_api_fetchers.h>

View file

@ -0,0 +1,3 @@
// Nix C API for flake support.
//
#include <nix_api_flake.h>

View file

@ -0,0 +1,3 @@
// Nix C API for CLI support.
//
#include <nix_api_main.h>

View file

@ -0,0 +1,3 @@
// Nix C API for store operations.
//
#include <nix_api_store.h>

View file

@ -0,0 +1,7 @@
// Nix C API for utilities.
//
// Most notably containing functions for handling
// the `nix_c_context` structure, which is used throughout
// the Nix C APIs for error handling.
//
#include <nix_api_util.h>

View file

@ -1,23 +0,0 @@
// Pure C API for store operations
#include <nix_api_store.h>
// Pure C API for error handling
#include <nix_api_util.h>
// Pure C API for the Nix evaluator
#include <nix_api_expr.h>
// Pure C API for external values
#include <nix_api_external.h>
// Pure C API for value manipulation
#include <nix_api_value.h>
// Pure C API for fetcher operations
#include <nix_api_fetchers.h>
// Pure C API for flake support
#include <nix_api_flake.h>
// Pure C API for main/CLI support
#include <nix_api_main.h>