Expand description
Nix primitive operations (primops).
This module provides a safe, closure-based API for registering custom Nix primitive operations (primops).
§Overview
Primops are Rust functions that appear as Nix builtins. There are two ways to expose a primop:
- Global builtin: call
PrimOp::registerbefore creating anyEvalState. All subsequently created states will include the primop inbuiltins. - Value-embedded: call
PrimOp::into_valueon an existingEvalStateto obtain a callableValue.
§Example
use std::sync::Arc;
use nix_bindings::{
Context,
EvalStateBuilder,
Store,
primop::{NixValueOps, PrimOp},
};
let ctx = Arc::new(Context::new()?);
// Register a global builtin that doubles an integer
PrimOp::new(&ctx, "double", 1, Some("Double an integer"), |args, ret| {
let n = args[0].as_int()?;
ret.set_int(n * 2)
})?
.register(&ctx)?;
let store = Arc::new(Store::open(&ctx, None)?);
let state = EvalStateBuilder::new(&store)?.build()?;
let result = state.eval_from_string("builtins.double 21", "<eval>")?;
assert_eq!(result.as_int()?, 42);Structs§
- ArgAttrs
- A borrowed Nix attribute set value.
- ArgList
- A borrowed Nix list value.
- PrimOp
- A Nix primitive operation (primop) wrapping a Rust closure.
- Prim
OpArg - A borrowed Nix value passed as an argument to a primop callback.
- Prim
OpRet - The writable return-value slot provided to a primop closure.
- Prim
OpValue - An owned Nix value used within a primop callback.
Traits§
- NixValue
Ops - Read access to a Nix value.