Nix 2.17.0
Nix, the purely functional package manager; stable external interfaces
All Data Structures Files Functions Variables Typedefs Modules Pages
libexpr

Bindings to the Nix evaluator. More...

Modules

 Primops
 Create your own primops.
 
 GC
 Reference counting and garbage collector operations.
 
 Externals
 Deal with external values.
 
 Manipulating values
 Functions to inspect and change nix Value's.
 

Files

file  nix_api_expr.h
 Main entry for the libexpr C bindings.
 
file  nix_api_value.h
 libexpr C bindings dealing with values
 

Data Structures

struct  State
 Represents a nix evaluator state. More...
 
struct  Value
 Represents a nix value. More...
 

Typedefs

typedef struct State State
 
typedef void Value
 
typedef void Value
 
typedef struct State State
 

Enumerations

enum  ValueType {
  NIX_TYPE_THUNK , NIX_TYPE_INT , NIX_TYPE_FLOAT , NIX_TYPE_BOOL ,
  NIX_TYPE_STRING , NIX_TYPE_PATH , NIX_TYPE_NULL , NIX_TYPE_ATTRS ,
  NIX_TYPE_LIST , NIX_TYPE_FUNCTION , NIX_TYPE_EXTERNAL
}
 

Functions

nix_err nix_libexpr_init (nix_c_context *context)
 Initializes the Nix expression evaluator. More...
 
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. More...
 
nix_err nix_value_call (nix_c_context *context, State *state, Value *fn, Value *arg, Value *value)
 Calls a Nix function with an argument. More...
 
nix_err nix_value_force (nix_c_context *context, State *state, Value *value)
 Forces the evaluation of a Nix value. More...
 
nix_err nix_value_force_deep (nix_c_context *context, State *state, Value *value)
 Forces the deep evaluation of a Nix value. More...
 
Statenix_state_create (nix_c_context *context, const char **searchPath, Store *store)
 Creates a new Nix state. More...
 
void nix_state_free (State *state)
 Frees a Nix state. More...
 
Valuenix_alloc_value (nix_c_context *context, State *state)
 Allocate a Nix value. More...
 

Detailed Description

Bindings to the Nix evaluator.

Example (without error handling):

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.
Represents a nix evaluator state.
Represents a nix value.

Function Documentation

◆ nix_alloc_value()

Value * nix_alloc_value ( nix_c_context context,
State state 
)

Allocate a Nix value.

Owned by the GC. Use nix_gc_decref when you're done with the pointer

Parameters
[out]contextOptional, stores error information
[in]statenix evaluator state
Returns
value, or null in case of errors

◆ nix_expr_eval_from_string()

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.

Parameters
[out]contextOptional, stores error information
[in]stateThe state of the evaluation.
[in]exprThe Nix expression to parse.
[in]pathThe file path to associate with the expression.
[out]valueThe result of the evaluation. You should allocate this yourself.
Returns
NIX_OK if the evaluation was successful, an error code otherwise.

◆ nix_libexpr_init()

nix_err nix_libexpr_init ( nix_c_context context)

Initializes the Nix expression evaluator.

This function should be called before creating a State. This function can be called multiple times.

Parameters
[out]contextOptional, stores error information
Returns
NIX_OK if the initialization was successful, an error code otherwise.

◆ nix_state_create()

State * nix_state_create ( nix_c_context context,
const char **  searchPath,
Store store 
)

Creates a new Nix state.

Parameters
[out]contextOptional, stores error information
[in]searchPathThe NIX_PATH.
[in]storeThe Nix store to use.
Returns
A new Nix state or NULL on failure.

◆ nix_state_free()

void nix_state_free ( State state)

Frees a Nix state.

Does not fail.

Parameters
[in]stateThe state to free.

◆ nix_value_call()

nix_err nix_value_call ( nix_c_context context,
State state,
Value fn,
Value arg,
Value value 
)

Calls a Nix function with an argument.

Parameters
[out]contextOptional, stores error information
[in]stateThe state of the evaluation.
[in]fnThe Nix function to call.
[in]argThe argument to pass to the function.
[out]valueThe result of the function call.
Returns
NIX_OK if the function call was successful, an error code otherwise.

◆ nix_value_force()

nix_err nix_value_force ( nix_c_context context,
State state,
Value value 
)

Forces the evaluation of a Nix value.

The Nix interpreter is lazy, and not-yet-evaluated Values can be of type NIX_TYPE_THUNK instead of their actual value.

This function converts these Values into their final type.

Note
You don't need this function for basic API usage, since all functions that return a value call it for you. The only place you will see a NIX_TYPE_THUNK is in the primop callback.
Parameters
[out]contextOptional, stores error information
[in]stateThe state of the evaluation.
[in,out]valueThe Nix value to force.
Postcondition
values is not of type NIX_TYPE_THUNK
Returns
NIX_OK if the force operation was successful, an error code otherwise.

◆ nix_value_force_deep()

nix_err nix_value_force_deep ( nix_c_context context,
State state,
Value value 
)

Forces the deep evaluation of a Nix value.

Recursively calls nix_value_force

See also
nix_value_force
Warning
Calling this function on a recursive data structure will cause a stack overflow.
Parameters
[out]contextOptional, stores error information
[in]stateThe state of the evaluation.
[in,out]valueThe Nix value to force.
Returns
NIX_OK if the deep force operation was successful, an error code otherwise.