1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 22:42:41 +01:00

Add new C API for working with derivations

Also test the APIs we just added.
This commit is contained in:
John Ericson 2025-09-02 11:20:51 -04:00
parent 1a69fc6ab5
commit 9bc218ca3f
13 changed files with 270 additions and 47 deletions

View file

@ -0,0 +1,23 @@
{
"args": [
"-c",
"echo $name foo > $out"
],
"builder": "/bin/sh",
"env": {
"builder": "/bin/sh",
"name": "myname",
"out": "/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9",
"system": "x86_64-linux"
},
"inputDrvs": {},
"inputSrcs": [],
"name": "myname",
"outputs": {
"out": {
"hashAlgo": "sha256",
"method": "nar"
}
},
"system": "x86_64-linux"
}

View file

@ -0,0 +1,15 @@
#include <gtest/gtest.h>
#include "nix/store/tests/test-main.hh"
using namespace nix;
int main(int argc, char ** argv)
{
auto res = testMainForBuidingPre(argc, argv);
if (res)
return res;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -66,6 +66,7 @@ sources = files(
'local-overlay-store.cc',
'local-store.cc',
'machines.cc',
'main.cc',
'nar-info-disk-cache.cc',
'nar-info.cc',
'nix_api_store.cc',

View file

@ -1,7 +1,10 @@
#include <fstream>
#include "nix_api_util.h"
#include "nix_api_store.h"
#include "nix/store/tests/nix_api_store.hh"
#include "nix/store/globals.hh"
#include "nix/util/tests/string_callback.hh"
#include "nix/util/url.hh"
@ -197,4 +200,60 @@ TEST_F(nix_api_util_context, nix_store_real_path_binary_cache)
ASSERT_STREQ(path_raw.c_str(), rp.c_str());
}
template<typename F>
struct LambdaAdapter
{
F fun;
template<typename... Args>
static inline auto call(LambdaAdapter<F> * ths, Args... args)
{
return ths->fun(args...);
}
template<typename... Args>
static auto call_void(void * ths, Args... args)
{
return call(static_cast<LambdaAdapter<F> *>(ths), args...);
}
};
TEST_F(nix_api_store_test_base, build_from_json)
{
// FIXME get rid of these
nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations");
nix::settings.substituters = {};
auto * store = open_local_store();
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
std::ifstream t{unitTestData / "derivation/ca/self-contained.json"};
std::stringstream buffer;
buffer << t.rdbuf();
auto * drv = nix_derivation_from_json(ctx, store, buffer.str().c_str());
assert_ctx_ok();
ASSERT_NE(drv, nullptr);
auto * drvPath = nix_add_derivation(ctx, store, drv);
assert_ctx_ok();
ASSERT_NE(drv, nullptr);
auto cb = LambdaAdapter{.fun = [&](const char * outname, const StorePath * outPath) {
auto is_valid_path = nix_store_is_valid_path(ctx, store, outPath);
ASSERT_EQ(is_valid_path, true);
}};
auto ret = nix_store_realise(
ctx, store, drvPath, static_cast<void *>(&cb), decltype(cb)::call_void<const char *, const StorePath *>);
assert_ctx_ok();
ASSERT_EQ(ret, NIX_OK);
// Clean up
nix_store_path_free(drvPath);
nix_derivation_free(drv);
nix_store_free(store);
}
} // namespace nixC