There are two ways to interface with nix: embedding it, or as a plugin. Embedding means you link one of the nix libraries in your program and use it from there, while being a plugin means you make a library that gets loaded by the nix evaluator, specified through a configuration option.
Embedding the Nix Evaluator
These examples don't include error handling. See the Handling errors section for more information.
main.c:
#include <stdio.h>
int main() {
return 0;
}
nix_err nix_gc_decref(nix_c_context *context, const void *object)
Decrease the GC refcount.
Value * nix_alloc_value(nix_c_context *context, State *state)
Allocate a Nix value.
nix_err nix_libexpr_init(nix_c_context *context)
Initializes the Nix expression evaluator.
nix_err nix_expr_eval_from_string(nix_c_context *context, State *state, const char *expr, const char *path, Value *value)
Parses and evaluates a Nix expression from a string.
State * nix_state_create(nix_c_context *context, const char **searchPath, Store *store)
Creates a new Nix state.
nix_err nix_value_force(nix_c_context *context, State *state, Value *value)
Forces the evaluation of a Nix value.
void nix_state_free(State *state)
Frees a Nix state.
void nix_store_unref(Store *store)
Unref a nix store.
struct Store Store
reference to a nix store
Definition: nix_api_store.h:23
Store * nix_store_open(nix_c_context *, const char *uri, const char ***params)
Open a nix store.
const char * nix_get_string(nix_c_context *context, const Value *value)
Get string.
Main entry for the libexpr C bindings.
Main entry for the libutil C bindings.
libexpr C bindings dealing with values
Represents a nix evaluator state.
Usage:
$ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main
$ ./main
nix version 1.2.3
Writing a Nix Plugin
plugin.c:
} else {
}
}
void nix_plugin_entry() {
const char* args[] = {"n", NULL};
PrimOp *p =
nix_alloc_primop(NULL, increment, 1,
"increment", args,
"Example nix plugin function: increments an int");
}
struct PrimOp PrimOp
PrimOp function.
Definition: nix_api_value.h:55
nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp)
add a primop to builtins
PrimOp * nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity, const char *name, const char **args, const char *doc)
Allocate a primop.
ValueType nix_get_type(nix_c_context *context, const Value *value)
Get value type.
int64_t nix_get_int(nix_c_context *context, const Value *value)
Get int value.
nix_err nix_set_null(nix_c_context *context, Value *value)
Set null.
nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i)
Set an int.
Usage:
$ gcc plugin.c $(pkg-config nix-expr-c --libs --cflags) -shared -o plugin.so
$ nix --plugin-files ./plugin.so repl
nix-repl> builtins.increment 1
2