Skip to content

Commit b04856b

Browse files
C API: add nix_locked_flake_read_path for flake file reading
1 parent 236ebef commit b04856b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/libflake-c/nix_api_flake.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,20 @@ nix_value * nix_locked_flake_get_output_attrs(
206206
NIXC_CATCH_ERRS_NULL
207207
}
208208

209+
nix_err nix_locked_flake_read_path(
210+
nix_c_context * context,
211+
nix_locked_flake * lockedFlake,
212+
const char * path,
213+
nix_get_string_callback callback,
214+
void * user_data)
215+
{
216+
nix_clear_err(context);
217+
try {
218+
auto source_path = lockedFlake->lockedFlake->flake.path.parent() / nix::CanonPath(path);
219+
auto v = source_path.readFile();
220+
return call_nix_get_string_callback(v, callback, user_data);
221+
}
222+
NIXC_CATCH_ERRS
223+
}
224+
209225
} // extern "C"

src/libflake-c/nix_api_flake.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@ void nix_flake_reference_free(nix_flake_reference * store);
238238
nix_value * nix_locked_flake_get_output_attrs(
239239
nix_c_context * context, nix_flake_settings * settings, EvalState * evalState, nix_locked_flake * lockedFlake);
240240

241+
/**
242+
* @brief Reads a file within the flake.
243+
* @note The callback borrows the string only for the duration of the call.
244+
*
245+
* @param[out] context Optional, stores error information
246+
* @param[in] locked_flake the flake to get the path for
247+
* @param[in] path The path within the flake.
248+
* @param[in] callback The callback to call with the string
249+
* @param[in] user_data Additional data to pass for the callback
250+
*/
251+
nix_err nix_locked_flake_read_path(
252+
nix_c_context * context,
253+
nix_locked_flake * lockedFlake,
254+
const char * path,
255+
nix_get_string_callback callback,
256+
void * user_data);
257+
241258
#ifdef __cplusplus
242259
} // extern "C"
243260
#endif

0 commit comments

Comments
 (0)