diff --git a/src/libexpr/nix_api_value.cc b/src/libexpr/nix_api_value.cc index ead544a0b..ceff036c9 100644 --- a/src/libexpr/nix_api_value.cc +++ b/src/libexpr/nix_api_value.cc @@ -266,6 +266,18 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, NIXC_CATCH_ERRS_NULL } +const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i) { + if (context) + context->last_err_code = NIX_OK; + try { + auto &v = check_value_not_null(value); + const nix::Attr &a = (*v.attrs)[i]; + return ((const std::string &)(state->state.symbols[a.name])).c_str(); + } + NIXC_CATCH_ERRS_NULL +} + nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) { if (context) context->last_err_code = NIX_OK; diff --git a/src/libexpr/nix_api_value.h b/src/libexpr/nix_api_value.h index f47dafa6a..08eb34fc5 100644 --- a/src/libexpr/nix_api_value.h +++ b/src/libexpr/nix_api_value.h @@ -207,6 +207,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, State *state, const char *name); /** @brief Get an attribute by index in the sorted bindings + * + * Also gives you the name. * * Owned by the GC. Use nix_gc_decref when you're done with the pointer * @param[out] context Optional, stores error information @@ -218,6 +220,20 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value, */ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value, State *state, unsigned int i, const char **name); + +/** @brief Get an attribute name by index in the sorted bindings + * + * Useful when you want the name but want to avoid evaluation. + * + * Owned by the nix State + * @param[out] context Optional, stores error information + * @param[in] value Nix value to inspect + * @param[in] state nix evaluator state + * @param[in] i attribute index + * @return name, NULL in case of errors + */ +const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value, + State *state, unsigned int i); /**@}*/ /** @name Setters */