1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-20 09:19:36 +01:00

Add new C API for working with derivations

Also test the APIs we just added.
This commit is contained in:
John Ericson 2025-09-02 11:20:51 -04:00
parent 1a69fc6ab5
commit 9bc218ca3f
13 changed files with 270 additions and 47 deletions

View file

@ -166,11 +166,44 @@ void nix_store_path_free(StorePath * sp)
delete sp;
}
void nix_derivation_free(nix_derivation * drv)
{
delete drv;
}
StorePath * nix_store_path_clone(const StorePath * p)
{
return new StorePath{p->path};
}
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto drv = nix::Derivation::fromJSON(*store->ptr, nlohmann::json::parse(json));
auto drvPath = nix::writeDerivation(*store->ptr, drv, nix::NoRepair, /* read only */ true);
drv.checkInvariants(*store->ptr, drvPath);
return new nix_derivation{drv};
}
NIXC_CATCH_ERRS_NULL
}
StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto ret = nix::writeDerivation(*store->ptr, derivation->drv, nix::NoRepair);
return new StorePath{ret};
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path)
{
if (context)

View file

@ -23,6 +23,8 @@ extern "C" {
typedef struct Store Store;
/** @brief Nix store path */
typedef struct StorePath StorePath;
/** @brief Nix Derivation */
typedef struct nix_derivation nix_derivation;
/**
* @brief Initializes the Nix store library
@ -207,6 +209,32 @@ nix_err nix_store_realise(
nix_err
nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data);
/**
* @brief Create a `nix_derivation` from a JSON representation of that derivation.
*
* @param[out] context Optional, stores error information.
* @param[in] store nix store reference.
* @param[in] json JSON of the derivation as a string.
*/
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json);
/**
* @brief Add the given `nix_derivation` to the given store
*
* @param[out] context Optional, stores error information.
* @param[in] store nix store reference. The derivation will be inserted here.
* @param[in] derivation nix_derivation to insert into the given store.
*/
StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation);
/**
* @brief Deallocate a `nix_derivation`
*
* Does not fail.
* @param[in] drv the derivation to free
*/
void nix_derivation_free(nix_derivation * drv);
/**
* @brief Copy the closure of `path` from `srcStore` to `dstStore`.
*

View file

@ -1,6 +1,7 @@
#ifndef NIX_API_STORE_INTERNAL_H
#define NIX_API_STORE_INTERNAL_H
#include "nix/store/store-api.hh"
#include "nix/store/derivations.hh"
extern "C" {
@ -14,6 +15,11 @@ struct StorePath
nix::StorePath path;
};
struct nix_derivation
{
nix::Derivation drv;
};
} // extern "C"
#endif