Skip to content

Commit d0c6698

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

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

src/libstore-c/nix_api_store.cc

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ StorePath * nix_store_path_clone(const StorePath * p)
176176
return new StorePath{p->path};
177177
}
178178

179+
nix_derivation * nix_derivation_clone(const nix_derivation * d)
180+
{
181+
return new nix_derivation{d->drv};
182+
}
183+
179184
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json)
180185
{
181186
if (context)
@@ -216,4 +221,98 @@ nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store
216221
NIXC_CATCH_ERRS
217222
}
218223

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

src/libstore-c/nix_api_store.h

Lines changed: 90 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_output;
2830

2931
/**
3032
* @brief Initializes the Nix store library
@@ -235,6 +237,14 @@ StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_deriv
235237
*/
236238
void nix_derivation_free(nix_derivation * drv);
237239

240+
/**
241+
* @brief Copy a `nix_derivation`
242+
*
243+
* @param[in] d the derivation to copy
244+
* @return a new `nix_derivation`
245+
*/
246+
nix_derivation * nix_derivation_clone(const nix_derivation * d);
247+
238248
/**
239249
* @brief Copy the closure of `path` from `srcStore` to `dstStore`.
240250
*
@@ -245,6 +255,86 @@ void nix_derivation_free(nix_derivation * drv);
245255
*/
246256
nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path);
247257

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

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)