Nix 2.17.0
Nix, the purely functional package manager; stable external interfaces
Loading...
Searching...
No Matches
Getting started

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 <nix_api_util.h>
#include <nix_api_expr.h>
#include <nix_api_value.h>
#include <stdio.h>
int main() {
Store* store = nix_store_open(NULL, "dummy://", NULL);
State* state = nix_state_create(NULL, NULL, store); // empty nix path
Value *value = nix_alloc_value(NULL, state);
nix_expr_eval_from_string(NULL, state, "builtins.nixVersion", ".", value);
nix_value_force(NULL, state, value);
printf("nix version: %s\n", nix_get_string(NULL, value));
nix_gc_decref(NULL, value);
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.
Represents a nix value.

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:

#include <nix_api_util.h>
#include <nix_api_expr.h>
#include <nix_api_value.h>
void increment(State* state, int pos, Value** args, Value* v) {
nix_value_force(NULL, state, args[0]);
if (nix_get_type(NULL, args[0]) == NIX_TYPE_INT) {
nix_set_int(NULL, v, nix_get_int(NULL, args[0]) + 1);
} else {
nix_set_null(NULL, v);
}
}
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");
nix_gc_decref(NULL, p);
}
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