-
Notifications
You must be signed in to change notification settings - Fork 2
libstore-c: add derivation functions #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libstore-c: add derivation functions #210
Conversation
WalkthroughAdd C API wrappers and internal types for derivations and outputs, make StorePath parameters const-correct for validation/realise callbacks, implement derivation JSON parse/serialize/clone/free and store add/query/build helpers, and introduce a test pre-build helper, test mains, test-harness refactor, test data, and a derivation unit test. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Test as Test client
participant C_API as C API (nix_api_store)
participant Store
participant Callback
Test->>C_API: nix_derivation_from_json(ctx, store, json)
activate C_API
C_API->>C_API: parse JSON -> nix_derivation*
C_API-->>Test: return nix_derivation* wrapper
deactivate C_API
Test->>C_API: nix_add_derivation(ctx, store, derivation)
activate C_API
C_API->>Store: writeDerivation(derivation)
Store-->>C_API: StorePath
C_API-->>Test: StorePath* wrapper
deactivate C_API
sequenceDiagram
autonumber
participant Test as Test client
participant C_API as C API (nix_api_store)
participant Store
participant Callback
Test->>C_API: nix_store_realise(ctx, store, path, userdata, cb)
activate C_API
C_API->>Store: realise(path)
Store-->>C_API: event(outname, out_path)
C_API-->>Callback: cb(userdata, outname, const StorePath * out)
deactivate C_API
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (10)
src/libstore-c/nix_api_store_internal.h (2)
16-20: Wrappers store full nix::Derivation by value; consider pointer semantics to avoid heavy copies.Owning by value simplifies lifetime but can be costly to copy in callbacks and clones. Consider:
- Make
struct Derivation { std::shared_ptr<nix::Derivation> drv; }- Make
struct DerivationOutput { std::shared_ptr<nix::DerivationOutput> drv_out; }
This preserves C ABI (opaque pointer) while reducing copies.
21-24: Same consideration for DerivationOutput.Storing by value copies on every callback iteration; shared ownership avoids that without changing the public C surface.
src/libstore-c/nix_api_store.cc (4)
184-201: Avoid an extra deep copy; also add basic null-arg guards.
- Move the derived value into the wrapper to skip one copy.
- Optional: early-return or throw on
!store || !pathto avoid UB via deref.Apply:
nix_err nix_store_drv_from_path( @@ try { - nix::Derivation drv = store->ptr->derivationFromPath(path->path); + if (!store || !path) { /* keep style consistent with the rest of the file if throwing */ + throw std::invalid_argument("nix_store_drv_from_path: null store or path"); + } + nix::Derivation drv = store->ptr->derivationFromPath(path->path); if (callback) { - const Derivation tmp{drv}; + const Derivation tmp{std::move(drv)}; callback(userdata, &tmp); } }
203-206: Cloning performs a full deep copy.If you switch wrappers to
shared_ptr,clonebecomes a cheap ref bump:-Derivation * nix_drv_clone(const Derivation * d) -{ - return new Derivation{d->drv}; -} +Derivation * nix_drv_clone(const Derivation * d) { + return new Derivation{d->drv}; // shared_ptr copy if refactor applied +}If keeping value semantics, please confirm typical derivation sizes in your workloads to ensure cloning cost is acceptable.
213-230: Borrowed name lifetime not documented; potential misuse.
name.c_str()points into the derivation’s internal map. Add a doc note in the header: thenamepointer is valid only during the callback.Would you like me to patch the header comments to state this explicitly?
232-257: Same lifetime note for name and StorePath; consider avoiding per-iteration copies.
- Document that both
nameandpathare borrowed for the duration of the callback.- Optional: if wrappers move to pointer semantics, you can pass views without copying
DerivationOutputandStorePatheach iteration.src/libstore-c/nix_api_store.h (4)
224-241: Clarify callback contract and nullability.Suggest amending docs:
- Callback may be NULL (no-op).
- The Derivation pointer is borrowed only for the duration of the call and must not be freed; use
nix_drv_cloneto retain it.Proposed doc tweak:
/** * @brief Returns the derivation associated with the store path * - * @note The callback borrows the Derivation only for the duration of the call. + * @note The callback may be NULL (no-op). + * @note The callback borrows the Derivation only for the duration of the call. + * Do not call nix_drv_free() on this pointer; use nix_drv_clone() to retain.
242-257: Clone/free API: document ownership expectations.Add a brief note that only objects returned by
*_cloneare owned and must be freed with the matching*_free.
258-273: Documentnamelifetime and encoding.Add:
nameis borrowed and valid only during the callback.- Encoding: UTF‑8, no NUL bytes.
* @param[in] callback The function to call on every output + * @note The `name` argument is borrowed (valid only during the callback).
274-291: Documentnameandpathlifetimes; clarify thatpathmay be NULL.Add:
* @param[in] callback The function to call on every output and store path * @param[in] userdata The userdata to pass to the callback + * @note `name` and `path` are borrowed and valid only during the callback. + * `path` may be NULL when the output has no associated store path.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libstore-c/nix_api_store.cc(1 hunks)src/libstore-c/nix_api_store.h(2 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (16)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (18)
nix_store_drv_from_path(184-201)nix_store_drv_from_path(184-189)callback(132-132)callback(188-188)callback(216-216)callback(236-236)nix_drv_clone(203-206)nix_drv_clone(203-203)nix_drv_free(208-211)nix_drv_free(208-208)nix_drv_get_outputs(213-230)nix_drv_get_outputs(213-217)nix_drv_get_outputs_and_optpaths(232-257)nix_drv_get_outputs_and_optpaths(232-237)nix_drv_output_clone(259-262)nix_drv_output_clone(259-259)nix_drv_output_free(264-267)nix_drv_output_free(264-264)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (4)
src/libstore-c/nix_api_store.cc (1)
259-267: LGTM for output clone/free.Simple and consistent with the rest of the API.
src/libstore-c/nix_api_store.h (2)
26-29: Opaque typedefs: good addition.Keeps the C ABI stable while allowing internal evolution.
292-306: LGTM for DerivationOutput clone/free docs and API.Consistent with the Derivation ownership model.
src/libstore-c/nix_api_store_internal.h (1)
3-3: Include verified — header and required APIs are present.
Found src/libstore/include/nix/store/derivations.hh (and parsed-derivations.hh); Derivation and outputsAndOptPaths are defined there, and derivationFromPath is declared in src/libstore/include/nix/store/store-api.hh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
🧹 Nitpick comments (6)
src/libstore-test-support/include/nix/store/tests/test-main.hh (1)
11-11: Fix the typo in the function name.There's a typo in the function name - "Buiding" should be "Building".
Apply this fix:
-int testMainForBuidingPre(int argc, char ** argv); +int testMainForBuildingPre(int argc, char ** argv);Note that this typo appears in multiple files across the PR and should be fixed consistently.
src/libstore-tests/nix_api_store.cc (2)
205-221: Consider simplifying the lambda adapter pattern.While the
LambdaAdaptertemplate works, consider usingstd::functionor a simpler approach for better readability and maintainability.Alternative implementation using a simpler approach:
// Option 1: Direct function pointer with capture-less lambda auto cb = [](void * userdata, const char * outname, const char * outPath) { auto * ctx_store = static_cast<std::pair<nix_c_context*, Store*>*>(userdata); StorePath sp{outPath}; auto is_valid_path = nix_store_is_valid_path(ctx_store->first, ctx_store->second, &sp); ASSERT_EQ(is_valid_path, true); }; std::pair<nix_c_context*, Store*> userdata{ctx, store}; auto ret = nix_store_realise(ctx, store, drvPath, &userdata, cb);
233-235: Add file existence check and error handling.The test should verify the JSON file exists and handle potential I/O errors gracefully.
Improve error handling:
+ auto jsonPath = unitTestData / "derivation/ca/self-contained.json"; + ASSERT_TRUE(std::filesystem::exists(jsonPath)) << "Test data file not found: " << jsonPath; + - std::ifstream t{unitTestData / "derivation/ca/self-contained.json"}; + std::ifstream t{jsonPath}; + ASSERT_TRUE(t.is_open()) << "Failed to open test data file: " << jsonPath; std::stringstream buffer; buffer << t.rdbuf();src/libstore-test-support/test-main.cc (2)
10-10: Typo in function name:testMainForBuidingPreThe function name contains a typo - "Buiding" should be "Building".
Apply this diff to fix the typo:
-int testMainForBuidingPre(int argc, char ** argv) +int testMainForBuildingPre(int argc, char ** argv)Note that this will require updating all references to this function in other files, including the header declaration and any callers.
13-13: Minor: Typo in error messageThe error message mentions "libexpr unit tests" but this code is part of libstore test support.
Apply this diff to fix the error message:
- printError("test-build-remote: not supported in libexpr unit tests"); + printError("test-build-remote: not supported in libstore unit tests");src/libstore-c/nix_api_store.h (1)
233-233: Typo in documentation commentMissing closing quote in the comment.
Apply this diff to fix the typo:
- * @brief Deallocate a `nix_derivation' + * @brief Deallocate a `nix_derivation`
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
src/libexpr-tests/main.cc(1 hunks)src/libstore-c/nix_api_store.cc(2 hunks)src/libstore-c/nix_api_store.h(3 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)src/libstore-test-support/include/nix/store/tests/meson.build(1 hunks)src/libstore-test-support/include/nix/store/tests/nix_api_store.hh(2 hunks)src/libstore-test-support/include/nix/store/tests/test-main.hh(1 hunks)src/libstore-test-support/meson.build(1 hunks)src/libstore-test-support/test-main.cc(1 hunks)src/libstore-tests/data/derivation/ca/self-contained.json(1 hunks)src/libstore-tests/main.cc(1 hunks)src/libstore-tests/meson.build(1 hunks)src/libstore-tests/nix_api_store.cc(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/libstore-c/nix_api_store_internal.h
🧰 Additional context used
🧬 Code graph analysis (7)
src/libstore-test-support/test-main.cc (1)
src/libutil/include/nix/util/environment-variables.hh (1)
setEnv(52-52)
src/libstore-tests/main.cc (2)
src/libstore-test-support/include/nix/store/tests/test-main.hh (1)
testMainForBuidingPre(11-11)src/libstore-test-support/test-main.cc (2)
testMainForBuidingPre(10-45)testMainForBuidingPre(10-10)
src/libexpr-tests/main.cc (2)
src/libstore-test-support/include/nix/store/tests/test-main.hh (1)
testMainForBuidingPre(11-11)src/libstore-test-support/test-main.cc (2)
testMainForBuidingPre(10-45)testMainForBuidingPre(10-10)
src/libstore-tests/nix_api_store.cc (2)
src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (1)
store(95-98)src/libstore-c/nix_api_store.cc (14)
nix_derivation_from_json(177-191)nix_derivation_from_json(177-177)nix_add_derivation(193-203)nix_add_derivation(193-193)nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)nix_store_realise(127-154)nix_store_realise(127-132)nix_store_path_free(162-165)nix_store_path_free(162-162)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_store_free(55-58)nix_store_free(55-55)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (16)
nix_derivation_from_json(177-191)nix_derivation_from_json(177-177)nix_add_derivation(193-203)nix_add_derivation(193-193)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_store_drv_from_path(217-234)nix_store_drv_from_path(217-222)callback(132-132)callback(221-221)callback(239-239)callback(259-259)nix_derivation_get_outputs(236-253)nix_derivation_get_outputs(236-240)nix_derivation_get_outputs_and_optpaths(255-280)nix_derivation_get_outputs_and_optpaths(255-260)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (4)
drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)
src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (1)
src/libstore-c/nix_api_store.cc (6)
nix_libstore_init(13-21)nix_libstore_init(13-13)nix_store_open(33-53)nix_store_open(33-33)nix_store_free(55-58)nix_store_free(55-55)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (8)
src/libstore-test-support/meson.build (1)
37-37: LGTM! Test main source file properly added to build.The addition of
test-main.ccto the sources list is correctly integrated with the existing build configuration.src/libstore-tests/meson.build (1)
69-69: LGTM! Main test entry point correctly added.The
main.ccfile is properly added to the test executable sources list.src/libstore-test-support/include/nix/store/tests/meson.build (1)
12-12: LGTM! Test header properly exposed.The
test-main.hhheader is correctly added to the public headers list.src/libstore-tests/data/derivation/ca/self-contained.json (1)
1-23: LGTM! Well-structured test fixture.The JSON derivation fixture is properly formatted and contains all necessary fields for a self-contained content-addressed derivation test case.
src/libstore-tests/nix_api_store.cc (1)
245-248: Type mismatch in the callback signature.The callback expects
const char *for the second parameter but the lambda is usingconst StorePath *. This type mismatch could cause undefined behavior.Let me verify the actual callback signature expected by
nix_store_realise:Based on the search results, please ensure the callback signature matches what
nix_store_realiseexpects. The callback should likely useconst char *for both parameters.src/libstore-tests/main.cc (1)
9-11: Fix the logic error in the return condition.The condition is inverted. When
testMainForBuidingPrereturns 0 (success), the main function should continue. Currently it exits early on success.Apply this fix:
auto res = testMainForBuidingPre(argc, argv); - if (!res) + if (res) return res;Likely an incorrect or invalid review comment.
src/libstore-test-support/test-main.cc (1)
41-41: Verify the correct function for setting environment variablesThe code uses
setEnvbut based on the relevant code snippets, this might not be the correct function name in this context.Let me verify the correct function name for setting environment variables in this codebase:
src/libstore-c/nix_api_store.cc (1)
184-184: Verify the intent of passingtrueforreadOnlyparameterThe code passes
/* read only */ truetowriteDerivation, but then the result is discarded. This seems to be for validation purposes only.Let me verify the intent and usage of this pattern:
| auto res = testMainForBuidingPre(argc, argv); | ||
| if (!res) | ||
| return res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the inverted logic condition.
The condition logic appears to be inverted. When testMainForBuidingPre returns 0 (EXIT_SUCCESS), the test should continue rather than exit.
Apply this fix:
auto res = testMainForBuidingPre(argc, argv);
- if (!res)
+ if (res)
return res;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| auto res = testMainForBuidingPre(argc, argv); | |
| if (!res) | |
| return res; | |
| auto res = testMainForBuidingPre(argc, argv); | |
| if (res) | |
| return res; |
🤖 Prompt for AI Agents
In src/libexpr-tests/main.cc around lines 9 to 11, the logic is inverted:
currently it returns when testMainForBuidingPre returns 0 (success). Change the
condition to return only on non-zero (error) — e.g. check if (res != 0) or if
(res) and then return res; so the test continues when res == 0.
| EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg; | ||
| assert(store); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling pattern is inconsistent
Using EXPECT_NE followed by assert(store) creates an inconsistent error handling pattern. If the store is null, EXPECT_NE will report the failure but continue execution, then assert will crash the test harness.
Apply this diff to use consistent error handling:
- EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg;
- assert(store);
+ ASSERT_NE(store, nullptr) << "Could not open store: " << errMsg;Using ASSERT_NE will properly fail the test and stop execution if the store is null, which is the intended behavior.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg; | |
| assert(store); | |
| ASSERT_NE(store, nullptr) << "Could not open store: " << errMsg; |
🤖 Prompt for AI Agents
In src/libstore-test-support/include/nix/store/tests/nix_api_store.hh around
lines 71 to 72, the test uses EXPECT_NE(store, nullptr) followed by
assert(store) which mixes a non-fatal GoogleTest expectation with a hard abort;
replace this with a single fatal check using ASSERT_NE(store, nullptr) << "Could
not open store: " << errMsg; and remove the subsequent assert(store) so the test
fails and stops cleanly when the store is null.
| // FIXME get rid of these | ||
| nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); | ||
| nix::settings.substituters = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use proper settings API for test configuration.
Setting experimental features and substituters directly on global settings objects may not be thread-safe and could affect other tests.
Consider using the proper API for test isolation:
- // FIXME get rid of these
- nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
- nix::settings.substituters = {};
+ // Use store-specific configuration or proper test setup
+ const char * ca_feature[] = {"experimental-features", "ca-derivations"};
+ const char * no_subst[] = {"substituters", ""};
+ const char ** kvs[] = {ca_feature, no_subst, nullptr};
+ auto * store = nix_store_open(ctx, "local", kvs);Committable suggestion skipped: line range outside the PR's diff.
|
|
||
| auto * store = open_local_store(); | ||
|
|
||
| std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential null pointer dereference.
The environment variable _NIX_TEST_UNIT_DATA might not be set, leading to undefined behavior when passed to std::filesystem::path.
Add null check:
+ const char* test_data_env = getenv("_NIX_TEST_UNIT_DATA");
+ ASSERT_NE(test_data_env, nullptr) << "_NIX_TEST_UNIT_DATA environment variable not set";
- std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
+ std::filesystem::path unitTestData{test_data_env};📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")}; | |
| const char* test_data_env = getenv("_NIX_TEST_UNIT_DATA"); | |
| ASSERT_NE(test_data_env, nullptr) << "_NIX_TEST_UNIT_DATA environment variable not set"; | |
| std::filesystem::path unitTestData{test_data_env}; |
🤖 Prompt for AI Agents
In src/libstore-tests/nix_api_store.cc around line 231, the code calls
getenv("_NIX_TEST_UNIT_DATA") directly into std::filesystem::path which can
dereference a null pointer if the env var is unset; modify the code to call
getenv into a temporary char* variable, check it for nullptr, and handle the
error (e.g., log and return/throw or use a fallback path) before constructing
std::filesystem::path from the non-null value so no null pointer is passed to
the path constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (4)
src/libstore-c/nix_api_store.h (3)
26-29: Fix typedef typo to exposenix_derivation_outputin CCurrently the alias is misspelled, breaking C consumers (prototypes use
nix_derivation_output*but no such typedef exists in C). Correct the alias.-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivatio_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
302-309: Header/impl mismatch:nix_derivation_output_clonevsnix_drv_output_cloneImplementation still uses
nix_drv_output_clone. Rename impl to match header to avoid link errors.I’ve proposed the corresponding .cc fix below.
310-317: Header/impl mismatch:nix_derivation_output_freevsnix_drv_output_freeSame naming mismatch for the free function.
src/libstore-c/nix_api_store.cc (1)
282-290: Rename functions to match header (nix_derivation_output_*)These names must match the header to avoid unresolved symbols.
-nix_derivation_output * nix_drv_output_clone(const nix_derivation_output * o) +nix_derivation_output * nix_derivation_output_clone(const nix_derivation_output * o) { return new nix_derivation_output{o->drv_out}; } -void nix_drv_output_free(nix_derivation_output * o) +void nix_derivation_output_free(nix_derivation_output * o) { delete o; }#!/bin/bash # Verify no remaining mismatched names rg -nP 'nix_drv_output_(clone|free)|nix_derivatio_noutput'
🧹 Nitpick comments (7)
src/libstore-c/nix_api_store.h (6)
192-198: Realise callback type is good; mark input path as const and clarify borrowThe output pointer is borrowed-only; also the input
pathisn’t mutated—make it const.-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, - void * userdata, - void (*callback)(void * userdata, const char * outname, const StorePath * out)); +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path, + void * userdata, + void (*callback)(void * userdata, const char * outname, const StorePath * out));Also consider adding: “The StorePath* passed to callback is valid only for the duration of the call.”
214-222: Document ownership and side effects fornix_derivation_from_jsonClarify that the return value must be freed and that no store write occurs (read-only check).
/** * @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. + * @return Owned pointer; call `nix_derivation_free` when done. + * @note Validates invariants without writing to the store. */
223-231: Explain that this writes to the store and return ownership of StorePathSmall doc tweak to set expectations.
/** * @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. + * @return Owned `StorePath*` of the written derivation; free with `nix_store_path_free`. + * @note Writes the derivation to the store. */
232-239: Fix mismatched quote in docstringUse matching backticks.
-/** - * @brief Deallocate a `nix_derivation' +/** + * @brief Deallocate a `nix_derivation`
268-283: Clarifynameanddrv_outputlifetimes in callbackState explicitly that both pointers are valid only during the callback.
/** * @brief Iterate through all of the outputs in a derivation * - * @note The callback borrows the DerivationOutput only for the duration of the call. + * @note The callback borrows `name` and `drv_output` only for the duration of the call.
284-301: Document nullablepathand borrowMake it clear that
pathmay be NULL and is borrowed./** * @brief Iterate and get all of the derivation outputs and their store paths. * - * @note The callback borrows the DerivationOutput and StorePath only for the duration of the call. + * @note The callback borrows the `drv_output` and `path` only for the duration of the call. + * `path` may be NULL if no store path is associated.src/libstore-c/nix_api_store.cc (1)
127-133: Make input path const to match header suggestionThe path isn’t mutated; align with proposed header change.
-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, - void * userdata, - void (*callback)(void * userdata, const char *, const StorePath *)) +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path, + void * userdata, + void (*callback)(void * userdata, const char *, const StorePath *))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (18)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(221-221)callback(239-239)callback(259-259)nix_derivation_from_json(177-191)nix_derivation_from_json(177-177)nix_add_derivation(193-203)nix_add_derivation(193-193)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_store_drv_from_path(217-234)nix_store_drv_from_path(217-222)nix_derivation_get_outputs(236-253)nix_derivation_get_outputs(236-240)nix_derivation_get_outputs_and_optpaths(255-280)nix_derivation_get_outputs_and_optpaths(255-260)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)name(563-570)name(577-583)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (10)
src/libstore-c/nix_api_store.h (2)
155-155: Const-correctness LGTMUsing
const StorePath *here is correct and matches the implementation.
250-267: API shape LGTM; borrowed lifetime note is goodSignature and const usage look correct.
src/libstore-c/nix_api_store.cc (8)
94-102: Const-correctness LGTMMatches header and underlying API; good.
147-149: BorrowedStorePathpassed to callback; ensure callers don’t retainWe construct a local
StorePathand pass its address. Fine as a borrow, but callers must not store the pointer.Please confirm downstream callbacks don’t capture or retain
outbeyond the call.
167-171: Destructor wrapper LGTMOwnership and behavior are correct.
177-191: JSON -> derivation flow LGTMGood use of
fromJSON, read-only write for path, and invariant checks with proper error handling.
193-204: Store insertion LGTMStraightforward write and return an owned
StorePath.
217-234: Borrowednix_derivationin callback LGTMBorrowing semantics are clear; error handling consistent.
236-253: Outputs iteration LGTMPattern is correct and safe under borrow semantics.
255-279: Outputs + optional paths LGTMNull handling and temp wrappers are sound under borrow semantics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
src/libstore-c/nix_api_store.h (1)
26-29: Fix typedef typo: breaks C callers using nix_derivation_outputHeader uses
nix_derivation_outputin APIs but the typedef declaresnix_derivatio_noutput, so C code will fail to compile withoutstructkeyword. Correct the alias.-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivatio_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
🧹 Nitpick comments (4)
src/libstore-c/nix_api_store.h (4)
192-198: Make input path const for realise API, to match callback and intentThe callback now receives
const StorePath * out(good). The inputpathshould also beconst StorePath *for consistency and to signal no mutation.nix_err nix_store_realise( nix_c_context * context, Store * store, - StorePath * path, + const StorePath * path, void * userdata, void (*callback)(void * userdata, const char * outname, const StorePath * out));
223-231: Clarify ownership of returned StorePathDocument that the returned
StorePath *must be freed by the caller (vianix_store_path_free). Avoids leaks for C consumers./** * @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. + * @note The returned StorePath is owned by the caller and must be freed with nix_store_path_free(). */ StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation);
268-283: Document borrow semantics fornameargument in outputs callbackLike the output object,
nameis only valid during the callback. Add a brief note to prevent misuse./** * @brief Iterate through all of the outputs in a derivation * - * @note The callback borrows the DerivationOutput only for the duration of the call. + * @note The callback borrows the DerivationOutput and the `name` string only for the duration of the call. * * @param[out] context Optional, stores error information * @param[in] drv The derivation * @param[in] callback The function to call on every output * @param[in] userdata Userdata to pass to the callback */
284-301: Explicitly state thatpathmay be NULLThe API name says “optpaths”; make it explicit in the docs that
pathcan beNULL./** * @brief Iterate and get all of the derivation outputs and their store paths. * - * @note The callback borrows the DerivationOutput and StorePath only for the duration of the call. + * @note The callback borrows the DerivationOutput and StorePath only for the duration of the call. + * The StorePath pointer may be NULL if the output has no store path. * * @param[out] context Optional, stores error information * @param[in] drv The derivation * @param[in] store The nix store * @param[in] callback The function to call on every output and store path * @param[in] userdata The userdata to pass to the callback */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/libstore-c/nix_api_store.cc
🧰 Additional context used
🧬 Code graph analysis (1)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (20)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(221-221)callback(239-239)callback(259-259)nix_derivation_from_json(177-191)nix_derivation_from_json(177-177)nix_add_derivation(193-203)nix_add_derivation(193-193)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_store_drv_from_path(217-234)nix_store_drv_from_path(217-222)nix_derivation_get_outputs(236-253)nix_derivation_get_outputs(236-240)nix_derivation_get_outputs_and_optpaths(255-280)nix_derivation_get_outputs_and_optpaths(255-260)nix_derivation_get_structured_attrs(282-295)nix_derivation_get_structured_attrs(282-284)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build_aarch64-darwin / manual
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
🔇 Additional comments (5)
src/libstore-c/nix_api_store.h (5)
155-155: Const-correctness improvement LGTMSwitching
nix_store_is_valid_pathto takeconst StorePath *is correct and consistent with read-only semantics.
238-238: Free function LGTM
nix_derivation_freematches the ownership model introduced by the new constructor.
250-267: API shape LGTM; good borrowing semantics noteThe borrow-lifetime note for
nix_store_drv_from_pathis clear and safe for stack-backed wrapper usage in the implementation.
312-319: Clone API LGTMClear ownership and symmetry with
free.
320-327: Free API LGTMConsistent with other object lifetimes in this header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/libstore-c/nix_api_store.h (1)
26-29: Fix typedef: alias name typo breaks build
nix_derivation_outputfunctions below rely on a proper typedef, but Line 29 definesnix_derivation_noutput. This prevents use of the unqualified name in prototypes and will fail to compile.Apply:
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
🧹 Nitpick comments (5)
src/libstore-c/nix_api_store.h (3)
192-198: Document borrowed lifetime foroutin realise callbackThe callback now receives
const StorePath *. Like other APIs here, the pointer is a borrowed view valid only during the callback, but this isn’t documented fornix_store_realise.Proposed doc tweak:
/** * @brief Realise a Nix store path * * Blocking, calls callback once for each realised output. * * @note When working with expressions, consider using e.g. nix_string_realise to get the output. `.drvPath` may not be * accurate or available in the future. See https://github.com/NixOS/nix/issues/6507 + * + * @note The `out` StorePath passed to the callback is borrowed and only valid + * for the duration of the callback. Clone it if you need to retain it. * * @param[out] context Optional, stores error information
223-231: Const-qualify the derivation parameter
nix_add_derivationdoes not mutatederivation. Preferconst nix_derivation *for API clarity.Header change:
-StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation); +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation);I’ve included the matching .cc change in a later comment.
303-313: Define behavior when structured attrs are absent; fix minor param-name inconsistencyWhen
structuredAttrsis missing, the implementation currently does nothing and still returnsNIX_OK. Please specify behavior (e.g., call back with"null"or return a not-found error) to avoid ambiguity for callers. Also, docs mentionuser_databut the parameter isuserdataelsewhere.I’ve included a concrete .cc diff to emit
"null"below. If preferred, we can document the “no callback on absence” rule instead.src/libstore-c/nix_api_store.cc (2)
193-203: Const-qualifyderivationparameter to mirror header suggestionNo mutation occurs; const is appropriate.
-StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation) +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation)
283-297: Emit explicit value whenstructuredAttrsis absentCurrently returns
NIX_OKbut never invokes the callback if attrs are missing. Consider invoking the callback with"null"to make absence explicit and keep behavior uniform.nix_err nix_derivation_get_structured_attrs( nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata) { if (context) context->last_err_code = NIX_OK; try { - if (auto structuredAttrs = drv->drv.structuredAttrs) { - if (callback) { - auto result = structuredAttrs->structuredAttrs.dump(); - callback(result.data(), result.size(), userdata); - } - } + if (auto structuredAttrs = drv->drv.structuredAttrs) { + if (callback) { + auto result = structuredAttrs->structuredAttrs.dump(); + callback(result.data(), result.size(), userdata); + } + } else if (callback) { + static const char null_json[] = "null"; + callback(null_json, sizeof null_json - 1, userdata); + } } NIXC_CATCH_ERRS }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (24)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(221-221)callback(239-239)callback(259-260)nix_derivation_from_json(177-191)nix_derivation_from_json(177-177)nix_add_derivation(193-203)nix_add_derivation(193-193)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_store_drv_from_path(217-234)nix_store_drv_from_path(217-222)nix_derivation_get_outputs(236-253)nix_derivation_get_outputs(236-240)nix_derivation_get_outputs_and_optpaths(255-281)nix_derivation_get_outputs_and_optpaths(255-261)nix_derivation_get_structured_attrs(283-297)nix_derivation_get_structured_attrs(283-284)nix_derivation_output_clone(299-302)nix_derivation_output_clone(299-299)nix_derivation_output_free(304-307)nix_derivation_output_free(304-304)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (20)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build_aarch64-darwin / manual
- GitHub Check: build_x86_64-linux / test
🔇 Additional comments (14)
src/libstore-c/nix_api_store.h (6)
155-155: Const-correctness on path parameter — looks goodChanging
nix_store_is_valid_pathto takeconst StorePath *is correct and matches usage in the implementation.
214-222: Clarify side effects ofnix_derivation_from_jsonHeader says “Create a nix_derivation from JSON,” but the implementation parses JSON and calls
writeDerivation(read-only mode) andcheckInvariants. Please confirm and document whether this function is guaranteed to be side‑effect free with respect to the store (i.e., does not persist anything) and that insertion should happen only vianix_add_derivation.
250-267: Borrowed-lifetime contract is clear — LGTMThe API notes the derivation is only borrowed during the callback. Matches the implementation pattern.
278-283: Output iteration API — LGTMCallback shape and borrowing semantics look consistent.
295-302: Outputs + optional paths — LGTMGood use of
const Store *and explicit “borrowed” note.
320-328: Clone/free wrappers — LGTMNames and signatures now match the header; simple ownership semantics.
src/libstore-c/nix_api_store.cc (8)
94-102:isValidPathwrapper — LGTMMatches the new
const StorePath *signature and returnsfalseon exceptions viaNIXC_CATCH_ERRS_RES(false).
127-149: Realise callback receivesStorePath *— implementation OKPassing a stack
StorePathto the callback is fine given the synchronous, borrowed-lifetime contract. No change requested.Please ensure downstream callbacks do not stash the
StorePath *beyond the call.
167-171: Destructor — LGTMSimple delete wrapper; no issues.
177-191: Confirmfrom_jsonstore side effectsThis parses JSON, invokes
writeDerivationwith/* read only */ true, then checks invariants. Please confirm that this does not write to the store and is intended purely for validation + wrapping. If so, consider adding a note in the header.
217-234: Derivation-from-path callback — LGTMTemporary wrapper and borrowed-lifetime behavior are correct and documented in the header.
236-253: Outputs iteration — LGTMStraightforward mapping to
drv.outputs; good.
255-281: Outputs + opt paths — LGTMCorrectly handles both present and absent paths.
299-307: Clone/free — LGTMNames now align with the header; ownership is clear.
5f2c94b to
dca4c01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (9)
src/libstore-c/nix_api_store.h (5)
173-174: Makenix_store_real_pathtake a const pathNo mutation; const matches other getters.
-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data); +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data);Companion
.ccchange provided below.
192-198: Clarify lifetime and const‑qualifypathin realise; standardizeuser_dataname
- Path isn’t mutated; mark it
const.- Document that
outonly lives for the callback duration (you pass a pointer to a stack object).- Use
user_dataconsistently across APIs.-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, - void * userdata, - void (*callback)(void * userdata, const char * outname, const StorePath * out)); +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path, + void * user_data, + void (*callback)(void * user_data, const char * outname, const StorePath * out));Add to the doc block above:
- “The
outpointer is only valid for the duration of the callback.”- “
pathmust be a derivation path (.drv). If you intend to realise an output path, use or pass the corresponding .drv.” (Or implement support for both; see.ccnote.)
214-222: Document side effects innix_derivation_from_jsonThis calls
writeDerivation(..., /*read only*/ true)to compute the drv path and thencheckInvariants. Clarify that it does not persist in the store and thatnix_add_derivationperforms the write.Suggest updating the comment:
- * @brief Create a `nix_derivation` from a JSON representation of that derivation. + * @brief Parse a derivation from JSON. + * + * Does not persist to the store; writes in read-only mode only to compute the drv path for invariant checks. + * Use nix_add_derivation() to insert into the store.
230-230: Naming consistency: considernix_store_add_derivationMost store ops are prefixed with
nix_store_*. Consider addingnix_store_add_derivationas the public name (keepnix_add_derivationas an alias if already used).Would you like a follow-up patch that adds
nix_store_add_derivationas a thin wrapper and marksnix_add_derivationfor deprecation in the header comments?
285-291: Rename “derived_path” to “deriver_path” and document lifetimeThe callback receives the path of the deriver, not a “derived” path. Also note the pointer is only valid during the callback (local stack object).
- * @param[in] callback Called for when the path info is resolved + * @param[in] callback Called when the path info is resolved; if present, receives the deriver path (.drv). + * The provided StorePath pointer is valid only for the duration of the callback. ... -nix_err nix_store_query_path_info( +nix_err nix_store_query_path_info( nix_c_context * context, Store * store, const StorePath * store_path, - void * userdata, - void (*callback)(void * userdata, const StorePath * derived_path)); + void * user_data, + void (*callback)(void * user_data, const StorePath * deriver_path));Companion
.ccparam-name alignment optional (names don’t affect ABI).src/libstore-c/nix_api_store.cc (4)
104-112: Makenix_store_real_pathtakeconst StorePath *No mutation; update signature to match header change.
-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data) +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data)
182-196:nix_derivation_from_json: behavior noteReads JSON, computes drv path in read-only mode, checks invariants, returns wrapper. Please mirror this in the header doc to set expectations.
210-220: Makenix_store_copy_closurepath constPath isn’t mutated.
-nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path) +nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, const StorePath * path) @@ - paths.insert(path->path); + paths.insert(path->path);Header change provided above.
241-262: Clarify “deriver path” and lifetime; standardizeuser_dataThe pointer you pass is to a stack
StorePath; add a brief comment and consider renaming the parameter toderiver_path. Also align param name withuser_data.-nix_err nix_store_query_path_info( +nix_err nix_store_query_path_info( nix_c_context * context, Store * store, const StorePath * store_path, - void * userdata, - void (*callback)(void * userdata, const StorePath * derived_path)) + void * user_data, + void (*callback)(void * user_data, const StorePath * deriver_path)) @@ - callback(userdata, &deriver_tmp); + // NOTE: deriver_tmp is stack-allocated; only valid during this callback. + callback(user_data, &deriver_tmp); @@ - callback(userdata, nullptr); + callback(user_data, nullptr);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (29)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(226-226)callback(246-246)callback(267-267)callback(287-288)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_add_derivation(198-208)nix_add_derivation(198-198)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(222-239)nix_store_drv_from_path(222-227)nix_store_query_path_info(241-262)nix_store_query_path_info(241-246)nix_derivation_get_outputs(264-281)nix_derivation_get_outputs(264-268)nix_derivation_get_outputs_and_optpaths(283-309)nix_derivation_get_outputs_and_optpaths(283-289)nix_derivation_to_json(311-323)nix_derivation_to_json(311-312)nix_derivation_output_clone(325-328)nix_derivation_output_clone(325-325)nix_derivation_output_free(330-333)nix_derivation_output_free(330-330)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (18)
src/libstore-c/nix_api_store.h (7)
155-156: LGTM: const-correctness on is_valid_pathThe
const StorePath *parameter aligns with underlying API and avoids accidental mutation.
258-275: LGTM: borrow semantics documentedBorrowed derivation pointer for the duration of the callback is appropriate; matches implementation.
302-307: LGTM: outputs enumeratorBorrowed output wrapper per-callback is safe and mirrors typical pattern.
319-326: LGTM: outputs + optional pathsGood use of
outputsAndOptPaths; borrow semantics are clear.
335-336: LGTM: JSON via callbackMatches the project’s string-callback pattern; avoids dangling pointers.
344-352: LGTM: clone/free ofnix_derivation_outputSignatures now match; simple ownership model is clear.
26-30: Fix typedef typo: breaks public API and CFFI generation
nix_derivation_noutputis a typo; callers expectnix_derivation_output. This will confuse users and break bindings.-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;src/libstore-c/nix_api_store.cc (11)
94-102: LGTM: const-correctis_valid_pathimplementationPass-through to
Store::isValidPath(const StorePath&)is correct with error capture.
156-160: LGTM: callback lifetime forStorePathYou correctly pass a pointer to a stack
StorePathonly during the callback.Please add a one-line comment above the callback to warn consumers not to retain the pointer.
167-170: LGTM:nix_derivation_freeStraightforward delete.
177-181: LGTM:nix_derivation_cloneValue-copy wrapper is fine.
198-208: LGTM:nix_add_derivationWrites to store and returns
StorePath.
222-239: LGTM:nix_store_drv_from_pathBorrow semantics clear and implemented safely.
264-281: LGTM: outputs enumeratorCorrect wrapping and safe callback usage.
283-309: LGTM: outputs + optional pathsGood handling of optional path and borrow semantics.
311-323: LGTM: derivation to JSONLocal
std::stringlifetime is correctly scoped to the callback.
325-333: LGTM: clone/free names now match headerConsistency restored versus earlier mismatch.
127-154: Realise: make path const and clarify .drv vs output-path handlingUse const StorePath*, rename userdata->user_data and pass it to the callback. Either require callers to pass a .drv, or support output paths by using DerivedPath::Opaque and branching on a predicate such as StorePath::isDerivation(). Verification failed (rg returned "nix: No such file or directory"); confirm whether a derivation predicate exists or run the search below.
-nix_err nix_store_realise( +nix_err nix_store_realise( nix_c_context * context, Store * store, - StorePath * path, - void * userdata, - void (*callback)(void * userdata, const char *, const StorePath *)) + const StorePath * path, + void * user_data, + void (*callback)(void * user_data, const char *, const StorePath *)) { @@ - const auto nixStore = store->ptr; - auto results = nixStore->buildPathsWithResults(paths, nix::bmNormal, nixStore); @@ - callback(userdata, outputName.c_str(), &p); + callback(user_data, outputName.c_str(), &p);Run from repo root to verify availability of a derivation predicate:
rg -nP --type=cpp -C2 '\b(isDerivation\s*(|.isDerivation\s*()'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (9)
src/libstore-c/nix_api_store.h (6)
192-198: Document borrowed lifetime and make input path const.
- Clarify the callback gets a borrowed
StorePath*valid only during the call.- Input
pathcan beconst.-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, - void * userdata, - void (*callback)(void * userdata, const char * outname, const StorePath * out)); +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path, + void * userdata, + void (*callback)(void * userdata, const char * outname, const StorePath * out));Add to the docblock: “The
outpointer is borrowed and valid only for the duration of the callback.”
214-221: Add ownership note for return value.State that the returned
nix_derivation*must be freed withnix_derivation_free./** * @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. + * @return owned pointer; free with `nix_derivation_free`. */
233-239: Clarify callback string lifetime (and consider consistent string-callback style).
- Document that both
output_nameandpathare borrowed and valid only during the callback.- Consider switching
pathtonix_get_string_callbackfor consistency with other getters./** * @brief Hashes the derivation and gives the output paths ... - * @param[in] callback Called for every output to provide the output path. + * @param[in] callback Called for every output. The `output_name` and `path` + * pointers are borrowed and valid only during the call.
247-248: Add ownership note for returned StorePath.Mention the caller must
nix_store_path_freethe result.- * @param[in] derivation nix_derivation to insert into the given store. + * @param[in] derivation nix_derivation to insert into the given store. + * @return owned StorePath; free with `nix_store_path_free`.
302-307: Document nullable and borrowed result.
derived_pathcan be null; also clarify borrowed lifetime.- * @param[in] callback Called for when the path info is resolved + * @param[in] callback Called when the path info is resolved. If the path has no + * deriver, `derived_path` is NULL. The pointer is borrowed + * and valid only during the callback.
353-359: Consider using the common string-callback helper.For consistency with other APIs, prefer
call_nix_get_string_callback(see .cc suggestion).src/libstore-c/nix_api_store.cc (3)
127-133: Make input path const to match header suggestion.-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path,
198-221: Avoid passing c_str() of a temporary directly; hold it in a local.Safer and clearer across FFI boundaries; also document borrowing in the header.
- if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); - } + if (callback) { + const auto pathStr = store->ptr->printStorePath(outPath); + callback(userdata, output.first.c_str(), pathStr.c_str()); + }
336-352: Use common helper for string callbacks for consistency.- auto result = drv->drv.toJSON(store->ptr->config).dump(); - if (callback) { - callback(result.data(), result.size(), userdata); - } + auto result = drv->drv.toJSON(store->ptr->config).dump(); + return call_nix_get_string_callback(result, callback, user_data);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (32)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(251-251)callback(271-271)callback(292-292)callback(312-313)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(247-264)nix_store_drv_from_path(247-252)nix_store_query_path_info(266-287)nix_store_query_path_info(266-271)nix_derivation_get_outputs(289-306)nix_derivation_get_outputs(289-293)nix_derivation_get_outputs_and_optpaths(308-334)nix_derivation_get_outputs_and_optpaths(308-314)nix_derivation_to_json(336-352)nix_derivation_to_json(336-341)nix_derivation_output_clone(354-357)nix_derivation_output_clone(354-354)nix_derivation_output_free(359-362)nix_derivation_output_free(359-359)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (11)
src/libstore-c/nix_api_store.h (3)
155-155: Const-correctness on is_valid_path looks good.
366-374: Output clone/free API looks coherent.
26-29: Fix broken typedef: use nix_derivation_output (build-blocker).The forward typedef misspells the public alias; all later prototypes use
nix_derivation_output, which currently isn’t defined, causing compile errors.Apply:
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output; +/* (optional temporary compat) */ +/* typedef struct nix_derivation_output nix_derivation_noutput; */Run to catch any remaining typos:
#!/bin/bash rg -nP '\b(nix_derivation_noutput|nix_derivatio_noutput)\b' -S -g '!**/build/**'src/libstore-c/nix_api_store.cc (8)
94-102: LGTM: const path in is_valid_path and direct passthrough.
146-149: Borrowed StorePath lifetime is limited to the callback.Implementation matches the intended borrowing pattern; ensure the header documents this (see related comment).
182-196: Confirm side effects of writeDerivation(readOnly=true).This calls
writeDerivation(..., /* read only */ true)during JSON parsing. Verify this does not persist anything or mutate store state unexpectedly (to avoid surprising IO beforenix_add_derivation).Would you like me to add an inline comment explaining the rationale once confirmed?
247-264: Borrowed derivation lifetime matches doc; good.Ensure downstream callbacks don’t retain the pointer beyond the call.
266-287: Nullable deriver handling looks correct; add doc note (see header).
289-306: Output iteration wrapper: OK.
308-334: Outputs-and-paths wrapper: OK; clear null-path branch.
354-361: Output clone/free: straightforward.
af1c1fc to
90e65c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (4)
src/libstore-c/nix_api_store.h (2)
192-198: Clarify lifetime of callback StorePath; consider const for input pathThe callback receives
const StorePath * outthat borrows a stack wrapper; document that it’s valid only during the call. Also, thepathinput param can beconst StorePath *.- * @param[in] path Path to build + * @param[in] path Path to build + * @note The `out` StorePath passed to the callback is borrowed and only valid + * for the duration of the callback invocation. @@ - StorePath * path, + const StorePath * path,Matching .cc change shown in its comment.
301-307: Document borrowed StorePath lifetime in callback
derived_pathpoints to a stack wrapper; add the same borrowing note used elsewhere./** * @brief Queries for the nix store path info. * + * @note The `derived_path` argument, when non-null, is borrowed and only valid + * during the callback invocation.src/libstore-c/nix_api_store.cc (2)
127-149: Realise: pass borrowed StorePath; make input path constYou create a stack
StorePathand pass its address—good. Make the inputpathparamconst StorePath *to be fully const-correct.-nix_err nix_store_realise( - nix_c_context * context, - Store * store, - StorePath * path, +nix_err nix_store_realise( + nix_c_context * context, + Store * store, + const StorePath * path,
336-352: to_json: consider using helper for consistencyMinor: other functions use
call_nix_get_string_callback; using it here would unify behavior and return codes.- auto result = drv->drv.toJSON(store->ptr->config).dump(); - if (callback) { - callback(result.data(), result.size(), userdata); - } + auto result = drv->drv.toJSON(store->ptr->config).dump(); + return call_nix_get_string_callback(result, callback, userdata);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.cc (3)
src/libstore/include/nix/store/store-api.hh (26)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)info(535-539)info(762-762)name(563-570)name(577-583)src/libstore/include/nix/store/realisation.hh (1)
outPath(80-83)src/libstore/include/nix/store/store-dir-config.hh (3)
name(83-83)name(85-85)name(91-97)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (32)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(251-251)callback(271-271)callback(292-292)callback(312-313)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(247-264)nix_store_drv_from_path(247-252)nix_store_query_path_info(266-287)nix_store_query_path_info(266-271)nix_derivation_get_outputs(289-306)nix_derivation_get_outputs(289-293)nix_derivation_get_outputs_and_optpaths(308-334)nix_derivation_get_outputs_and_optpaths(308-314)nix_derivation_to_json(336-352)nix_derivation_to_json(336-341)nix_derivation_output_clone(354-357)nix_derivation_output_clone(354-354)nix_derivation_output_free(359-362)nix_derivation_output_free(359-359)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (20)
src/libstore-c/nix_api_store.h (10)
246-246: Add-derivation API shape is goodConsistent with other alloc-returning functions.
254-262: Free/clone API for derivation looks goodSimple and consistent with other wrappers.
285-291: Borrowed derivation lifetime note: goodThe “borrows for duration of the call” contract is clear and matches implementation.
318-323: Output enumeration API looks solidBorrowing semantics are documented; clone/free helpers provided.
335-342: Outputs+optpaths API looks solidConsistent with other borrowed-pointer callbacks.
352-357: JSON serialization via callback is correctMatches the project’s callback pattern for strings.
359-373: Derivation output clone/free look correctNo concerns.
155-155: Approve: const StorePath parameter is safe*Call sites in src/libstore-tests and src/libexpr-tests pass StorePath* (implicit conversion to const is valid) and none mutate the path — no changes required.
26-29: Fix typedef: breaks compilation by aliasing the wrong nameYou forward-declare the struct but typedef it to
nix_derivation_noutputwhile all APIs usenix_derivation_output. This will not compile in C headers using the typedef’d name.Apply:
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output; +/* (optional compat) */ +/* typedef nix_derivation_output nix_derivation_noutput; */
214-221: API addition: from-JSON derivation — transitive nlohmann/json.hpp confirmedInterface is fine.
Verified include chains from src/libstore-c/nix_api_store.cc to <nlohmann/json.hpp>:
- src/libstore-c/nix_api_store.cc -> src/libstore-c/nix_api_store_internal.h -> src/libstore/include/nix/store/derivations.hh -> src/libstore/include/nix/store/parsed-derivations.hh -> <nlohmann/json.hpp>
- src/libstore-c/nix_api_store.cc -> src/libstore/include/nix/store/globals.hh -> src/libstore/include/nix/store/build/derivation-builder.hh -> src/libstore/include/nix/store/derivation-options.hh -> <nlohmann/json.hpp>
No changes required.
src/libstore-c/nix_api_store.cc (10)
94-101: Const StorePath in is_valid_path: goodMatches header and underlying
isValidPathAPI.
167-171: Derivation free: OKStraight delete—fine.
177-181: Derivation clone: OKShallow copy of wrapper—fine.
223-233: add_derivation: OKWrites and returns wrapper path.
247-264: drv_from_path: OK with borrowed wrapperTemporary wrapper and lifetime are documented in header.
266-287: query_path_info: OK; add borrowed-path doc in headerImplementation aligns with suggested doc change.
289-306: get_outputs: OKBorrowed output wrapper; clone/free available.
308-334: get_outputs_and_optpaths: OKBorrowed output and optional path—consistent.
354-361: derivation_output clone/free: OKSimple and correct.
182-196: from_json: add explicit JSON include or confirm PCH; annotate read-only writeDerivation
- src/libstore/pch/precompiled-headers.hh already includes <nlohmann/json.hpp>. Either add #include <nlohmann/json.hpp> to src/libstore-c/nix_api_store.cc (or one of its direct headers) or ensure this translation unit is guaranteed to use that precompiled header — avoid relying on fragile transitive includes.
- Add a one-line comment before the writeDerivation(..., /* read only */ true) call stating that read-only=true is only used to compute drvPath for invariant checking and does not mutate the store.
Also test the APIs we just added.
Use `StorePath *` not `const char *`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (2)
51-55: Fix UB: mkdtemp modifies a const buffer; use std::string::data()Casting c_str() to char* is undefined. Use a mutable buffer.
Apply this diff:
- auto tmpl = nix::absPath(std::filesystem::path(nix::defaultTempDir()) / "tests_nix-store.XXXXXX", true); - nixDir = mkdtemp((char *) tmpl.c_str()); + std::string tmpl = nix::absPath( + std::filesystem::path(nix::defaultTempDir()) / "tests_nix-store.XXXXXX", true); + // mkdtemp mutates tmpl in place + nixDir = mkdtemp(tmpl.data());
45-49: Windows path suffix construction is wrong; use std::to_string(i)std::string{i} creates a 1-char string, not a decimal of i. Generates invalid paths.
- for (size_t i = 0; true; ++i) { - nixDir = tmpl + std::string{i}; + for (size_t i = 0; true; ++i) { + nixDir = tmpl + std::to_string(i); if (std::filesystem::create_directory(nixDir)) break; }
🧹 Nitpick comments (4)
src/libstore-test-support/include/nix/store/tests/test-main.hh (1)
8-10: Tighten the Doxygen summary and clarify return contract.Shorter, clearer, and documents expected return semantics.
-/** - * Call this for a GTest test suite that will including performing Nix - * builds, before running tests. - */ +/** + * Prepare the environment for tests that perform Nix builds. + * Call this before InitGoogleTest(). + * Returns 0 (EXIT_SUCCESS) on success; non-zero on failure. + */src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (1)
11-11: Remove duplicate includeMinor cleanup.
-#include <filesystem>src/libstore-c/nix_api_store.h (1)
173-175: Const-correctness: accept const StorePath in real_path/realiseNeither function mutates the StorePath.
-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data); +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data); @@ -nix_err nix_store_realise( +nix_err nix_store_realise( nix_c_context * context, Store * store, - StorePath * path, + const StorePath * path, void * userdata, void (*callback)(void * userdata, const char * outname, const StorePath * out));Also applies to: 192-199
src/libstore-c/nix_api_store.cc (1)
104-114: Consider const StorePath for real_path*The path isn’t mutated; mirror const-correctness throughout.
-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data) +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
src/libexpr-tests/main.cc(1 hunks)src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)src/libstore-test-support/include/nix/store/tests/meson.build(1 hunks)src/libstore-test-support/include/nix/store/tests/nix_api_store.hh(2 hunks)src/libstore-test-support/include/nix/store/tests/test-main.hh(1 hunks)src/libstore-test-support/meson.build(1 hunks)src/libstore-test-support/test-main.cc(1 hunks)src/libstore-tests/data/derivation/ca/self-contained.json(1 hunks)src/libstore-tests/main.cc(1 hunks)src/libstore-tests/meson.build(1 hunks)src/libstore-tests/nix_api_store.cc(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (8)
- src/libstore-tests/meson.build
- src/libstore-c/nix_api_store_internal.h
- src/libstore-test-support/test-main.cc
- src/libstore-test-support/meson.build
- src/libstore-test-support/include/nix/store/tests/meson.build
- src/libstore-tests/nix_api_store.cc
- src/libstore-tests/main.cc
- src/libstore-tests/data/derivation/ca/self-contained.json
🧰 Additional context used
🧬 Code graph analysis (4)
src/libexpr-tests/main.cc (2)
src/libstore-test-support/include/nix/store/tests/test-main.hh (1)
testMainForBuidingPre(11-11)src/libstore-test-support/test-main.cc (2)
testMainForBuidingPre(10-45)testMainForBuidingPre(10-10)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (33)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(299-299)callback(320-320)callback(340-341)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-315)nix_store_query_path_info(294-299)nix_derivation_get_outputs(317-334)nix_derivation_get_outputs(317-321)nix_derivation_get_outputs_and_optpaths(336-362)nix_derivation_get_outputs_and_optpaths(336-342)nix_derivation_to_json(364-380)nix_derivation_to_json(364-369)nix_derivation_output_clone(382-385)nix_derivation_output_clone(382-382)nix_derivation_output_free(387-390)nix_derivation_output_free(387-387)
src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (1)
src/libstore-c/nix_api_store.cc (6)
nix_libstore_init(13-21)nix_libstore_init(13-13)nix_store_open(33-53)nix_store_open(33-33)nix_store_free(55-58)nix_store_free(55-55)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (8)
src/libexpr-tests/main.cc (2)
3-3: Include looks correct.Header path and namespace usage align with the new helper.
9-11: Fix inverted return check — currently exits on success and skips tests.Return only on non‑zero.
- auto res = testMainForBuidingPre(argc, argv); - if (!res) - return res; + auto res = testMainForBuidingPre(argc, argv); + if (res) + return res;src/libstore-test-support/include/nix/store/tests/nix_api_store.hh (1)
71-73: Use ASSERT_NE instead of EXPECT_NE + assertAvoid mixed non-fatal + hard abort. Use a single fatal assertion.
- EXPECT_NE(store, nullptr) << "Could not open store: " << errMsg; - assert(store); + ASSERT_NE(store, nullptr) << "Could not open store: " << errMsg;src/libstore-c/nix_api_store.cc (3)
94-102: LGTM: const-correct is_valid_pathSwitch to const StorePath* matches store-api.hh and improves safety.
146-149: LGTM: realise now borrows StorePath in callbackConstructing a temporary StorePath per output and borrowing it during the callback aligns with other APIs.
198-217: Avoid dangling C-string in make_outputs; pass StorePath to callbackMatch the header change and borrow a StorePath.
-nix_err nix_derivation_make_outputs( - nix_c_context * context, - Store * store, - const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), - void * userdata) +nix_err nix_derivation_make_outputs( + nix_c_context * context, + Store * store, + const char * json, + void (*callback)(void * userdata, const char * output_name, const StorePath * path), + void * userdata) @@ - if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); - } + if (callback) { + const StorePath tmp_path{outPath}; + callback(userdata, output.first.c_str(), &tmp_path); + }src/libstore-c/nix_api_store.h (2)
232-238: API hazard: callback receives ephemeral C-string; pass StorePath insteadReturning printStorePath(...).c_str() is short-lived and unsafe if retained. Align with other APIs by borrowing a StorePath in the callback.
-nix_err nix_derivation_make_outputs( - nix_c_context * context, - Store * store, - const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), - void * userdata); +nix_err nix_derivation_make_outputs( + nix_c_context * context, + Store * store, + const char * json, + void (*callback)(void * userdata, const char * output_name, const StorePath * path), + void * userdata);Also update the doc comment to note the StorePath is borrowed only for the duration of the callback.
26-29: Typo breaks C API: wrong typedef alias for derivation outputPublic alias must be nix_derivation_output, not nix_derivation_noutput.
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
| if (exists(std::filesystem::path{nixDir})) { | ||
| for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) { | ||
| std::filesystem::permissions(path, std::filesystem::perms::owner_all); | ||
| } | ||
| std::filesystem::remove_all(nixDir); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Destructor can throw and uses unqualified exists(); switch to error_code overloads
Throwing from a destructor risks std::terminate; also qualifies exists().
- if (exists(std::filesystem::path{nixDir})) {
- for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) {
- std::filesystem::permissions(path, std::filesystem::perms::owner_all);
- }
- std::filesystem::remove_all(nixDir);
- }
+ std::error_code ec;
+ if (std::filesystem::exists(std::filesystem::path{nixDir})) {
+ for (const auto & entry : std::filesystem::recursive_directory_iterator(
+ nixDir, std::filesystem::directory_options::skip_permission_denied)) {
+ std::filesystem::permissions(
+ entry.path(),
+ std::filesystem::perms::owner_all,
+ std::filesystem::perm_options::replace,
+ ec);
+ }
+ std::filesystem::remove_all(nixDir, ec);
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (exists(std::filesystem::path{nixDir})) { | |
| for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) { | |
| std::filesystem::permissions(path, std::filesystem::perms::owner_all); | |
| } | |
| std::filesystem::remove_all(nixDir); | |
| } | |
| std::error_code ec; | |
| if (std::filesystem::exists(std::filesystem::path{nixDir})) { | |
| for (const auto & entry : std::filesystem::recursive_directory_iterator( | |
| nixDir, std::filesystem::directory_options::skip_permission_denied)) { | |
| std::filesystem::permissions( | |
| entry.path(), | |
| std::filesystem::perms::owner_all, | |
| std::filesystem::perm_options::replace, | |
| ec); | |
| } | |
| std::filesystem::remove_all(nixDir, ec); | |
| } |
🤖 Prompt for AI Agents
In src/libstore-test-support/include/nix/store/tests/nix_api_store.hh around
lines 26-31, the destructor currently calls unqualified exists() and other
filesystem functions that can throw; change to the std::filesystem::exists
overload that accepts a std::error_code and fully-qualify the call
(std::filesystem::exists), then use the error_code overloads for
recursive_directory_iterator, permissions and remove_all so no exceptions
propagate from the destructor: create a std::error_code ec; check if
(std::filesystem::exists(std::filesystem::path{nixDir}, ec)) and only iterate
using std::filesystem::recursive_directory_iterator(nixDir,
std::filesystem::directory_options::none, ec) (or construct and check ec per
entry), call std::filesystem::permissions(path, perms, ec) and
std::filesystem::remove_all(nixDir, ec); handle or ignore ec values
appropriately to ensure the destructor cannot throw.
| nix_store_free(store); | ||
| } | ||
|
|
||
| Store * store; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Initialize member to avoid deleting indeterminate pointer
store is uninitialized until init_local_store(); initialize to nullptr.
- Store * store;
+ Store * store{nullptr};📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| Store * store; | |
| Store * store{nullptr}; |
🤖 Prompt for AI Agents
In src/libstore-test-support/include/nix/store/tests/nix_api_store.hh around
line 92, the member "Store * store;" is left uninitialized which can lead to
deleting an indeterminate pointer; initialize it to nullptr (or = nullptr) in
the declaration so the pointer has a defined value until init_local_store() sets
it, and update any destructor/reset logic to safely handle the nullptr case if
needed.
| * Call this for a GTest test suite that will including performing Nix | ||
| * builds, before running tests. | ||
| */ | ||
| int testMainForBuidingPre(int argc, char ** argv); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
🧩 Analysis chain
Fix typo in public symbol: “Buiding” → “Building”.
Public API name should be correct; best to rename now while churn is low.
-int testMainForBuidingPre(int argc, char ** argv);
+int testMainForBuildingPre(int argc, char ** argv);Also update all call sites and the definition (e.g., src/libstore-test-support/test-main.cc). To find and verify:
🏁 Script executed:
#!/bin/bash
rg -n 'testMainForBuidingPre|testMainForBuildingPre' -C2Length of output: 1317
Rename public symbol: testMainForBuidingPre → testMainForBuildingPre
Fix typo in public API — rename the declaration, definition and all call sites.
Files to update:
- src/libstore-test-support/include/nix/store/tests/test-main.hh (line 11)
- src/libstore-test-support/test-main.cc (line 10)
- src/libstore-tests/main.cc (line 9)
- src/libexpr-tests/main.cc (line 9)
Diff (header):
-int testMainForBuidingPre(int argc, char ** argv);
+int testMainForBuildingPre(int argc, char ** argv);
🤖 Prompt for AI Agents
In src/libstore-test-support/include/nix/store/tests/test-main.hh around line
11, the public function name has a typo; rename the declaration from
testMainForBuidingPre to testMainForBuildingPre and update the matching
definition and all call sites accordingly: change the definition in
src/libstore-test-support/test-main.cc (line ~10) and update callers in
src/libstore-tests/main.cc (line ~9) and src/libexpr-tests/main.cc (line ~9) so
they use testMainForBuildingPre(int argc, char **argv); ensure signatures match
exactly and rebuild to verify all references are fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
src/libstore-c/nix_api_store.h (2)
325-351: Clarify the callback parameter semantics.The documentation states that "Path and result for the callback only exist for the lifetime of the call" but doesn't specify what format the path parameter takes. Based on the implementation, it appears to be a string representation of the path.
/** * @brief Builds the paths, if they are a derivation then they get built. * - * @note Path and result for the callback only exist for the lifetime of - * the call. Result is a string containing the build result in JSON. + * @note Path (string) and result (JSON string) for the callback only exist for + * the lifetime of the call. * * @param[out] context Optional, stores error information * @param[in] store nix store reference * @param[in] store_paths Pointer to list of nix store paths * @param[in] num_store_paths Number of nix store paths * @param[in] callback The callback to trigger for build results * @param[in] userdata User data to pass to the callback + * @return error code, NIX_OK on success. */
397-402: Fix parameter name mismatch in function declaration.The parameter is declared as
userdatabut the documentation refers to it asuser_data. This inconsistency should be resolved.nix_err nix_derivation_to_json( nix_c_context * context, const nix_derivation * drv, const Store * store, nix_get_string_callback callback, - void * userdata); + void * user_data);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (36)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(299-299)callback(322-322)callback(349-349)callback(369-370)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-315)nix_store_query_path_info(294-299)nix_store_build_paths(317-344)nix_store_build_paths(317-323)nix_derivation_get_outputs(346-363)nix_derivation_get_outputs(346-350)nix_derivation_get_outputs_and_optpaths(365-391)nix_derivation_get_outputs_and_optpaths(365-371)nix_derivation_to_json(393-409)nix_derivation_to_json(393-398)nix_derivation_output_clone(411-414)nix_derivation_output_clone(411-411)nix_derivation_output_free(416-419)nix_derivation_output_free(416-416)
src/libstore-c/nix_api_store.cc (2)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)src/libstore/include/nix/store/realisation.hh (1)
outPath(80-83)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (17)
src/libstore-c/nix_api_store.h (2)
29-29: Fix the typedef name.The typedef has a typo - it should be
nix_derivation_outputnotnix_derivatio_noutput. This is inconsistent with how the type is used throughout the rest of the file.Apply this diff to fix the typedef:
-typedef struct nix_derivation_output nix_derivatio_noutput; +typedef struct nix_derivation_output nix_derivation_output;
232-237: Address existing feedback: use StorePath instead of const char * for path parameter.Switch to passing a borrowed
StorePathto the callback (consistent withnix_store_realise) to prevent accidental retention of a dangling C-string.- void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path),src/libstore-c/nix_api_store.cc (15)
94-94: LGTM!The parameter is now correctly declared as
const StorePath * path, which improves const-correctness and aligns with the function's read-only nature.
132-132: LGTM!The callback signature has been updated to use
const StorePath *instead ofconst char *, which provides better type safety and consistency with other APIs.
147-148: LGTM!The implementation correctly creates a temporary
StorePathobject and passes its address to the callback, ensuring the callback receives a valid pointer for the duration of the call.
167-170: LGTM!Simple and correct deallocation function for derivation wrappers.
177-180: LGTM!Correct implementation of derivation cloning using the copy constructor.
182-196: LGTM!The implementation correctly parses JSON, writes the derivation to the store, validates invariants, and follows the established error handling pattern.
198-221: Address existing feedback: use StorePath instead of const char * for path parameter.Switch to passing a borrowed
StorePathto the callback (consistent withnix_store_realise) to prevent accidental retention of a dangling C-string.Apply this diff to match the corrected header signature:
- void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), @@ - if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); - } + if (callback) { + const StorePath tmp_path{outPath}; + callback(userdata, output.first.c_str(), &tmp_path); + }
223-233: LGTM!The function correctly writes the derivation to the store and returns a wrapped StorePath. The error handling follows the established pattern.
275-292: LGTM!The implementation correctly retrieves a derivation from a path and provides it via callback using a temporary wrapper. The callback receives a valid pointer for the duration of the call.
294-315: LGTM!The implementation correctly queries path info and handles the optional deriver by passing either a valid StorePath or nullptr to the callback.
346-363: LGTM!The implementation correctly iterates over derivation outputs and provides each via callback using temporary wrappers.
365-391: LGTM!The implementation correctly handles optional paths by passing either a valid StorePath or nullptr to the callback, maintaining consistency with other optional parameter patterns in the API.
411-414: LGTM!Correct implementation of derivation output cloning.
416-419: LGTM!Correct implementation of derivation output deallocation.
317-344: Avoid per-result JSON serialization in the callback loop (src/libstore-c/nix_api_store.cc:317-344)
- Serializing with json.dump() for every result is potentially expensive for large result sets — batch results into a single JSON array or reuse/pre-allocate a buffer and reduce the number of dumps/callback calls.
- Also verify string lifetime: passing .c_str() from temporaries (result.path.to_string(...).c_str() and json.dump().c_str()) is only safe if the callback copies the strings synchronously; if the callback may retain them, allocate persistent strings or change the API to accept/own std::string.
| nix_err nix_derivation_to_json( | ||
| nix_c_context * context, | ||
| const nix_derivation * drv, | ||
| const Store * store, | ||
| nix_get_string_callback callback, | ||
| void * userdata) | ||
| { | ||
| if (context) | ||
| context->last_err_code = NIX_OK; | ||
| try { | ||
| auto result = drv->drv.toJSON(store->ptr->config).dump(); | ||
| if (callback) { | ||
| callback(result.data(), result.size(), userdata); | ||
| } | ||
| } | ||
| NIXC_CATCH_ERRS | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick
Fix parameter name to match header declaration.
The parameter should be named user_data to match the header declaration, not userdata.
nix_err nix_derivation_to_json(
nix_c_context * context,
const nix_derivation * drv,
const Store * store,
nix_get_string_callback callback,
- void * userdata)
+ void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
try {
auto result = drv->drv.toJSON(store->ptr->config).dump();
if (callback) {
- callback(result.data(), result.size(), userdata);
+ callback(result.data(), result.size(), user_data);
}
}
NIXC_CATCH_ERRS
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| nix_err nix_derivation_to_json( | |
| nix_c_context * context, | |
| const nix_derivation * drv, | |
| const Store * store, | |
| nix_get_string_callback callback, | |
| void * userdata) | |
| { | |
| if (context) | |
| context->last_err_code = NIX_OK; | |
| try { | |
| auto result = drv->drv.toJSON(store->ptr->config).dump(); | |
| if (callback) { | |
| callback(result.data(), result.size(), userdata); | |
| } | |
| } | |
| NIXC_CATCH_ERRS | |
| } | |
| nix_err nix_derivation_to_json( | |
| nix_c_context * context, | |
| const nix_derivation * drv, | |
| const Store * store, | |
| nix_get_string_callback callback, | |
| void * user_data) | |
| { | |
| if (context) | |
| context->last_err_code = NIX_OK; | |
| try { | |
| auto result = drv->drv.toJSON(store->ptr->config).dump(); | |
| if (callback) { | |
| callback(result.data(), result.size(), user_data); | |
| } | |
| } | |
| NIXC_CATCH_ERRS | |
| } |
🤖 Prompt for AI Agents
In src/libstore-c/nix_api_store.cc around lines 393 to 409, the function
parameter is named "userdata" but the header declares "user_data"; rename the
parameter to "user_data" in the nix_derivation_to_json signature and update all
uses inside the function (e.g., the callback invocation) to pass user_data
instead of userdata so the implementation matches the header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
src/libstore-c/nix_api_store.h (4)
316-331: Doc polish: rename param tostore_pathand note possible NULL deriver.Align param name with signature and clarify callback may receive NULL when there’s no deriver.
Apply:
/** * @brief Queries for the nix store path info. * * @param[out] context Optional, stores error information * @param[in] store nix store reference - * @param[in] path A store path + * @param[in] store_path A store path * @param[in] userdata The data to pass to the callback - * @param[in] callback Called for when the path info is resolved + * @param[in] callback Called when the path info is resolved; derived_path may be NULL if there is no deriver */
345-352: Optional: consider passingStorePath *forpathfor consistency.
build_pathscurrently passes strings; other new callbacks useStorePath *. Consider switching for consistency and to avoid string lifetimes, or keep as-is since the doc notes the lifetime.- void (*callback)(void * userdata, const char * path, const char * result), + void (*callback)(void * userdata, const StorePath * path, const char * result),
239-247: Take derivation by const pointer in nix_add_derivationderivation isn't mutated — change the parameter to const in both the header and the implementation.
--- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation); +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation);--- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation) +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation)
192-198: Const-qualify the inputpathinnix_store_realise(header + implementation)path is not modified; change the parameter to
const StorePath * pathin both src/libstore-c/nix_api_store.h and src/libstore-c/nix_api_store.cc (call sites need no changes).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (36)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(299-299)callback(322-322)callback(349-349)callback(369-370)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-315)nix_store_query_path_info(294-299)nix_store_build_paths(317-344)nix_store_build_paths(317-323)nix_derivation_get_outputs(346-363)nix_derivation_get_outputs(346-350)nix_derivation_get_outputs_and_optpaths(365-391)nix_derivation_get_outputs_and_optpaths(365-371)nix_derivation_to_json(393-409)nix_derivation_to_json(393-398)nix_derivation_output_clone(411-414)nix_derivation_output_clone(411-411)nix_derivation_output_free(416-419)nix_derivation_output_free(416-416)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (11)
src/libstore-c/nix_api_store.h (11)
155-155: LGTM: const-correctis_valid_pathparameter.
214-222: LGTM:nix_derivation_from_jsonAPI and docs.
254-254: LGTM: destructor fornix_derivation.
262-262: LGTM: clone semantics fornix_derivation.
298-315: LGTM: doc style and borrow semantics fornix_store_drv_from_path.
353-368: LGTM: outputs iterator borrows output objects.
369-387: LGTM: outputs+optpaths iterator with borrow semantics.
388-403: LGTM: callback-based JSON getter avoids dangling pointers.
410-418: LGTM: clone/free fornix_derivation_output.
232-238: Avoid ephemeral C-strings in callback; passStorePath *instead.Currently header exposes
const char * path, but implementation passesprintStorePath(...).c_str()from a temporary. Switch to borrowing aStorePathlike other APIs.Apply:
nix_err nix_derivation_make_outputs( nix_c_context * context, Store * store, const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), void * userdata);Note: Implementation needs to wrap
outPathin aStorePath tmp{outPath}; callback(userdata, name, &tmp);.
26-29: Fix typedef alias: should benix_derivation_output(compile error).The alias name is misspelled; prototypes below use
nix_derivation_outputas a typedef, so this breaks compilation.Apply:
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (3)
src/libstore-c/nix_api_store.h (2)
298-308: Doc: add @return for error code.Add return documentation for consistency with other APIs.
/** * @brief Returns the derivation associated with the store path * * @note The callback borrows the Derivation only for the duration of the call. * * @param[out] context Optional, stores error information * @param[in] store The nix store * @param[in] path The nix store path * @param[in] callback The callback to call * @param[in] userdata The userdata to pass to the callback + * @return error code, NIX_OK on success. */
173-174: Const-correctness: make path parameter const.The function does not mutate
path; preferconst StorePath *(would require matching .cc change).-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data); +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data);src/libstore-c/nix_api_store.cc (1)
104-106: Const-correctness follow-up (if header changes): real_path arg can be const.If you adopt the header const change, mirror it here.
-nix_err nix_store_real_path( - nix_c_context * context, Store * store, StorePath * path, nix_get_string_callback callback, void * user_data) +nix_err nix_store_real_path( + nix_c_context * context, Store * store, const StorePath * path, nix_get_string_callback callback, void * user_data)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/libstore-c/nix_api_store_internal.h
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (36)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(299-299)callback(322-322)callback(349-349)callback(369-370)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-315)nix_store_query_path_info(294-299)nix_store_build_paths(317-344)nix_store_build_paths(317-323)nix_derivation_get_outputs(346-363)nix_derivation_get_outputs(346-350)nix_derivation_get_outputs_and_optpaths(365-391)nix_derivation_get_outputs_and_optpaths(365-371)nix_derivation_to_json(393-405)nix_derivation_to_json(393-394)nix_derivation_output_clone(407-410)nix_derivation_output_clone(407-407)nix_derivation_output_free(412-415)nix_derivation_output_free(412-412)
src/libstore-c/nix_api_store.cc (1)
src/libstore/include/nix/store/store-api.hh (26)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)output(620-623)output(620-620)output(625-628)output(625-625)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (23)
src/libstore-c/nix_api_store.h (13)
155-155: LGTM: const-correctness for is_valid_path.Signature updated to
const StorePath *. Matches implementation and upstream API expectations.
192-198: LGTM: realise callback now returns StorePath wrapper.Avoids passing ephemeral C-strings; consistent with other APIs.
246-246: LGTM: add_derivation returns StorePath wrapper.Matches implementation and ownership model.
254-254: LGTM: derivation_free API.
262-262: Confirm clone semantics preserve store pointer.Header is fine; ensure implementation copies the associated Store so downstream calls (e.g., to_json) don’t deref null store.
325-331: LGTM: query_path_info borrows StorePath in callback.
345-352: LGTM: build_paths API shape and lifetime note.
363-368: LGTM: get_outputs API borrows output wrapper.
380-387: LGTM: get_outputs_and_optpaths returns optional StorePath.
396-397: LGTM: derivation_to_json via string-callback.
405-413: LGTM: derivation_output clone/free APIs.
26-29: Blocker: typedef typo breaks API (unknown type ‘nix_derivation_output’).Forward-declares the wrong alias (
nix_derivation_noutput) while the API usesnix_derivation_output*. This will not compile.Apply:
-/** @brief Nix Derivation Output */ -typedef struct nix_derivation_output nix_derivation_noutput; +/** @brief Nix Derivation Output */ +typedef struct nix_derivation_output nix_derivation_output;
232-238: Avoid ephemeral C-string; pass StorePath in callback.Current signature exposes
const char * paththat’s built from a temporary; switch to borrowedconst StorePath *like other APIs to prevent dangling pointers.Proposed header change:
-nix_err nix_derivation_make_outputs( - nix_c_context * context, - Store * store, - const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), - void * userdata); +nix_err nix_derivation_make_outputs( + nix_c_context * context, + Store * store, + const char * json, + void (*callback)(void * userdata, const char * output_name, const StorePath * path), + void * userdata);I’ve included the matching .cc fix in that file’s comment.
src/libstore-c/nix_api_store.cc (10)
94-102: LGTM: is_valid_path matches new const StorePath * signature.
132-149: LGTM: realise now passes StorePath wrapper to callback.Borrowed
StorePathon the stack is fine for callback-scoped use.
182-196: LGTM: from_json writes/validates, wraps with store.
223-233: LGTM: add_derivation writes and returns StorePath wrapper.
294-315: LGTM: query_path_info borrows optional deriver path.
317-344: LGTM with note: JSON/result strings are callback-scoped.Header documents lifetime; usage aligns.
Please confirm downstream callbacks don’t retain these pointers after return.
346-363: LGTM: get_outputs borrows per-output wrapper.
365-391: LGTM: get_outputs_and_optpaths borrows output/path, handles null.
393-405: LGTM: derivation_to_json returns via string-callback.
198-221: Avoid dangling C-string in make_outputs; return StorePath.
printStorePath(...).c_str()points to a temporary. Align with other APIs and pass a borrowedStorePath.-nix_err nix_derivation_make_outputs( +nix_err nix_derivation_make_outputs( nix_c_context * context, Store * store, const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), void * userdata) { @@ - for (auto & output : drv.outputs) { + for (auto & output : drv.outputs) { nix::Hash h = hashesModulo.hashes.at(output.first); auto outPath = store->ptr->makeOutputPath(output.first, h, drv.name); if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); + const StorePath tmp_path{outPath}; + callback(userdata, output.first.c_str(), &tmp_path); } } }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (5)
src/libstore-c/nix_api_store.h (5)
192-198: Realise callback now returns StorePath — clarify borrow/lifetime in docs.*Please add a brief note that the StorePath pointer is only valid during the callback and that outname is only valid for the duration of the call.
214-221: Creation from JSON LGTM; document ownership.Consider adding “Free with nix_derivation_free” to the docblock to make ownership explicit.
239-247: Make derivation param const and document return ownership.The function doesn’t mutate the derivation; mark it const and note the caller must free the returned StorePath.
Header:
-StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation); +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation);Implementation (src/libstore-c/nix_api_store.cc, Lines 222-232):
-StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation) +StorePath * nix_add_derivation(nix_c_context * context, Store * store, const nix_derivation * derivation)
316-331: Clarify nullability and standardize parameter order.
- Document that derived_path may be NULL if there is no deriver (matches implementation).
- For API consistency with most functions here, consider using (callback, user_data) ordering.
Header:
- * @param[in] userdata The data to pass to the callback - * @param[in] callback Called for when the path info is resolved + * @param[in] callback Called when the path info is resolved. The derived_path can be NULL if absent. + * @param[in] userdata The data to pass to the callback @@ -nix_err nix_store_query_path_info( +nix_err nix_store_query_path_info( @@ - void * userdata, - void (*callback)(void * userdata, const StorePath * derived_path)); + void (*callback)(void * userdata, const StorePath * derived_path), + void * userdata);Implementation (src/libstore-c/nix_api_store.cc, Lines 293-314):
-nix_err nix_store_query_path_info( +nix_err nix_store_query_path_info( @@ - void * userdata, - void (*callback)(void * userdata, const StorePath * derived_path)) + void (*callback)(void * userdata, const StorePath * derived_path), + void * userdata)
332-352: Prefer passing StorePath for ‘path’ to avoid ephemeral strings.*Current callback receives const char * path built via to_string(...).c_str(), whose lifetime ends after the call; while documented, this is easy to misuse. Passing a StorePath pointer aligns with other APIs and is safer.
Header:
- * @param[in] callback The callback to trigger for build results + * @param[in] callback The callback to trigger for build results. The path is borrowed and only valid during the call. @@ - void (*callback)(void * userdata, const char * path, const char * result), + void (*callback)(void * userdata, const StorePath * path, const char * result),Implementation (src/libstore-c/nix_api_store.cc, Lines 316-343):
- callback(userdata, result.path.to_string(store->ptr->config).c_str(), json.dump().c_str()); + const StorePath tmp{result.path}; + callback(userdata, &tmp, json.dump().c_str());
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/libstore-c/nix_api_store.h(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (36)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(299-299)callback(322-322)callback(349-349)callback(369-370)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-315)nix_store_query_path_info(294-299)nix_store_build_paths(317-344)nix_store_build_paths(317-323)nix_derivation_get_outputs(346-363)nix_derivation_get_outputs(346-350)nix_derivation_get_outputs_and_optpaths(365-391)nix_derivation_get_outputs_and_optpaths(365-371)nix_derivation_to_json(393-405)nix_derivation_to_json(393-394)nix_derivation_output_clone(407-410)nix_derivation_output_clone(407-407)nix_derivation_output_free(412-415)nix_derivation_output_free(412-412)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build_aarch64-darwin / manual
- GitHub Check: build_aarch64-darwin / test
- GitHub Check: build_x86_64-linux / test
🔇 Additional comments (7)
src/libstore-c/nix_api_store.h (7)
26-29: Typedefs LGTM; earlier typo fixed.The public aliases look correct and consistent.
155-155: Const-correctness LGTM.Signature matches the implementation and improves safety.
353-368: Outputs enumeration LGTM.Borrowing semantics are documented; implementation constructs a temporary per-callback as expected.
369-387: Outputs+optpaths LGTM.Nullability of path is handled and documented; borrowing semantics are clear.
388-398: Callback-style JSON getter LGTM; resolves dangling return risk.This replaces the unsafe const char * return with a safe callback pattern.
399-413: Derivation output clone/free LGTM.Ownership semantics are clear and consistent with other wrappers.
223-238: Avoid ephemeral C-strings in callback; pass StorePath instead.The implementation calls callback with printStorePath(...).c_str(), which is a temporary. Switch the callback to accept a StorePath pointer to avoid dangling pointers and align with other APIs.
Apply this header diff:
- * @param[in] callback Called for every output to provide the output path. + * @param[in] callback Called for every output to provide the output path (borrowed; valid only during the call). @@ -nix_err nix_derivation_make_outputs( +nix_err nix_derivation_make_outputs( @@ - void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path),And update the implementation (src/libstore-c/nix_api_store.cc, Lines 197-220):
- void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), @@ - if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); - } + if (callback) { + const StorePath tmp{outPath}; + callback(userdata, output.first.c_str(), &tmp); + }
e6d6dfd to
284a700
Compare
284a700 to
b2fff15
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (6)
src/libstore-c/nix_api_store_internal.h (1)
16-25: Clarify ownership/borrowing of Store in nix_derivation.
Callers must ensure Store outlives any nix_derivation instances; add a brief note to prevent UAFs.Apply this diff to annotate intent:
struct nix_derivation { nix::Derivation drv; - Store * store; + // Borrowed; must outlive this wrapper. + Store * store; };src/libstore-c/nix_api_store.h (3)
192-198: Document borrowed lifetime and make input path const in realise.
The callback receives a pointer to a stack-allocated StorePath; document borrowing. Also make the input path const./** * @brief Realise a Nix store path * * Blocking, calls callback once for each realised output. * + * @note The callback borrows the output StorePath only for the duration of the call. + * * @note When working with expressions, consider using e.g. nix_string_realise to get the output. `.drvPath` may not be * accurate or available in the future. See https://github.com/NixOS/nix/issues/6507 * * @param[out] context Optional, stores error information * @param[in] store Nix Store reference * @param[in] path Path to build * @param[in] userdata data to pass to every callback invocation * @param[in] callback called for every realised output */ nix_err nix_store_realise( nix_c_context * context, Store * store, - StorePath * path, + const StorePath * path, void * userdata, void (*callback)(void * userdata, const char * outname, const StorePath * out));Also applies to: 179-191
298-315: Doc: callback borrows both derivation and its Store.
Header says derivation is borrowed; explicitly mention the associated Store is borrowed as well to prevent misuse.- * @note The callback borrows the Derivation only for the duration of the call. + * @note The callback borrows the Derivation and its associated Store only for the duration of the call.
325-331: Standardize parameter order and fix doc param name in query_path_info.
Other APIs use (callback, user_data). Also the doc uses “path” but the parameter is “store_path”./** * @brief Queries for the nix store path info. * * @param[out] context Optional, stores error information * @param[in] store nix store reference - * @param[in] path A store path - * @param[in] userdata The data to pass to the callback - * @param[in] callback Called for when the path info is resolved + * @param[in] store_path A store path + * @param[in] callback Called for when the path info is resolved + * @param[in] user_data The data to pass to the callback */ nix_err nix_store_query_path_info( nix_c_context * context, Store * store, const StorePath * store_path, - void * userdata, - nix_get_string_callback callback); + nix_get_string_callback callback, + void * user_data);Matching .cc fix provided in nix_api_store.cc comment.
src/libstore-c/nix_api_store.cc (2)
132-133: Realise: pass borrowed StorePath; also make input path const.
The callback receives a stack-allocated StorePath; safe if callers don’t retain it. Align the function signature with const input.-nix_err nix_store_realise( +nix_err nix_store_realise( nix_c_context * context, Store * store, - StorePath * path, + const StorePath * path, void * userdata, void (*callback)(void * userdata, const char *, const StorePath *)) @@ - for (const auto & [outputName, realisation] : result.builtOutputs) { - StorePath p{realisation.outPath}; - callback(userdata, outputName.c_str(), &p); + for (const auto & [outputName, realisation] : result.builtOutputs) { + const StorePath p{realisation.outPath}; + callback(userdata, outputName.c_str(), &p); }Also applies to: 147-149
294-311: Standardize parameter order for query_path_info (callback before user_data).
Matches the majority of APIs and reduces cognitive overhead.-nix_err nix_store_query_path_info( - nix_c_context * context, - Store * store, - const StorePath * store_path, - void * userdata, - nix_get_string_callback callback) +nix_err nix_store_query_path_info( + nix_c_context * context, + Store * store, + const StorePath * store_path, + nix_get_string_callback callback, + void * user_data) { @@ - if (callback) { + if (callback) { auto result = info->toJSON(store->ptr->config, true, nix::HashFormat::Nix32).dump(); - callback(result.data(), result.size(), userdata); + callback(result.data(), result.size(), user_data); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)
🔇 Additional comments (9)
src/libstore-c/nix_api_store_internal.h (1)
4-4: LGTM: necessary include for derivation types.
Brings in nix::Derivation and nix::DerivationOutput; required for the new wrappers.src/libstore-c/nix_api_store.h (5)
26-29: LGTM: public derivation wrapper typedefs.
Consistent with the internal wrappers.
155-155: LGTM: const-correctness for is_valid_path.
Prevents accidental mutation.
246-263: LGTM: add/clone/free for derivations.
Clone preserves the Store pointer (per implementation); good.
345-352: LGTM: outputs iteration, JSON serialization, and output lifetimes.
Borrowing semantics are documented; API surface is coherent.Also applies to: 363-387, 396-413
214-238: Avoid dangling C-strings in make_outputs; pass StorePath instead.
Current signature exposes a transient C-string from printStorePath; align with other APIs that pass StorePath.nix_err nix_derivation_make_outputs( nix_c_context * context, Store * store, const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), void * userdata);Matching .cc fix provided in nix_api_store.cc comment.
src/libstore-c/nix_api_store.cc (3)
94-102: LGTM: const-correct is_valid_path.
Straight passthrough to nix::Store::isValidPath.
167-170: LGTM: derivation lifecycle, path/build helpers, and JSON I/O.
Implementations are consistent with the header and follow the borrowing pattern.Also applies to: 177-181, 182-196, 223-233, 275-292, 313-340, 342-359, 361-387, 389-401, 403-411
198-221: Fix: avoid dangling C-strings in make_outputs; pass StorePath instead.
printStorePath(...).c_str()is a temporary. Mirror realise/closure patterns by passing a borrowed StorePath.-nix_err nix_derivation_make_outputs( - nix_c_context * context, - Store * store, - const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), - void * userdata) +nix_err nix_derivation_make_outputs( + nix_c_context * context, + Store * store, + const char * json, + void (*callback)(void * userdata, const char * output_name, const StorePath * path), + void * userdata) { @@ - for (auto & output : drv.outputs) { + for (auto & output : drv.outputs) { nix::Hash h = hashesModulo.hashes.at(output.first); auto outPath = store->ptr->makeOutputPath(output.first, h, drv.name); if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); + const StorePath tmp_path{outPath}; + callback(userdata, output.first.c_str(), &tmp_path); } }Update header declaration accordingly (see nix_api_store.h comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
♻️ Duplicate comments (2)
src/libstore-c/nix_api_store.h (1)
230-236: Critical: callback path must not be a temporary C-stringChange callback to take a borrowed StorePath (consistent with other APIs) to avoid dangling pointers.
Header diff (see matching .cc change in its comment):
- void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path),src/libstore-c/nix_api_store.cc (1)
198-221: Bug: callback receives pointer to temporary C-string (use StorePath or string-callback)printStorePath(...).c_str() returns a pointer to a temporary; callers might retain it, leading to UAF. Align with other APIs by passing a borrowed StorePath.
Apply these changes (header and impl):
In src/libstore-c/nix_api_store.h:
- void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path),In src/libstore-c/nix_api_store.cc:
- if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); - } + if (callback) { + const StorePath tmp_path{outPath}; + callback(userdata, output.first.c_str(), &tmp_path); + }
🧹 Nitpick comments (10)
src/libstore-c/nix_api_store_internal.h (1)
16-20: nix_derivation holds a raw Store — clarify lifetime or hold a strong ref*Raw pointer risks dangling if Store is freed before the derivation. Either:
- Document that nix_derivation borrows Store and Store must outlive it (minimal), or
- Store a nix::refnix::Store in nix_derivation to enforce lifetime.
Example (if choosing strong ref):
-struct nix_derivation -{ - nix::Derivation drv; - Store * store; -}; +struct nix_derivation +{ + nix::Derivation drv; + // Prefer strong reference to avoid dangling pointers. + nix::ref<nix::Store> store_ref; +};If you keep Store*, please add explicit borrow semantics in the public header near the typedef and constructors.
src/libstore-c/nix_api_store.cc (2)
147-149: Borrowed StorePath in realise callback — add header notePassing a pointer to a stack-allocated StorePath is fine if the header documents that the pointer is only valid during the callback. Please add that note to nix_store_realise in the header for clarity.
313-340: build_paths: temporaries passed as const char; prefer StorePath/string-callback*Even with the doc note, passing result.path.to_string(...).c_str() and json.dump().c_str() risks misuse. Consider:
- Passing a StorePath for the path, and
- Delivering the JSON via nix_get_string_callback.
Example:
- void (*callback)(void * userdata, const char * path, const char * result), + void (*callback)(void * userdata, const StorePath * path, const char * result), @@ - callback(userdata, result.path.to_string(store->ptr->config).c_str(), json.dump().c_str()); + const StorePath tmp{result.path}; + callback(userdata, &tmp, json.dump().c_str());src/libstore-c/nix_api_store.h (7)
26-27: Add borrow-lifetime note for nix_derivationPlease document that nix_derivation borrows its Store; the Store must outlive the derivation instance, unless you switch to holding a strong reference internally.
Proposed doc:
/** @brief Nix Derivation */ typedef struct nix_derivation nix_derivation; +/** @note A nix_derivation borrows its Store; ensure the Store outlives the derivation. */
190-196: Realise callback lifetime notePlease add “The out StorePath pointer is only valid for the duration of the callback” to match actual usage in the implementation.
Suggested doc tweak:
- * @param[in] callback called for every realised output + * @param[in] callback called for every realised output. The passed StorePath is borrowed + * and valid only for the duration of the callback.
212-220: from_json: clarify Store ownership semanticsDocument that the returned nix_derivation borrows the Store passed in; the Store must remain alive until nix_derivation_free.
244-245: Add note about freeing returned StorePathMention that the returned StorePath must be freed with nix_store_path_free.
- * @param[in] derivation nix_derivation to insert into the given store. + * @param[in] derivation nix_derivation to insert into the given store. + * @return owned StorePath of the derivation; free with nix_store_path_free().
252-252: Doc nit: mismatched quoteUse consistent quoting in the brief.
- * @brief Deallocate a `nix_derivation' + * @brief Deallocate a `nix_derivation`
343-350: build_paths: consider safer callback typesEven with the note, prefer passing a StorePath for the path or using nix_get_string_callback to eliminate temporary C-string risks.
378-379: to_json: add note about Store lifetimeSince implementation uses drv->store->ptr->config, document that drv must have an associated Store that is still alive.
- * @param[in] drv The derivation + * @param[in] drv The derivation (must be associated with a live Store)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (28)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(318-318)callback(346-347)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-311)nix_store_query_path_info(294-299)nix_store_build_paths(313-340)nix_store_build_paths(313-319)nix_derivation_get_outputs_and_optpaths(342-366)nix_derivation_get_outputs_and_optpaths(342-348)nix_derivation_to_json(368-380)nix_derivation_to_json(368-369)
src/libstore-c/nix_api_store.cc (2)
src/libstore/include/nix/store/store-api.hh (22)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)src/libstore/include/nix/store/realisation.hh (1)
outPath(80-83)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (13)
src/libstore-c/nix_api_store_internal.h (1)
4-4: Include looks correctIncluding nix/store/derivations.hh here is appropriate for nix::Derivation usage.
src/libstore-c/nix_api_store.cc (9)
94-102: Const-correct path checkChanging to const StorePath * is right and consistent with usage.
167-171: Derivation freeDestructor is correct.
177-181: Derivation clone preserves Store pointerGood fix preserving the associated store pointer. Please ensure header docs mention that the cloned object still borrows the same Store lifetime.
182-196: from_json: invariants check is goodParsing, computing drvPath read-only, and checkInvariants look correct.
223-233: nix_add_derivation OKWrites derivation and returns StorePath wrapper; looks good.
275-292: drv_from_path wrapperConstructing a temporary nix_derivation with matching store is correct; borrowed for callback duration.
294-312: query_path_info JSON via callbackLooks correct and consistent.
342-366: outputs_and_optpathsAPI shape and borrowed StorePath usage look good.
368-380: to_json via drv->store configOK, consistent with wrapper design.
src/libstore-c/nix_api_store.h (3)
153-154: Const-correctness improvementGood change to const StorePath *.
260-260: Clone API fineSignature is fine; ensure callers understand it borrows the same Store as the source.
296-313: Doc style already fixed; looks goodDoxygen-style block and params are present.
0d30905 to
ad566a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (2)
src/libstore-c/nix_api_store.h (1)
221-235: Callback uses const char * for path parameter.The callback signature (line 234) uses
const char * pathwhich is consistent with the implementation, but creates the dangling pointer risk discussed in the.ccfile review (lines 198-221).See the review comment on
src/libstore-c/nix_api_store.cclines 198-221 for the detailed analysis and suggested fix.src/libstore-c/nix_api_store.cc (1)
198-221: Potential dangling pointer in callback.Line 216 passes
store->ptr->printStorePath(outPath).c_str()to the callback. While the temporary survives the immediate call, if the callback stores this pointer for later use, it will dangle.Past review comments suggested using
const StorePath *instead ofconst char *. If this was intentional (for API simplicity), document that the path string is only valid during the callback:/** * @brief Hashes the derivation and gives the output paths * + * @note The path string passed to the callback is only valid for the duration + * of the callback invocation. Callers must copy it if they need to retain it. + * * @param[in] context Optional, stores error information.Alternatively, apply the safer API pattern used in other functions:
nix_err nix_derivation_make_outputs( nix_c_context * context, Store * store, const char * json, - void (*callback)(void * userdata, const char * output_name, const char * path), + void (*callback)(void * userdata, const char * output_name, const StorePath * path), void * userdata) { // ... existing code ... if (callback) { - callback(userdata, output.first.c_str(), store->ptr->printStorePath(outPath).c_str()); + const StorePath tmp{outPath}; + callback(userdata, output.first.c_str(), &tmp); }This would require updating both the header (line 234) and implementation.
🧹 Nitpick comments (7)
src/libstore-c/nix_api_store.cc (3)
313-340: Document temporary string lifetimes in callback.Lines 335 passes temporary C-strings (from
.c_str()) to the callback. While these survive the immediate call, storing them leads to use-after-free.Add documentation clarifying the string lifetimes:
/** * @brief Builds the paths, if they are a derivation then they get built. * - * @note Path and result for the callback only exist for the lifetime of - * the call. Result is a string containing the build result in JSON. + * @note The path and result strings passed to the callback are only valid + * for the duration of the callback invocation. Callers must copy them + * if they need to retain the data. + * Result is a string containing the build result in JSON.This matches the pattern in other callback-based APIs and makes the contract explicit.
127-154: Document StorePath lifetime in headerAugment the
@param callbackdoc (around line 188–189) to note that theStorePath *outpointer is borrowed and only valid during the callback invocation.
182-196: Add explanatory comment for read-only derivation pathExplain that the
readOnly = trueflag bypasses adding to the store and only computes the fixed-output path for validation:src/libstore-c/nix_api_store.cc:189
- auto drvPath = nix::writeDerivation(*store->ptr, drv, nix::NoRepair, /* read only */ true); + // In read-only mode we compute the fixed-output (CA) path for validation without writing to the store + // (writeDerivation returns a path derived from the derivation contents) + auto drvPath = nix::writeDerivation(*store->ptr, drv, nix::NoRepair, /* read only */ true);src/libstore-c/nix_api_store.h (3)
190-195: Document borrowed StorePath in callback.The callback signature correctly uses
const StorePath *(line 195), but the documentation should explicitly state that theStorePathis borrowed and only valid during the callback./** * @brief Realise a Nix store path * * Blocking, calls callback once for each realised output. * + * @note The StorePath passed to the callback is borrowed and only valid + * for the duration of the callback invocation. + * * @note When working with expressions, consider using e.g. nix_string_realise to get the output. `.drvPath` may not be
246-252: Document Store lifetime requirements.The documentation should clarify that freeing a derivation does not affect the associated
Store, and that theStoremust remain valid for all derivations that reference it./** - * @brief Deallocate a `nix_derivation' + * @brief Deallocate a `nix_derivation` * + * @note This does not free the associated Store. The Store must outlive + * all derivations that reference it. + * * Does not fail. * @param[in] drv the derivation to free */
254-260: Document Store sharing in clone.The clone shares the same
Storepointer as the original. Document this to clarify lifetime requirements./** * @brief Copy a `nix_derivation` * + * @note The cloned derivation shares the same Store reference as the original. + * The Store must remain valid for both the original and all clones. + * * @param[in] d the derivation to copy * @return a new `nix_derivation` */src/libstore-c/nix_api_store_internal.h (1)
16-20: Document Store lifetime requirements for nix_derivation.The
nix_derivationstruct holds a borrowedStore *pointer. Users must ensure theStoreoutlives anynix_derivationthat references it, otherwise dereferencingdrv->storeleads to use-after-free.Consider adding this comment to the internal header:
struct nix_derivation { nix::Derivation drv; + // Borrowed pointer; the Store must outlive this nix_derivation. + // Ownership remains with the caller who provided the Store. Store * store; };The public API header currently lacks any mention of this requirement. To verify, you can run:
rg -n -C3 -P "(outlive|lifetime|must remain valid|borrowed)" src/libstore-c/nix_api_store.hNo matches for
Storeindicate the lifetime contract isn’t documented there. Add a corresponding note innix_api_store.hnear the functions that produce or acceptnix_derivation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libstore-c/nix_api_store.cc(5 hunks)src/libstore-c/nix_api_store.h(5 hunks)src/libstore-c/nix_api_store_internal.h(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/libstore-c/nix_api_store.h (1)
src/libstore-c/nix_api_store.cc (28)
nix_store_is_valid_path(94-102)nix_store_is_valid_path(94-94)callback(132-132)callback(202-202)callback(255-255)callback(279-279)callback(318-318)callback(346-346)nix_derivation_from_json(182-196)nix_derivation_from_json(182-182)nix_derivation_make_outputs(198-221)nix_derivation_make_outputs(198-203)nix_add_derivation(223-233)nix_add_derivation(223-223)nix_derivation_free(167-170)nix_derivation_free(167-167)nix_derivation_clone(177-180)nix_derivation_clone(177-177)nix_store_drv_from_path(275-292)nix_store_drv_from_path(275-280)nix_store_query_path_info(294-311)nix_store_query_path_info(294-299)nix_store_build_paths(313-340)nix_store_build_paths(313-319)nix_derivation_get_outputs_and_optpaths(342-365)nix_derivation_get_outputs_and_optpaths(342-347)nix_derivation_to_json(367-379)nix_derivation_to_json(367-368)
src/libstore-c/nix_api_store.cc (2)
src/libstore/include/nix/store/store-api.hh (24)
path(318-318)path(340-340)path(346-346)path(353-353)path(357-357)path(394-394)path(400-400)path(405-405)path(415-415)path(451-451)path(461-464)path(461-461)path(474-477)path(474-474)path(482-482)path(490-490)callback(425-425)callback(453-453)drvPath(697-697)drvPath(772-772)drvPath(777-777)drvPath(782-782)info(535-539)info(762-762)src/libstore/include/nix/store/realisation.hh (1)
outPath(80-83)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build_x86_64-linux / vm_tests_smoke
- GitHub Check: build_x86_64-linux / test
- GitHub Check: build_x86_64-linux / manual
- GitHub Check: build_aarch64-darwin / build
🔇 Additional comments (17)
src/libstore-c/nix_api_store.cc (8)
94-102: LGTM: const-correctness improvement.The parameter is now correctly marked
constsince the path is not modified.
167-170: LGTM: proper resource cleanup.Correctly frees the
nix_derivationwithout touching the borrowedStorepointer.
177-180: LGTM: clone now preserves Store pointer.The clone correctly copies both
drvandstore. Both the original and clone share the same borrowedStorepointer, which is the expected behavior.
223-233: LGTM: correctly writes derivation to store.This function actually writes the derivation (unlike
nix_derivation_from_jsonwhich uses read-only mode) and returns the resulting store path.
275-292: LGTM: derivation wrapper now includes Store.The temporary wrapper (line 287) correctly includes both
drvandstore, fixing the issue from past reviews. The callback receives a borrowed derivation valid only during its execution.
294-311: LGTM: correct string callback pattern.The JSON string is created as a local variable and passed to the callback via
data()andsize(). The string remains valid for the callback duration.
342-365: LGTM: proper handling of optional paths.Correctly creates a temporary
StorePathwrapper when a path exists (line 356) and passesnullptrwhen absent (line 359). The callback receives borrowed pointers valid only during the call.
367-379: No parameter name mismatch;userdatais consistent in header and implementation.src/libstore-c/nix_api_store.h (9)
26-27: LGTM: opaque type declaration.Standard C API pattern for opaque types.
153-153: LGTM: const-qualified parameter.Correctly marks the path parameter as
constsince it's not modified.
212-219: LGTM: function declaration.Declaration is clear and matches the implementation.
237-244: LGTM: clear API declaration.Documentation and signature are clear and match the implementation.
296-312: LGTM: well-documented API.The documentation correctly uses Doxygen format and notes that the derivation is borrowed during the callback.
314-328: LGTM: documentation matches parameter.Parameter name in documentation (line 319) correctly matches the function parameter (line 326).
330-349: Documentation mentions temporary strings.The note at lines 333-334 correctly states that strings are temporary, though the wording could be more explicit (see review comment on implementation in
.ccfile lines 313-340).
351-367: LGTM: accurate documentation.Documentation correctly describes that the callback receives output names and optional store paths, not full
DerivationOutputstructures.
369-378: Verify implementation parameter name.Header uses
userdata(line 378). Verify this matches the implementation (see review comment onsrc/libstore-c/nix_api_store.cclines 367-379).
Motivation
Need to be able to read some derivation information from Rust.
Context
Summary by CodeRabbit
New Features
API Changes
Tests
Documentation