Skip to main content

Module primop

Module primop 

Source
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:

§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.
PrimOpArg
A borrowed Nix value passed as an argument to a primop callback.
PrimOpRet
The writable return-value slot provided to a primop closure.
PrimOpValue
An owned Nix value used within a primop callback.

Traits§

NixValueOps
Read access to a Nix value.