Skip to content

Commit cd12d13

Browse files
libstore-c: add more derivation functions
1 parent 773dd61 commit cd12d13

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

src/libstore-c/nix_api_store.cc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,96 @@ nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store
216216
NIXC_CATCH_ERRS
217217
}
218218

219+
nix_err nix_store_drv_from_path(
220+
nix_c_context * context,
221+
Store * store,
222+
const StorePath * path,
223+
void (*callback)(void * userdata, const nix_derivation * drv),
224+
void * userdata)
225+
{
226+
if (context)
227+
context->last_err_code = NIX_OK;
228+
try {
229+
nix::Derivation drv = store->ptr->derivationFromPath(path->path);
230+
if (callback) {
231+
const nix_derivation tmp{drv};
232+
callback(userdata, &tmp);
233+
}
234+
}
235+
NIXC_CATCH_ERRS
236+
}
237+
238+
nix_err nix_derivation_get_outputs(
239+
nix_c_context * context,
240+
const nix_derivation * drv,
241+
void (*callback)(void * userdata, const char * name, const nix_derivation_output * drv_output),
242+
void * userdata)
243+
{
244+
if (context)
245+
context->last_err_code = NIX_OK;
246+
try {
247+
if (callback) {
248+
for (const auto & [name, result] : drv->drv.outputs) {
249+
const nix_derivation_output tmp{result};
250+
callback(userdata, name.c_str(), &tmp);
251+
}
252+
}
253+
}
254+
NIXC_CATCH_ERRS
255+
}
256+
257+
nix_err nix_derivation_get_outputs_and_optpaths(
258+
nix_c_context * context,
259+
const nix_derivation * drv,
260+
const Store * store,
261+
void (*callback)(
262+
void * userdata, const char * name, const nix_derivation_output * drv_output, const StorePath * path),
263+
void * userdata)
264+
{
265+
if (context)
266+
context->last_err_code = NIX_OK;
267+
try {
268+
auto value = drv->drv.outputsAndOptPaths(store->ptr->config);
269+
if (callback) {
270+
for (const auto & [name, result] : value) {
271+
const nix_derivation_output tmp_output{result.first};
272+
273+
if (auto store_path = result.second) {
274+
const StorePath tmp_path{*store_path};
275+
callback(userdata, name.c_str(), &tmp_output, &tmp_path);
276+
} else {
277+
callback(userdata, name.c_str(), &tmp_output, nullptr);
278+
}
279+
}
280+
}
281+
}
282+
NIXC_CATCH_ERRS
283+
}
284+
285+
nix_err nix_derivation_get_structured_attrs(
286+
nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata)
287+
{
288+
if (context)
289+
context->last_err_code = NIX_OK;
290+
try {
291+
if (auto structuredAttrs = drv->drv.structuredAttrs) {
292+
if (callback) {
293+
auto result = structuredAttrs->structuredAttrs.dump();
294+
callback(result.data(), result.size(), userdata);
295+
}
296+
}
297+
}
298+
NIXC_CATCH_ERRS
299+
}
300+
301+
nix_derivation_output * nix_derivation_output_clone(const nix_derivation_output * o)
302+
{
303+
return new nix_derivation_output{o->drv_out};
304+
}
305+
306+
void nix_derivation_output_free(nix_derivation_output * o)
307+
{
308+
delete o;
309+
}
310+
219311
} // extern "C"

src/libstore-c/nix_api_store.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ typedef struct Store Store;
2525
typedef struct StorePath StorePath;
2626
/** @brief Nix Derivation */
2727
typedef struct nix_derivation nix_derivation;
28+
/** @brief Nix Derivation Output */
29+
typedef struct nix_derivation_output nix_derivation_noutput;
2830

2931
/**
3032
* @brief Initializes the Nix store library
@@ -245,6 +247,86 @@ void nix_derivation_free(nix_derivation * drv);
245247
*/
246248
nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path);
247249

250+
/**
251+
* @brief Returns the derivation associated with the store path
252+
*
253+
* @note The callback borrows the Derivation only for the duration of the call.
254+
*
255+
* @param[out] context Optional, stores error information
256+
* @param[in] store The nix store
257+
* @param[in] path The nix store path
258+
* @param[in] callback The callback to call
259+
* @param[in] userdata The userdata to pass to the callback
260+
*/
261+
nix_err nix_store_drv_from_path(
262+
nix_c_context * context,
263+
Store * store,
264+
const StorePath * path,
265+
void (*callback)(void * userdata, const nix_derivation * drv),
266+
void * userdata);
267+
268+
/**
269+
* @brief Iterate through all of the outputs in a derivation
270+
*
271+
* @note The callback borrows the DerivationOutput only for the duration of the call.
272+
*
273+
* @param[out] context Optional, stores error information
274+
* @param[in] drv The derivation
275+
* @param[in] callback The function to call on every output
276+
* @param[in] userdata Userdata to pass to the callback
277+
*/
278+
nix_err nix_derivation_get_outputs(
279+
nix_c_context * context,
280+
const nix_derivation * drv,
281+
void (*callback)(void * userdata, const char * name, const nix_derivation_output * drv_output),
282+
void * userdata);
283+
284+
/**
285+
* @brief Iterate and get all of the derivation outputs and their store paths.
286+
*
287+
* @note The callback borrows the DerivationOutput and StorePath only for the duration of the call.
288+
*
289+
* @param[out] context Optional, stores error information
290+
* @param[in] drv The derivation
291+
* @param[in] store The nix store
292+
* @param[in] callback The function to call on every output and store path
293+
* @param[in] userdata The userdata to pass to the callback
294+
*/
295+
nix_err nix_derivation_get_outputs_and_optpaths(
296+
nix_c_context * context,
297+
const nix_derivation * drv,
298+
const Store * store,
299+
void (*callback)(
300+
void * userdata, const char * name, const nix_derivation_output * drv_output, const StorePath * path),
301+
void * userdata);
302+
303+
/**
304+
* @brief Gets the structured attrs of derivation as a JSON string
305+
*
306+
* @param[out] context Optional, stores error information
307+
* @param[in] drv The derivation
308+
* @param[in] callback Called with the JSON string
309+
* @param[in] user_data Arbitrary data passed to the callback
310+
*/
311+
nix_err nix_derivation_get_structured_attrs(
312+
nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata);
313+
314+
/**
315+
* @brief Copy of a 'nix_derivation_output'
316+
*
317+
* @param[in] o the derivation output to copy
318+
* @return a new 'nix_derivation_output'
319+
*/
320+
nix_derivation_output * nix_derivation_output_clone(const nix_derivation_output * o);
321+
322+
/**
323+
* @brief Deallocate a 'nix_derivation_output'
324+
*
325+
* Does not fail.
326+
* @param[in] o the derivation output to free
327+
*/
328+
void nix_derivation_output_free(nix_derivation_output * o);
329+
248330
// cffi end
249331
#ifdef __cplusplus
250332
}

src/libstore-c/nix_api_store_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ struct nix_derivation
2020
nix::Derivation drv;
2121
};
2222

23+
struct nix_derivation_output
24+
{
25+
nix::DerivationOutput drv_out;
26+
};
27+
2328
} // extern "C"
2429

2530
#endif

0 commit comments

Comments
 (0)