!lTqhfnNvIkcEackxxk:matrix.org

Nix Bindings

83 Members
Nix C API https://github.com/NixOS/nix/blob/master/doc/manual/src/c-api.md, language-specific bindings, and their use cases (currently broken link: https://nixos.org/manual/nix/unstable/c-api)22 Servers

Load older messages


SenderMessageTime
14 Sep 2025
@emma:rory.gay@emma:rory.gay joined the room.08:39:35
18 Sep 2025
@jaredbaur:matrix.orgJared Baur joined the room.05:37:32
23 Sep 2025
@a-kenji:matrix.orgkenji changed their display name from a-kenji to kenji.10:40:46
25 Sep 2025
@rouffy:catgirl.cloudrouffy joined the room.01:47:04
30 Sep 2025
@17lifers:mikuplushfarm.ovh17lifers (at mikuplushfarm) joined the room.16:05:36
16 Oct 2025
@vorburger:matrix.orgVorburger.ch joined the room.22:00:15
22 Oct 2025
@jake:hillion.co.ukJake Hillion joined the room.10:45:39
24 Oct 2025
@programmed_insanity:matrix.orgProgrammedInsanity joined the room.18:07:19
7 Nov 2025
@emma:rory.gay@emma:rory.gay left the room.22:39:49
15 Nov 2025
@noib3:matrix.orgnoib3 joined the room.13:27:43
@noib3:matrix.orgnoib3

Hi everyone, I'm trying to write a simple Nix plugin in Rust that adds a builtins.mylib primop with a single add function, but it immediately segfaults when I call nix_alloc_value. This is the current code:

const NO_ARGS: &[*const c_char; 1] = &[ptr::null_mut()];

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn add(
    _user_data: *mut c_void,
    ctx: *mut nix_c_context,
    _state: *mut EvalState,
    args: *mut *mut Value,
    ret: *mut Value,
) {
    let a = nix_get_int(ctx, *args.offset(0));
    let b = nix_get_int(ctx, *args.offset(1));
    nix_init_int(ctx, ret, a + b);
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn my_lib(
    _user_data: *mut c_void,
    ctx: *mut nix_c_context,
    state: *mut EvalState,
    _args: *mut *mut Value,
    ret: *mut Value,
) {
    // Create an attrset builder with a capacity of 1.
    let builder = nix_make_bindings_builder(ctx, state, 1);

    let add_name = c"add".as_ptr();

    let mut add_args: [*const c_char; 3] =
        [c"a".as_ptr(), c"b".as_ptr(), ptr::null()];

    let add_primop = nix_alloc_primop(
        ctx,
        Some(add),
        2,
        add_name,
        add_args.as_mut_ptr(),
        c"Add two integers together".as_ptr(),
        ptr::null_mut(),
    );

    // Convert the primop into a Value and add it to the builder.
    let add_value = nix_alloc_value(ctx, state);
    nix_init_primop(ctx, add_value, add_primop);
    nix_bindings_builder_insert(ctx, builder, add_name, add_value);

    // Finalize the builder.
    nix_make_attrs(ctx, ret, builder);

    // Clean up.
    nix_bindings_builder_free(builder);
    nix_value_decref(ctx, add_value);
    nix_gc_decref(ctx, add_primop as *const c_void);
}

#[unsafe(no_mangle)]
#[allow(clippy::missing_safety_doc)]
#[allow(unsafe_op_in_unsafe_fn)]
pub unsafe extern "C" fn nix_plugin_entry() {
    let ctx = nix_c_context_create();

    let primop = nix_alloc_primop(
        ctx,
        Some(my_lib),
        0,
        c"mylib".as_ptr(),
        NO_ARGS as *const _ as *mut _,
        c"mylib".as_ptr(),
        ptr::null_mut(),
    );

    nix_register_primop(ctx, primop);

    nix_gc_decref(ctx, primop as *const c_void);

    nix_c_context_free(ctx);
}

Which results in:

$ nix --plugin-files ./target/debug/libnix_jettison.dylib repl
Nix 2.32.3
Type :? for help.
nix-repl> builtins.mylib
fish: Job 1, '/nix/store/0r71b4nz6rizb9l0fxdl…' terminated by signal SIGSEGV (Address boundary error)
13:33:49
@hsjobeki:matrix.orghsjobekiI also don't know much about nix bindings. I'm just in this channel to listen. Best advice i can give is to take a look at https://github.com/nixops4/nixops4 this is maintained by roberth who is a nix maintainer.14:23:26
23 Nov 2025
@pascalj:matrix.orgpascal joined the room.21:23:35
13 Dec 2025
@joerg:thalheim.ioMic92those recently got extracted into their own repo11:28:53
@joerg:thalheim.ioMic92* those bindings recently got extracted into their own repo from nixops411:29:06
@joerg:thalheim.ioMic92I did use the c api already to build a nix plugin, 11:30:24
@joerg:thalheim.ioMic92but haven't published it yet.11:30:29
15 Dec 2025
@ghishadow:matrix.orgghishadow joined the room.08:22:00

There are no newer messages yet.


Back to Room ListRoom Version: 10