From 0b10d6f9dd4457b5f1a86e3b61ad1b4b4956b645 Mon Sep 17 00:00:00 2001 From: Peter Bynum Date: Wed, 10 Dec 2025 10:53:14 -0500 Subject: [PATCH 1/2] add query_from_hash_part c API (cherry picked from commit eaf474bf2408e6ff6478664608c5306b0dc9b66e) --- doc/manual/rl-next/c-api-new-store-methods.md | 8 ++++++++ src/libstore-c/nix_api_store.cc | 16 ++++++++++++++++ src/libstore-c/nix_api_store.h | 11 +++++++++++ 3 files changed, 35 insertions(+) create mode 100644 doc/manual/rl-next/c-api-new-store-methods.md diff --git a/doc/manual/rl-next/c-api-new-store-methods.md b/doc/manual/rl-next/c-api-new-store-methods.md new file mode 100644 index 00000000000..7269847a2c4 --- /dev/null +++ b/doc/manual/rl-next/c-api-new-store-methods.md @@ -0,0 +1,8 @@ +--- +synopsis: "C API: New store API methods" +prs: [14766] +--- + +The C API now includes additional methods: + +- `nix_store_query_path_from_hash_part()` - Get the full store path given its hash part diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 80fcf10cb0d..bfdcab83581 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -453,4 +453,20 @@ nix_err nix_derivation_get_outputs_and_optpaths( NIXC_CATCH_ERRS } +StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * store, const char * hash) +{ + if (context) + context->last_err_code = NIX_OK; + try { + std::optional s = store->ptr->queryPathFromHashPart(hash); + + if (!s.has_value()) { + return nullptr; + } + + return new StorePath{std::move(s.value())}; + } + NIXC_CATCH_ERRS_NULL +} + } // extern "C" diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index 5e542b0caaf..1344b571dc8 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -357,6 +357,17 @@ nix_err nix_derivation_get_outputs_and_optpaths( nix_err nix_derivation_to_json( nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata); +/** + * @brief Query the full store path given the hash part of a valid store + * path, or empty if no matching path is found. + * + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[in] hash Hash part of path as a string + * @return Store path reference, NULL if no matching path is found. + */ +StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * store, const char * hash); + // cffi end #ifdef __cplusplus } From 93387a4f0f384b694d46a8b9c62d1aa680bfc8ba Mon Sep 17 00:00:00 2001 From: Peter Bynum Date: Wed, 10 Dec 2025 11:29:03 -0500 Subject: [PATCH 2/2] Add nix_store_copy_path C API (cherry picked from commit 72ab64b612c15b6f2027bd6d730b5459b93afebb) --- doc/manual/rl-next/c-api-new-store-methods.md | 1 + src/libstore-c/nix_api_store.cc | 22 +++++++++++++++++++ src/libstore-c/nix_api_store.h | 13 +++++++++++ 3 files changed, 36 insertions(+) diff --git a/doc/manual/rl-next/c-api-new-store-methods.md b/doc/manual/rl-next/c-api-new-store-methods.md index 7269847a2c4..28792e7cc42 100644 --- a/doc/manual/rl-next/c-api-new-store-methods.md +++ b/doc/manual/rl-next/c-api-new-store-methods.md @@ -6,3 +6,4 @@ prs: [14766] The C API now includes additional methods: - `nix_store_query_path_from_hash_part()` - Get the full store path given its hash part +- `nix_store_copy_path()` - Copy a single store path between two stores, allows repairs and configuring signature checking diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index bfdcab83581..4133d769f21 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -469,4 +469,26 @@ StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * NIXC_CATCH_ERRS_NULL } +nix_err nix_store_copy_path( + nix_c_context * context, Store * srcStore, Store * dstStore, const StorePath * path, bool repair, bool checkSigs) +{ + if (context) + context->last_err_code = NIX_OK; + try { + if (srcStore == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Source store is null"); + + if (dstStore == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Destination store is null"); + + if (path == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Store path is null"); + + auto repairFlag = repair ? nix::RepairFlag::Repair : nix::RepairFlag::NoRepair; + auto checkSigsFlag = checkSigs ? nix::CheckSigsFlag::CheckSigs : nix::CheckSigsFlag::NoCheckSigs; + nix::copyStorePath(*srcStore->ptr, *dstStore->ptr, path->path, repairFlag, checkSigsFlag); + } + NIXC_CATCH_ERRS +} + } // extern "C" diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index 1344b571dc8..964c4066154 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -368,6 +368,19 @@ nix_err nix_derivation_to_json( */ StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * store, const char * hash); +/** + * @brief Copy a path from one store to another. + * + * @param[out] context Optional, stores error information + * @param[in] srcStore nix source store reference + * @param[in] dstStore nix destination store reference + * @param[in] path The path to copy + * @param[in] repair Whether to repair the path + * @param[in] checkSigs Whether to check path signatures are trusted before copying + */ +nix_err nix_store_copy_path( + nix_c_context * context, Store * srcStore, Store * dstStore, const StorePath * path, bool repair, bool checkSigs); + // cffi end #ifdef __cplusplus }