How to use libstore instead of `nix-store --realise`

Hello,

I have an application that at times runs nix-store --realize <path> --store daemon. I would like to replace the pointless fork() + exec() and instead send an RPC to nix daemon directly.

It seems to me that I can do that with nix/src/libstore-c/nix_api_store.h at 5fd799cfa70d22a625bc706a599b6244f654718d · NixOS/nix · GitHub, but I’m a bit at a loss of how to use this. For example, how do I correrctly create and initialize the nix_c_context and Store parameters? What’s the best way to link against the library (there doesn’t seem to be a pkg-config file for libstore)?

Could someone help getting me started? Is there perhaps an example for using this API somewhere?

Thanks!

Hello,

For example, how do I correrctly create and initialize the nix_c_context

/**
 * @brief Initializes the Nix store library
 *
 * This function should be called before creating a Store
 * This function can be called multiple times.
 *
 * @param[out] context Optional, stores error information
 * @return NIX_OK if the initialization was successful, an error code otherwise.
 */
nix_err nix_libstore_init(nix_c_context * context);

the context argument to this function is marked out, so I assume correct initialization is

nix_c_context* context;
assert(nix_libstore_init(context) == NIX_OK);

But this is just an hypothesis from reading the header.