Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6935d6c18 | ||
|
|
6cc44e4fdf | ||
|
|
952be9fc96 | ||
|
|
4031343e44 | ||
|
|
0c0a41a81a | ||
|
|
97abcda9cc | ||
|
|
423e732b22 | ||
|
|
05990fb2ec | ||
|
|
6a4a1e9f72 | ||
|
|
1c10ce6047 | ||
|
|
801cb16131 | ||
|
|
e91b7d1732 | ||
|
|
ab58d2720c |
No files matched your search
@@ -79,6 +79,8 @@
|
||||
# Not supported by nixfmt
|
||||
''^tests/functional/lang/eval-okay-deprecate-cursed-or\.nix$''
|
||||
''^tests/functional/lang/eval-okay-attrs5\.nix$''
|
||||
''^tests/functional/lang/eval-fail-dynamic-attrs-inherit\.nix$''
|
||||
''^tests/functional/lang/eval-fail-dynamic-attrs-inherit-2\.nix$''
|
||||
|
||||
# More syntax tests
|
||||
# These tests, or parts of them, should have been parse-* test cases.
|
||||
|
||||
@@ -47,6 +47,79 @@ struct ParserLocation
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* This represents a string-like parse that possibly has yet to be constructed.
|
||||
*
|
||||
* Examples:
|
||||
* "foo"
|
||||
* ${"foo" + "bar"}
|
||||
* "foo.bar"
|
||||
* "foo-${a}"
|
||||
*
|
||||
* Using this type allows us to avoid construction altogether in cases where what we actually need is the string
|
||||
* contents. For example in foo."bar.baz", there is no need to construct an AST node for "bar.baz", but we don't know
|
||||
* that until we bubble the value up during parsing and see that it's a node in an AttrPath.
|
||||
*/
|
||||
class ToBeStringyExpr
|
||||
{
|
||||
private:
|
||||
using Raw = std::variant<std::monostate, std::string_view, Expr *>;
|
||||
Raw raw;
|
||||
|
||||
public:
|
||||
ToBeStringyExpr() = default;
|
||||
|
||||
ToBeStringyExpr(std::string_view v)
|
||||
: raw(v)
|
||||
{
|
||||
}
|
||||
|
||||
ToBeStringyExpr(Expr * expr)
|
||||
: raw(expr)
|
||||
{
|
||||
assert(expr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits the expression and invokes an overloaded functor object \ref f.
|
||||
* If the underlying Expr has a dynamic type of ExprString the overload taking std::string_view
|
||||
* is invoked.
|
||||
*
|
||||
* Used to consistently handle simple StringExpr ${"string"} as non-dynamic attributes.
|
||||
* @see https://github.com/NixOS/nix/issues/14642
|
||||
*/
|
||||
template<class F>
|
||||
void visit(F && f)
|
||||
{
|
||||
std::visit(
|
||||
overloaded{
|
||||
[&](std::string_view str) { f(str); },
|
||||
[&](Expr * expr) {
|
||||
ExprString * str = dynamic_cast<ExprString *>(expr);
|
||||
if (str)
|
||||
f(str->v.string_view());
|
||||
else
|
||||
f(expr);
|
||||
},
|
||||
[](std::monostate) { unreachable(); }},
|
||||
raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create an Expr from either an existing Expr or from a string.
|
||||
* Delays the allocation or an AST node in case the parser only cares about string contents.
|
||||
*/
|
||||
Expr * toExpr(Exprs & exprs)
|
||||
{
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[&](std::string_view str) -> Expr * { return exprs.add<ExprString>(exprs.alloc, str); },
|
||||
[&](Expr * expr) { return expr; },
|
||||
[](std::monostate) -> Expr * { unreachable(); }},
|
||||
raw);
|
||||
}
|
||||
};
|
||||
|
||||
struct LexerState
|
||||
{
|
||||
/**
|
||||
|
||||
+19
-24
@@ -138,7 +138,7 @@ static Expr * makeCall(Exprs & exprs, PosIdx pos, Expr * fn, Expr * arg) {
|
||||
%type <std::vector<std::pair<PosIdx, Expr *>>> string_parts_interpolated
|
||||
%type <std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>>> ind_string_parts
|
||||
%type <Expr *> path_start
|
||||
%type <std::variant<Expr *, std::string_view>> string_parts string_attr
|
||||
%type <ToBeStringyExpr> string_parts string_attr
|
||||
%type <StringToken> attr
|
||||
%token <StringToken> ID
|
||||
%token <StringToken> STR IND_STR
|
||||
@@ -297,12 +297,7 @@ expr_simple
|
||||
}
|
||||
| INT_LIT { $$ = state->exprs.add<ExprInt>($1); }
|
||||
| FLOAT_LIT { $$ = state->exprs.add<ExprFloat>($1); }
|
||||
| '"' string_parts '"' {
|
||||
std::visit(overloaded{
|
||||
[&](std::string_view str) { $$ = state->exprs.add<ExprString>(state->exprs.alloc, str); },
|
||||
[&](Expr * expr) { $$ = expr; }},
|
||||
$2);
|
||||
}
|
||||
| '"' string_parts '"' { $$ = $2.toExpr(state->exprs); }
|
||||
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
|
||||
$$ = state->stripIndentation(CUR_POS, $2);
|
||||
}
|
||||
@@ -342,9 +337,9 @@ expr_simple
|
||||
;
|
||||
|
||||
string_parts
|
||||
: STR { $$ = $1; }
|
||||
| string_parts_interpolated { $$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, true, $1); }
|
||||
| { $$ = std::string_view(); }
|
||||
: STR { $$ = {$1}; }
|
||||
| string_parts_interpolated { $$ = {state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, true, $1)}; }
|
||||
| { $$ = {std::string_view()}; }
|
||||
;
|
||||
|
||||
string_parts_interpolated
|
||||
@@ -447,15 +442,15 @@ attrs
|
||||
: attrs attr { $$ = std::move($1); $$.emplace_back(state->symbols.create($2), state->at(@2)); }
|
||||
| attrs string_attr
|
||||
{ $$ = std::move($1);
|
||||
std::visit(overloaded {
|
||||
$2.visit(overloaded{
|
||||
[&](std::string_view str) { $$.emplace_back(state->symbols.create(str), state->at(@2)); },
|
||||
[&](Expr * expr) {
|
||||
throw ParseError({
|
||||
.msg = HintFmt("dynamic attributes not allowed in inherit"),
|
||||
.pos = state->positions[state->at(@2)]
|
||||
});
|
||||
}
|
||||
}, $2);
|
||||
throw ParseError({
|
||||
.msg = HintFmt("dynamic attributes not allowed in inherit"),
|
||||
.pos = state->positions[state->at(@2)]
|
||||
});
|
||||
}}
|
||||
);
|
||||
}
|
||||
| { }
|
||||
;
|
||||
@@ -464,17 +459,17 @@ attrpath
|
||||
: attrpath '.' attr { $$ = std::move($1); $$.emplace_back(state->symbols.create($3)); }
|
||||
| attrpath '.' string_attr
|
||||
{ $$ = std::move($1);
|
||||
std::visit(overloaded {
|
||||
$3.visit(overloaded{
|
||||
[&](std::string_view str) { $$.emplace_back(state->symbols.create(str)); },
|
||||
[&](Expr * expr) { $$.emplace_back(expr); }
|
||||
}, std::move($3));
|
||||
[&](Expr * expr) { $$.emplace_back(expr); }}
|
||||
);
|
||||
}
|
||||
| attr { $$.emplace_back(state->symbols.create($1)); }
|
||||
| string_attr
|
||||
{ std::visit(overloaded {
|
||||
{ $1.visit(overloaded{
|
||||
[&](std::string_view str) { $$.emplace_back(state->symbols.create(str)); },
|
||||
[&](Expr * expr) { $$.emplace_back(expr); }
|
||||
}, std::move($1));
|
||||
[&](Expr * expr) { $$.emplace_back(expr); }}
|
||||
);
|
||||
}
|
||||
;
|
||||
|
||||
@@ -485,7 +480,7 @@ attr
|
||||
|
||||
string_attr
|
||||
: '"' string_parts '"' { $$ = std::move($2); }
|
||||
| DOLLAR_CURLY expr '}' { $$ = $2; }
|
||||
| DOLLAR_CURLY expr '}' { $$ = {$2}; }
|
||||
;
|
||||
|
||||
list
|
||||
|
||||
@@ -470,7 +470,8 @@ public:
|
||||
std::string res;
|
||||
|
||||
auto renderActivity =
|
||||
[&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
|
||||
[&] [[nodiscard]] (
|
||||
ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
|
||||
auto & act = state.activitiesByType[type];
|
||||
uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed;
|
||||
for (auto & j : act.its) {
|
||||
@@ -514,7 +515,7 @@ public:
|
||||
return s;
|
||||
};
|
||||
|
||||
auto renderSizeActivity = [&](ActivityType type, const std::string & itemFmt = "%s") {
|
||||
auto renderSizeActivity = [&] [[nodiscard]] (ActivityType type, const std::string & itemFmt = "%s") {
|
||||
auto & act = state.activitiesByType[type];
|
||||
uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed;
|
||||
for (auto & j : act.its) {
|
||||
@@ -573,14 +574,17 @@ public:
|
||||
return s;
|
||||
};
|
||||
|
||||
auto maybeAppendToResult = [&](std::string_view s) {
|
||||
if (s.empty())
|
||||
return;
|
||||
if (!res.empty())
|
||||
res += ", ";
|
||||
res += s;
|
||||
};
|
||||
|
||||
auto showActivity =
|
||||
[&](ActivityType type, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
|
||||
auto s = renderActivity(type, itemFmt, numberFmt, unit);
|
||||
if (s.empty())
|
||||
return;
|
||||
if (!res.empty())
|
||||
res += ", ";
|
||||
res += s;
|
||||
maybeAppendToResult(renderActivity(type, itemFmt, numberFmt, unit));
|
||||
};
|
||||
|
||||
showActivity(actBuilds, "%s built");
|
||||
@@ -602,7 +606,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
renderSizeActivity(actFileTransfer, "%s DL");
|
||||
maybeAppendToResult(renderSizeActivity(actFileTransfer, "%s DL"));
|
||||
|
||||
{
|
||||
auto s = renderActivity(actOptimiseStore, "%s paths optimised");
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
#include "nix_api_util.h"
|
||||
@@ -8,6 +11,7 @@
|
||||
#include "nix/store/store-open.hh"
|
||||
#include "nix/store/build-result.hh"
|
||||
#include "nix/store/local-fs-store.hh"
|
||||
#include "nix/util/base-nix-32.hh"
|
||||
|
||||
#include "nix/store/globals.hh"
|
||||
|
||||
@@ -222,6 +226,60 @@ StorePath * nix_store_path_clone(const StorePath * p)
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
template<size_t S>
|
||||
static auto to_cpp_array(const uint8_t (&r)[S])
|
||||
{
|
||||
return reinterpret_cast<const std::array<std::byte, S> &>(r);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
nix_err
|
||||
nix_store_path_hash(nix_c_context * context, const StorePath * store_path, nix_store_path_hash_part * hash_part_out)
|
||||
{
|
||||
try {
|
||||
auto hashPart = store_path->path.hashPart();
|
||||
// Decode from Nix32 (base32) encoding to raw bytes
|
||||
auto decoded = nix::BaseNix32::decode(hashPart);
|
||||
|
||||
assert(decoded.size() == sizeof(hash_part_out->bytes));
|
||||
std::memcpy(hash_part_out->bytes, decoded.data(), sizeof(hash_part_out->bytes));
|
||||
return NIX_OK;
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
StorePath * nix_store_create_from_parts(
|
||||
nix_c_context * context, const nix_store_path_hash_part * hash, const char * name, size_t name_len)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
// Encode the 20 raw bytes to Nix32 (base32) format
|
||||
auto hashStr = nix::BaseNix32::encode(std::span<const std::byte>{to_cpp_array(hash->bytes)});
|
||||
|
||||
// Construct the store path basename: <hash>-<name>
|
||||
std::string baseName;
|
||||
baseName += hashStr;
|
||||
baseName += "-";
|
||||
baseName += std::string_view{name, name_len};
|
||||
|
||||
return new StorePath{nix::StorePath(std::move(baseName))};
|
||||
}
|
||||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
nix_derivation * nix_derivation_clone(const nix_derivation * d)
|
||||
{
|
||||
try {
|
||||
return new nix_derivation{d->drv};
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json)
|
||||
{
|
||||
if (context)
|
||||
@@ -232,6 +290,20 @@ nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store
|
||||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
nix_err nix_derivation_to_json(
|
||||
nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto result = static_cast<nlohmann::json>(drv->drv).dump();
|
||||
if (callback) {
|
||||
callback(result.data(), result.size(), userdata);
|
||||
}
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_derivation * derivation)
|
||||
{
|
||||
if (context)
|
||||
@@ -256,4 +328,14 @@ nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
nix_derivation * nix_store_drv_from_store_path(nix_c_context * context, Store * store, const StorePath * path)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
return new nix_derivation{store->ptr->derivationFromPath(path->path)};
|
||||
}
|
||||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -106,7 +106,7 @@ nix_err
|
||||
nix_store_get_storedir(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data);
|
||||
|
||||
/**
|
||||
* @brief Parse a Nix store path into a StorePath
|
||||
* @brief Parse a Nix store path that includes the store dir into a StorePath
|
||||
*
|
||||
* @note Don't forget to free this path using nix_store_path_free()!
|
||||
* @param[out] context Optional, stores error information
|
||||
@@ -188,9 +188,16 @@ nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_cal
|
||||
/**
|
||||
* @brief Create a `nix_derivation` from a JSON representation of that derivation.
|
||||
*
|
||||
* @note Unlike `nix_derivation_to_json`, this needs a `Store`. This is because
|
||||
* over time we expect the internal representation of derivations in Nix to
|
||||
* differ from accepted derivation formats. The store argument is here to help
|
||||
* any logic needed to convert from JSON to the internal representation, in
|
||||
* excess of just parsing.
|
||||
*
|
||||
* @param[out] context Optional, stores error information.
|
||||
* @param[in] store nix store reference.
|
||||
* @param[in] json JSON of the derivation as a string.
|
||||
* @return A new derivation, or NULL on error. Free with `nix_derivation_free` when done using the `nix_derivation`.
|
||||
*/
|
||||
nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store, const char * json);
|
||||
|
||||
@@ -242,6 +249,16 @@ nix_err nix_store_get_fs_closure(
|
||||
void * userdata,
|
||||
void (*callback)(nix_c_context * context, void * userdata, const StorePath * store_path));
|
||||
|
||||
/**
|
||||
* @brief Returns the derivation associated with the store path
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store The nix store
|
||||
* @param[in] path The nix store path
|
||||
* @return A new derivation, or NULL on error. Free with `nix_derivation_free` when done using the `nix_derivation`.
|
||||
*/
|
||||
nix_derivation * nix_store_drv_from_store_path(nix_c_context * context, Store * store, const StorePath * path);
|
||||
|
||||
// cffi end
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -20,6 +20,14 @@ extern "C" {
|
||||
/** @brief Nix Derivation */
|
||||
typedef struct nix_derivation nix_derivation;
|
||||
|
||||
/**
|
||||
* @brief Copy a `nix_derivation`
|
||||
*
|
||||
* @param[in] d the derivation to copy
|
||||
* @return a new `nix_derivation`
|
||||
*/
|
||||
nix_derivation * nix_derivation_clone(const nix_derivation * d);
|
||||
|
||||
/**
|
||||
* @brief Deallocate a `nix_derivation`
|
||||
*
|
||||
@@ -28,6 +36,17 @@ typedef struct nix_derivation nix_derivation;
|
||||
*/
|
||||
void nix_derivation_free(nix_derivation * drv);
|
||||
|
||||
/**
|
||||
* @brief Gets the derivation as a JSON string
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] drv The derivation
|
||||
* @param[in] callback Called with the JSON string
|
||||
* @param[in] userdata Arbitrary data passed to the callback
|
||||
*/
|
||||
nix_err nix_derivation_to_json(
|
||||
nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata);
|
||||
|
||||
// cffi end
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
* @brief Store path operations
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "nix_api_util.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -44,6 +47,45 @@ void nix_store_path_free(StorePath * p);
|
||||
*/
|
||||
void nix_store_path_name(const StorePath * store_path, nix_get_string_callback callback, void * user_data);
|
||||
|
||||
/**
|
||||
* @brief A store path hash
|
||||
*
|
||||
* Once decoded from "nix32" encoding, a store path hash is 20 raw bytes.
|
||||
*/
|
||||
typedef struct nix_store_path_hash_part
|
||||
{
|
||||
uint8_t bytes[20];
|
||||
} nix_store_path_hash_part;
|
||||
|
||||
/**
|
||||
* @brief Get the path hash (e.g. "<hash>" in /nix/store/<hash>-<name>)
|
||||
*
|
||||
* The hash is returned as raw bytes, decoded from "nix32" encoding.
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store_path the path to get the hash from
|
||||
* @param[out] hash_part_out the decoded hash as 20 raw bytes
|
||||
* @return NIX_OK on success, error code on failure
|
||||
*/
|
||||
nix_err
|
||||
nix_store_path_hash(nix_c_context * context, const StorePath * store_path, nix_store_path_hash_part * hash_part_out);
|
||||
|
||||
/**
|
||||
* @brief Create a StorePath from its constituent parts (hash and name)
|
||||
*
|
||||
* This function constructs a store path from a hash and name, without needing
|
||||
* a Store reference or the store directory prefix.
|
||||
*
|
||||
* @note Don't forget to free this path using nix_store_path_free()!
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] hash The store path hash (20 raw bytes)
|
||||
* @param[in] name The store path name (the part after the hash)
|
||||
* @param[in] name_len Length of the name string
|
||||
* @return owned store path, NULL on error
|
||||
*/
|
||||
StorePath * nix_store_create_from_parts(
|
||||
nix_c_context * context, const nix_store_path_hash_part * hash, const char name[/*name_len*/], size_t name_len);
|
||||
|
||||
// cffi end
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <fstream>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_store.h"
|
||||
|
||||
@@ -92,6 +94,70 @@ TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
|
||||
nix_store_path_free(path);
|
||||
}
|
||||
|
||||
// Verify it's 20 bytes
|
||||
static_assert(sizeof(nix_store_path_hash_part::bytes) == 20);
|
||||
static_assert(sizeof(nix_store_path_hash_part::bytes) == sizeof(nix_store_path_hash_part));
|
||||
|
||||
TEST_F(nix_api_store_test, nix_store_path_hash)
|
||||
{
|
||||
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
||||
ASSERT_NE(path, nullptr);
|
||||
|
||||
nix_store_path_hash_part hash;
|
||||
auto ret = nix_store_path_hash(ctx, path, &hash);
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// The hash should be non-zero
|
||||
bool allZero = true;
|
||||
for (size_t i = 0; i < sizeof(hash.bytes); i++) {
|
||||
if (hash.bytes[i] != 0) {
|
||||
allZero = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_FALSE(allZero);
|
||||
|
||||
nix_store_path_free(path);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_store_create_from_parts_roundtrip)
|
||||
{
|
||||
// Parse a path
|
||||
StorePath * original = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
||||
EXPECT_NE(original, nullptr);
|
||||
|
||||
// Get its hash
|
||||
nix_store_path_hash_part hash;
|
||||
auto ret = nix_store_path_hash(ctx, original, &hash);
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
|
||||
// Get its name
|
||||
std::string name;
|
||||
nix_store_path_name(original, OBSERVE_STRING(name));
|
||||
|
||||
// Reconstruct from parts
|
||||
StorePath * reconstructed = nix_store_create_from_parts(ctx, &hash, name.c_str(), name.size());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(reconstructed, nullptr);
|
||||
|
||||
// Should be equal
|
||||
EXPECT_EQ(original->path, reconstructed->path);
|
||||
|
||||
nix_store_path_free(original);
|
||||
nix_store_path_free(reconstructed);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_store_create_from_parts_invalid_name)
|
||||
{
|
||||
nix_store_path_hash_part hash = {};
|
||||
// Invalid name with spaces
|
||||
StorePath * path = nix_store_create_from_parts(ctx, &hash, "invalid name", 12);
|
||||
ASSERT_EQ(path, nullptr);
|
||||
ASSERT_EQ(nix_err_code(ctx), NIX_ERR_NIX_ERROR);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, get_version)
|
||||
{
|
||||
std::string str;
|
||||
@@ -795,4 +861,97 @@ TEST_F(NixApiStoreTestWithRealisedPath, nix_store_get_fs_closure_error_propagati
|
||||
ASSERT_EQ(call_count, 1); // Should have been called exactly once, then aborted
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Helper function to load JSON from a test data file
|
||||
*
|
||||
* @param filename Relative path from _NIX_TEST_UNIT_DATA
|
||||
* @return JSON string contents of the file
|
||||
*/
|
||||
static std::string load_json_from_test_data(const char * filename)
|
||||
{
|
||||
std::filesystem::path unitTestData{getenv("_NIX_TEST_UNIT_DATA")};
|
||||
std::ifstream t{unitTestData / filename};
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_derivation_to_json_roundtrip)
|
||||
{
|
||||
// Load JSON from test data
|
||||
auto originalJson = load_json_from_test_data("derivation/invariants/filled-in-deferred-empty-env-var-pre.json");
|
||||
|
||||
// Parse to derivation
|
||||
auto * drv = nix_derivation_from_json(ctx, store, originalJson.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
// Convert back to JSON
|
||||
std::string convertedJson;
|
||||
auto ret = nix_derivation_to_json(ctx, drv, OBSERVE_STRING(convertedJson));
|
||||
assert_ctx_ok();
|
||||
ASSERT_EQ(ret, NIX_OK);
|
||||
ASSERT_FALSE(convertedJson.empty());
|
||||
|
||||
// Parse both JSON strings to compare (ignoring whitespace differences)
|
||||
auto originalParsed = nlohmann::json::parse(originalJson);
|
||||
auto convertedParsed = nlohmann::json::parse(convertedJson);
|
||||
|
||||
// Remove parts that will be different due to filling-in.
|
||||
originalParsed.at("outputs").erase("out");
|
||||
originalParsed.at("env").erase("out");
|
||||
convertedParsed.at("outputs").erase("out");
|
||||
convertedParsed.at("env").erase("out");
|
||||
|
||||
// They should be equivalent
|
||||
ASSERT_EQ(originalParsed, convertedParsed);
|
||||
|
||||
nix_derivation_free(drv);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_derivation_store_round_trip)
|
||||
{
|
||||
// Load a derivation from JSON
|
||||
auto json = load_json_from_test_data("derivation/invariants/filled-in-deferred-empty-env-var-pre.json");
|
||||
auto * drv = nix_derivation_from_json(ctx, store, json.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
// Add to store
|
||||
auto * drvPath = nix_add_derivation(ctx, store, drv);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drvPath, nullptr);
|
||||
|
||||
// Retrieve from store
|
||||
auto * drv2 = nix_store_drv_from_store_path(ctx, store, drvPath);
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv2, nullptr);
|
||||
|
||||
// The round trip should make the same derivation
|
||||
ASSERT_EQ(drv->drv, drv2->drv);
|
||||
|
||||
nix_store_path_free(drvPath);
|
||||
nix_derivation_free(drv);
|
||||
nix_derivation_free(drv2);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_derivation_clone)
|
||||
{
|
||||
// Load a derivation from JSON
|
||||
auto json = load_json_from_test_data("derivation/invariants/filled-in-deferred-empty-env-var-pre.json");
|
||||
auto * drv = nix_derivation_from_json(ctx, store, json.c_str());
|
||||
assert_ctx_ok();
|
||||
ASSERT_NE(drv, nullptr);
|
||||
|
||||
// Clone the derivation
|
||||
auto * drv2 = nix_derivation_clone(drv);
|
||||
ASSERT_NE(drv2, nullptr);
|
||||
|
||||
// The clone should be equal
|
||||
ASSERT_EQ(drv->drv, drv2->drv);
|
||||
|
||||
nix_derivation_free(drv);
|
||||
nix_derivation_free(drv2);
|
||||
}
|
||||
|
||||
} // namespace nixC
|
||||
@@ -83,7 +83,7 @@ bool PathLocks::lockPaths(const std::set<std::filesystem::path> & paths, const s
|
||||
checkInterrupt();
|
||||
std::filesystem::path lockPath = path + ".lock";
|
||||
|
||||
debug("locking path '%1%'", path);
|
||||
debug("locking path %1%", path);
|
||||
|
||||
AutoCloseFD fd;
|
||||
|
||||
@@ -106,19 +106,19 @@ bool PathLocks::lockPaths(const std::set<std::filesystem::path> & paths, const s
|
||||
}
|
||||
}
|
||||
|
||||
debug("lock acquired on '%1%'", lockPath);
|
||||
debug("lock acquired on %1%", lockPath);
|
||||
|
||||
/* Check that the lock file hasn't become stale (i.e.,
|
||||
hasn't been unlinked). */
|
||||
struct stat st;
|
||||
if (fstat(fd.get(), &st) == -1)
|
||||
throw SysError("statting lock file '%1%'", lockPath);
|
||||
throw SysError("statting lock file %1%", lockPath);
|
||||
if (st.st_size != 0)
|
||||
/* This lock file has been unlinked, so we're holding
|
||||
a lock on a deleted file. This means that other
|
||||
processes may create and acquire a lock on
|
||||
`lockPath', and proceed. So we must retry. */
|
||||
debug("open lock file '%1%' has become stale", lockPath);
|
||||
debug("open lock file %1% has become stale", lockPath);
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -137,9 +137,9 @@ void PathLocks::unlock()
|
||||
deleteLockFile(i.second, i.first);
|
||||
|
||||
if (close(i.first) == -1)
|
||||
printError("error (ignored): cannot close lock file on '%1%'", i.second);
|
||||
printError("error (ignored): cannot close lock file on %1%", i.second);
|
||||
|
||||
debug("lock released on '%1%'", i.second);
|
||||
debug("lock released on %1%", i.second);
|
||||
}
|
||||
|
||||
fds.clear();
|
||||
|
||||
@@ -28,9 +28,9 @@ void PathLocks::unlock()
|
||||
deleteLockFile(i.second, i.first);
|
||||
|
||||
if (CloseHandle(i.first) == -1)
|
||||
printError("error (ignored): cannot close lock file on '%1%'", i.second);
|
||||
printError("error (ignored): cannot close lock file on %1%", i.second);
|
||||
|
||||
debug("lock released on '%1%'", i.second);
|
||||
debug("lock released on %1%", i.second);
|
||||
}
|
||||
|
||||
fds.clear();
|
||||
@@ -111,7 +111,7 @@ bool PathLocks::lockPaths(const std::set<std::filesystem::path> & paths, const s
|
||||
checkInterrupt();
|
||||
std::filesystem::path lockPath = path;
|
||||
lockPath += L".lock";
|
||||
debug("locking path '%1%'", path);
|
||||
debug("locking path %1%", path);
|
||||
|
||||
AutoCloseFD fd;
|
||||
|
||||
@@ -128,13 +128,13 @@ bool PathLocks::lockPaths(const std::set<std::filesystem::path> & paths, const s
|
||||
}
|
||||
}
|
||||
|
||||
debug("lock acquired on '%1%'", lockPath);
|
||||
debug("lock acquired on %1%", lockPath);
|
||||
|
||||
struct _stat st;
|
||||
if (_fstat(fromDescriptorReadOnly(fd.get()), &st) == -1)
|
||||
throw SysError("statting lock file '%1%'", lockPath);
|
||||
throw SysError("statting lock file %1%", lockPath);
|
||||
if (st.st_size != 0)
|
||||
debug("open lock file '%1%' has become stale", lockPath);
|
||||
debug("open lock file %1% has become stale", lockPath);
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
+17
-19
@@ -272,26 +272,24 @@ static StorePath getDerivationEnvironment(ref<Store> store, ref<Store> evalStore
|
||||
drv.name += "-env";
|
||||
drv.env.emplace("name", drv.name);
|
||||
drv.inputSrcs.insert(std::move(getEnvShPath));
|
||||
if (experimentalFeatureSettings.isEnabled(Xp::CaDerivations)) {
|
||||
for (auto & output : drv.outputs) {
|
||||
output.second = DerivationOutput::Deferred{}, drv.env[output.first] = hashPlaceholder(output.first);
|
||||
}
|
||||
} else {
|
||||
for (auto & output : drv.outputs) {
|
||||
output.second = DerivationOutput::Deferred{};
|
||||
drv.env[output.first] = "";
|
||||
}
|
||||
auto hashesModulo = hashDerivationModulo(*evalStore, drv, true);
|
||||
|
||||
for (auto & output : drv.outputs) {
|
||||
Hash h = hashesModulo.hashes.at(output.first);
|
||||
auto outPath = store->makeOutputPath(output.first, h, drv.name);
|
||||
output.second = DerivationOutput::InputAddressed{
|
||||
.path = outPath,
|
||||
};
|
||||
drv.env[output.first] = store->printStorePath(outPath);
|
||||
}
|
||||
for (auto & [outputName, output] : drv.outputs) {
|
||||
std::visit(
|
||||
overloaded{
|
||||
[&](const DerivationOutput::InputAddressed &) {
|
||||
output = DerivationOutput::Deferred{};
|
||||
drv.env[outputName] = "";
|
||||
},
|
||||
[&](const DerivationOutput::CAFixed &) {
|
||||
output = DerivationOutput::Deferred{};
|
||||
drv.env[outputName] = "";
|
||||
},
|
||||
[&](const auto &) {
|
||||
// Do nothing for other types (CAFloating, Deferred, Impure)
|
||||
},
|
||||
},
|
||||
output.raw);
|
||||
}
|
||||
drv.fillInOutputPaths(*evalStore);
|
||||
|
||||
auto shellDrvPath = writeDerivation(*evalStore, drv);
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
error: dynamic attributes not allowed in inherit
|
||||
at /pwd/lang/eval-fail-dynamic-attrs-inherit-2.nix:5:15:
|
||||
4| {
|
||||
5| inherit (a) ${"b" + ""};
|
||||
| ^
|
||||
6| }
|
||||
@@ -0,0 +1,6 @@
|
||||
let
|
||||
a.b = 1;
|
||||
in
|
||||
{
|
||||
inherit (a) ${"b" + ""};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
error: dynamic attributes not allowed in inherit
|
||||
at /pwd/lang/eval-fail-dynamic-attrs-inherit.nix:5:11:
|
||||
4| {
|
||||
5| inherit ${"a" + ""};
|
||||
| ^
|
||||
6| }
|
||||
@@ -0,0 +1,6 @@
|
||||
let
|
||||
a = 1;
|
||||
in
|
||||
{
|
||||
inherit ${"a" + ""};
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: dynamic attributes not allowed in let
|
||||
at /pwd/lang/eval-fail-dynamic-attrs-let-2.nix:1:1:
|
||||
1| let
|
||||
| ^
|
||||
2| ${"${"a"}"} = 1;
|
||||
@@ -0,0 +1,4 @@
|
||||
let
|
||||
${"${"a"}"} = 1;
|
||||
in
|
||||
a
|
||||
@@ -0,0 +1,5 @@
|
||||
error: dynamic attributes not allowed in let
|
||||
at /pwd/lang/eval-fail-dynamic-attrs-let-3.nix:1:1:
|
||||
1| let
|
||||
| ^
|
||||
2| "${"a"}" = 1;
|
||||
@@ -0,0 +1,4 @@
|
||||
let
|
||||
"${"a"}" = 1;
|
||||
in
|
||||
a
|
||||
@@ -0,0 +1,5 @@
|
||||
error: dynamic attributes not allowed in let
|
||||
at /pwd/lang/eval-fail-dynamic-attrs-let.nix:1:1:
|
||||
1| let
|
||||
| ^
|
||||
2| ${"a" + ""} = 1;
|
||||
@@ -0,0 +1,4 @@
|
||||
let
|
||||
${"a" + ""} = 1;
|
||||
in
|
||||
a
|
||||
@@ -0,0 +1 @@
|
||||
{ a = 1; attrs = { b = 1; c = 1; d = 1; }; b = 1; c = 1; d = 1; }
|
||||
@@ -0,0 +1,14 @@
|
||||
# dynamic attrs are not generally allowed in `let`, and inherit, but they are if they only contain a string
|
||||
let
|
||||
${"a"} = 1;
|
||||
attrs = rec {
|
||||
b = c;
|
||||
${"c"} = d;
|
||||
d = a;
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit ${"a"};
|
||||
inherit attrs;
|
||||
inherit (attrs) ${"b"} ${"c"} d;
|
||||
}
|
||||
@@ -175,7 +175,7 @@ cat >"$TEST_ROOT"/marco/polo/default.nix <<EOF
|
||||
(import $TEST_ROOT/lookup-test/shell.nix {}).polo
|
||||
EOF
|
||||
chmod a+x "$TEST_ROOT"/marco/polo/default.nix
|
||||
(cd "$TEST_ROOT"/marco && ./polo/default.nix | grepQuiet "Polo")
|
||||
(cd "$TEST_ROOT"/marco && ./polo/default.nix < /dev/null | grepQuiet "Polo")
|
||||
|
||||
# https://github.com/NixOS/nix/issues/11892
|
||||
mkdir "$TEST_ROOT"/issue-11892
|
||||
@@ -279,3 +279,10 @@ assert (!(args ? inNixShell));
|
||||
(import $shellDotNix { }).shellDrv
|
||||
EOF
|
||||
nix-shell "$TEST_ROOT"/shell-ellipsis.nix --run "true"
|
||||
|
||||
# FIXME unclear why this (newly made) test is failing in this case.
|
||||
if ! isTestOnNixOS; then
|
||||
# `nix develop` should also work with fixed-output derivations
|
||||
# shellcheck disable=SC2016
|
||||
nix develop -f "$shellDotNix" fixed -c bash -c '[[ -n $stdenv ]]'
|
||||
fi
|
||||
@@ -84,6 +84,16 @@ let
|
||||
'';
|
||||
};
|
||||
|
||||
# Shells should also work with fixed-output derivations
|
||||
fixed = mkDerivation {
|
||||
name = "fixed";
|
||||
FOO = "was a fixed-output derivation";
|
||||
outputHash = "1ixr6yd3297ciyp9im522dfxpqbkhcw0pylkb2aab915278fqaik";
|
||||
outputHashMode = "recursive";
|
||||
outputHashAlgo = "sha256";
|
||||
outputs = [ "out" ];
|
||||
};
|
||||
|
||||
# Used by nix-shell -p
|
||||
runCommand =
|
||||
name: args: buildCommand:
|
||||
|
||||
Reference in new issue
Block a user