Skip to content

Commit d05e85e

Browse files
committed
Fix DerivationOptions JSON implementation and test
1 parent 9daef9c commit d05e85e

File tree

11 files changed

+326
-18
lines changed

11 files changed

+326
-18
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"additionalSandboxProfile": "sandcastle",
3+
"allowLocalNetworking": true,
4+
"allowSubstitutes": false,
5+
"exportReferencesGraph": {
6+
"refs1": [
7+
"/164j69y6zir9z0339n8pjigg3rckinlr77bxsavzizdaaljb7nh9"
8+
],
9+
"refs2": [
10+
"/nix/store/qnml92yh97a6fbrs2m5qg5cqlc8vni58-bar.drv"
11+
]
12+
},
13+
"impureEnvVars": [
14+
"UNICORN"
15+
],
16+
"impureHostDeps": [
17+
"/usr/bin/ditto"
18+
],
19+
"noChroot": true,
20+
"outputChecks": {
21+
"forAllOutputs": {
22+
"allowedReferences": [
23+
"/164j69y6zir9z0339n8pjigg3rckinlr77bxsavzizdaaljb7nh9"
24+
],
25+
"allowedRequisites": [
26+
"/0nr45p69vn6izw9446wsh9bng9nndhvn19kpsm4n96a5mycw0s4z"
27+
],
28+
"disallowedReferences": [
29+
"/0nyw57wm2iicnm9rglvjmbci3ikmcp823czdqdzdcgsnnwqps71g"
30+
],
31+
"disallowedRequisites": [
32+
"/07f301yqyz8c6wf6bbbavb2q39j4n8kmcly1s09xadyhgy6x2wr8"
33+
],
34+
"ignoreSelfRefs": true,
35+
"maxClosureSize": null,
36+
"maxSize": null
37+
}
38+
},
39+
"passAsFile": [],
40+
"preferLocalBuild": true,
41+
"requiredSystemFeatures": [
42+
"rainbow",
43+
"uid-range"
44+
],
45+
"unsafeDiscardReferences": {}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"additionalSandboxProfile": "sandcastle",
3+
"allowLocalNetworking": true,
4+
"allowSubstitutes": false,
5+
"exportReferencesGraph": {
6+
"refs1": [
7+
"/164j69y6zir9z0339n8pjigg3rckinlr77bxsavzizdaaljb7nh9"
8+
],
9+
"refs2": [
10+
"/nix/store/qnml92yh97a6fbrs2m5qg5cqlc8vni58-bar.drv"
11+
]
12+
},
13+
"impureEnvVars": [
14+
"UNICORN"
15+
],
16+
"impureHostDeps": [
17+
"/usr/bin/ditto"
18+
],
19+
"noChroot": true,
20+
"outputChecks": {
21+
"perOutput": {
22+
"bin": {
23+
"allowedReferences": null,
24+
"allowedRequisites": null,
25+
"disallowedReferences": [
26+
"/0nyw57wm2iicnm9rglvjmbci3ikmcp823czdqdzdcgsnnwqps71g"
27+
],
28+
"disallowedRequisites": [
29+
"/07f301yqyz8c6wf6bbbavb2q39j4n8kmcly1s09xadyhgy6x2wr8"
30+
],
31+
"ignoreSelfRefs": false,
32+
"maxClosureSize": null,
33+
"maxSize": null
34+
},
35+
"dev": {
36+
"allowedReferences": null,
37+
"allowedRequisites": null,
38+
"disallowedReferences": [],
39+
"disallowedRequisites": [],
40+
"ignoreSelfRefs": false,
41+
"maxClosureSize": 5909,
42+
"maxSize": 789
43+
},
44+
"out": {
45+
"allowedReferences": [
46+
"/164j69y6zir9z0339n8pjigg3rckinlr77bxsavzizdaaljb7nh9"
47+
],
48+
"allowedRequisites": [
49+
"/0nr45p69vn6izw9446wsh9bng9nndhvn19kpsm4n96a5mycw0s4z"
50+
],
51+
"disallowedReferences": [],
52+
"disallowedRequisites": [],
53+
"ignoreSelfRefs": false,
54+
"maxClosureSize": null,
55+
"maxSize": null
56+
}
57+
}
58+
},
59+
"passAsFile": [],
60+
"preferLocalBuild": true,
61+
"requiredSystemFeatures": [
62+
"rainbow",
63+
"uid-range"
64+
],
65+
"unsafeDiscardReferences": {}
66+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"additionalSandboxProfile": "sandcastle",
3+
"allowLocalNetworking": true,
4+
"allowSubstitutes": false,
5+
"exportReferencesGraph": {
6+
"refs1": [
7+
"/nix/store/p0hax2lzvjpfc2gwkk62xdglz0fcqfzn-foo"
8+
],
9+
"refs2": [
10+
"/nix/store/vj2i49jm2868j2fmqvxm70vlzmzvgv14-bar.drv"
11+
]
12+
},
13+
"impureEnvVars": [
14+
"UNICORN"
15+
],
16+
"impureHostDeps": [
17+
"/usr/bin/ditto"
18+
],
19+
"noChroot": true,
20+
"outputChecks": {
21+
"forAllOutputs": {
22+
"allowedReferences": [
23+
"/nix/store/p0hax2lzvjpfc2gwkk62xdglz0fcqfzn-foo"
24+
],
25+
"allowedRequisites": [
26+
"/nix/store/z0rjzy29v9k5qa4nqpykrbzirj7sd43v-foo-dev"
27+
],
28+
"disallowedReferences": [
29+
"/nix/store/r5cff30838majxk5mp3ip2diffi8vpaj-bar"
30+
],
31+
"disallowedRequisites": [
32+
"/nix/store/9b61w26b4avv870dw0ymb6rw4r1hzpws-bar-dev"
33+
],
34+
"ignoreSelfRefs": true,
35+
"maxClosureSize": null,
36+
"maxSize": null
37+
}
38+
},
39+
"passAsFile": [],
40+
"preferLocalBuild": true,
41+
"requiredSystemFeatures": [
42+
"rainbow",
43+
"uid-range"
44+
],
45+
"unsafeDiscardReferences": {}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"additionalSandboxProfile": "",
3+
"allowLocalNetworking": false,
4+
"allowSubstitutes": true,
5+
"exportReferencesGraph": {},
6+
"impureEnvVars": [],
7+
"impureHostDeps": [],
8+
"noChroot": false,
9+
"outputChecks": {
10+
"forAllOutputs": {
11+
"allowedReferences": null,
12+
"allowedRequisites": null,
13+
"disallowedReferences": [],
14+
"disallowedRequisites": [],
15+
"ignoreSelfRefs": true,
16+
"maxClosureSize": null,
17+
"maxSize": null
18+
}
19+
},
20+
"passAsFile": [],
21+
"preferLocalBuild": false,
22+
"requiredSystemFeatures": [],
23+
"unsafeDiscardReferences": {}
24+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"additionalSandboxProfile": "sandcastle",
3+
"allowLocalNetworking": true,
4+
"allowSubstitutes": false,
5+
"exportReferencesGraph": {
6+
"refs1": [
7+
"/nix/store/p0hax2lzvjpfc2gwkk62xdglz0fcqfzn-foo"
8+
],
9+
"refs2": [
10+
"/nix/store/vj2i49jm2868j2fmqvxm70vlzmzvgv14-bar.drv"
11+
]
12+
},
13+
"impureEnvVars": [
14+
"UNICORN"
15+
],
16+
"impureHostDeps": [
17+
"/usr/bin/ditto"
18+
],
19+
"noChroot": true,
20+
"outputChecks": {
21+
"perOutput": {
22+
"bin": {
23+
"allowedReferences": null,
24+
"allowedRequisites": null,
25+
"disallowedReferences": [
26+
"/nix/store/r5cff30838majxk5mp3ip2diffi8vpaj-bar"
27+
],
28+
"disallowedRequisites": [
29+
"/nix/store/9b61w26b4avv870dw0ymb6rw4r1hzpws-bar-dev"
30+
],
31+
"ignoreSelfRefs": false,
32+
"maxClosureSize": null,
33+
"maxSize": null
34+
},
35+
"dev": {
36+
"allowedReferences": null,
37+
"allowedRequisites": null,
38+
"disallowedReferences": [],
39+
"disallowedRequisites": [],
40+
"ignoreSelfRefs": false,
41+
"maxClosureSize": 5909,
42+
"maxSize": 789
43+
},
44+
"out": {
45+
"allowedReferences": [
46+
"/nix/store/p0hax2lzvjpfc2gwkk62xdglz0fcqfzn-foo"
47+
],
48+
"allowedRequisites": [
49+
"/nix/store/z0rjzy29v9k5qa4nqpykrbzirj7sd43v-foo-dev"
50+
],
51+
"disallowedReferences": [],
52+
"disallowedRequisites": [],
53+
"ignoreSelfRefs": false,
54+
"maxClosureSize": null,
55+
"maxSize": null
56+
}
57+
}
58+
},
59+
"passAsFile": [],
60+
"preferLocalBuild": true,
61+
"requiredSystemFeatures": [
62+
"rainbow",
63+
"uid-range"
64+
],
65+
"unsafeDiscardReferences": {}
66+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"additionalSandboxProfile": "",
3+
"allowLocalNetworking": false,
4+
"allowSubstitutes": true,
5+
"exportReferencesGraph": {},
6+
"impureEnvVars": [],
7+
"impureHostDeps": [],
8+
"noChroot": false,
9+
"outputChecks": {
10+
"perOutput": {}
11+
},
12+
"passAsFile": [],
13+
"preferLocalBuild": false,
14+
"requiredSystemFeatures": [],
15+
"unsafeDiscardReferences": {}
16+
}

src/libstore-tests/derivation-advanced-attrs.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#include "nix/util/json-utils.hh"
1111

1212
#include "nix/store/tests/libstore.hh"
13-
#include "nix/util/tests/characterization.hh"
13+
#include "nix/util/tests/json-characterization.hh"
1414

1515
namespace nix {
1616

1717
using namespace nlohmann;
1818

19-
class DerivationAdvancedAttrsTest : public CharacterizationTest, public LibStoreTest
19+
class DerivationAdvancedAttrsTest : public JsonCharacterizationTest<Derivation>,
20+
public JsonCharacterizationTest<DerivationOptions>,
21+
public LibStoreTest
2022
{
2123
protected:
2224
std::filesystem::path unitTestData = getUnitTestData() / "derivation" / "ia";
@@ -454,4 +456,23 @@ TEST_F(CaDerivationAdvancedAttrsTest, advancedAttributes_structuredAttrs)
454456
{"rainbow", "uid-range", "ca-derivations"});
455457
};
456458

459+
#define TEST_JSON_OPTIONS(FIXUTURE, VAR, VAR2) \
460+
TEST_F(FIXUTURE, DerivationOptions_##VAR##_from_json) \
461+
{ \
462+
this->JsonCharacterizationTest<DerivationOptions>::readJsonTest(#VAR, advancedAttributes_##VAR2); \
463+
} \
464+
TEST_F(FIXUTURE, DerivationOptions_##VAR##_to_json) \
465+
{ \
466+
this->JsonCharacterizationTest<DerivationOptions>::writeJsonTest(#VAR, advancedAttributes_##VAR2); \
467+
}
468+
469+
TEST_JSON_OPTIONS(DerivationAdvancedAttrsTest, defaults, defaults)
470+
TEST_JSON_OPTIONS(DerivationAdvancedAttrsTest, all_set, ia)
471+
TEST_JSON_OPTIONS(CaDerivationAdvancedAttrsTest, all_set, ca)
472+
TEST_JSON_OPTIONS(DerivationAdvancedAttrsTest, structuredAttrs_defaults, structuredAttrs_defaults)
473+
TEST_JSON_OPTIONS(DerivationAdvancedAttrsTest, structuredAttrs_all_set, structuredAttrs_ia)
474+
TEST_JSON_OPTIONS(CaDerivationAdvancedAttrsTest, structuredAttrs_all_set, structuredAttrs_ca)
475+
476+
#undef TEST_JSON_OPTIONS
477+
457478
} // namespace nix

src/libstore/derivation-options.cc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,26 @@ DerivationOptions::fromStructuredAttrs(const StringMap & env, const StructuredAt
176176
return {};
177177
};
178178

179-
checks.allowedReferences = get_("allowedReferences");
180-
checks.allowedRequisites = get_("allowedRequisites");
181-
checks.disallowedReferences = get_("disallowedReferences").value_or(StringSet{});
182-
checks.disallowedRequisites = get_("disallowedRequisites").value_or(StringSet{});
183-
;
184-
185-
res.insert_or_assign(outputName, std::move(checks));
179+
res.insert_or_assign(
180+
outputName,
181+
OutputChecks{
182+
.maxSize = [&]() -> std::optional<uint64_t> {
183+
if (auto maxSize = get(output, "maxSize"))
184+
return maxSize->get<uint64_t>();
185+
else
186+
return std::nullopt;
187+
}(),
188+
.maxClosureSize = [&]() -> std::optional<uint64_t> {
189+
if (auto maxClosureSize = get(output, "maxClosureSize"))
190+
return maxClosureSize->get<uint64_t>();
191+
else
192+
return std::nullopt;
193+
}(),
194+
.allowedReferences = get_("allowedReferences"),
195+
.disallowedReferences = get_("disallowedReferences").value_or(StringSet{}),
196+
.allowedRequisites = get_("allowedRequisites"),
197+
.disallowedRequisites = get_("disallowedRequisites").value_or(StringSet{}),
198+
});
186199
}
187200
}
188201
return res;
@@ -364,6 +377,7 @@ DerivationOptions adl_serializer<DerivationOptions>::from_json(const json & json
364377

365378
.unsafeDiscardReferences = valueAt(json, "unsafeDiscardReferences"),
366379
.passAsFile = getStringSet(valueAt(json, "passAsFile")),
380+
.exportReferencesGraph = getMap<StringSet>(getObject(valueAt(json, "exportReferencesGraph")), getStringSet),
367381

368382
.additionalSandboxProfile = getString(valueAt(json, "additionalSandboxProfile")),
369383
.noChroot = getBoolean(valueAt(json, "noChroot")),
@@ -396,6 +410,7 @@ void adl_serializer<DerivationOptions>::to_json(json & json, const DerivationOpt
396410

397411
json["unsafeDiscardReferences"] = o.unsafeDiscardReferences;
398412
json["passAsFile"] = o.passAsFile;
413+
json["exportReferencesGraph"] = o.exportReferencesGraph;
399414

400415
json["additionalSandboxProfile"] = o.additionalSandboxProfile;
401416
json["noChroot"] = o.noChroot;
@@ -423,6 +438,8 @@ DerivationOptions::OutputChecks adl_serializer<DerivationOptions::OutputChecks>:
423438

424439
return {
425440
.ignoreSelfRefs = getBoolean(valueAt(json, "ignoreSelfRefs")),
441+
.maxSize = ptrToOwned<uint64_t>(getNullable(valueAt(json, "maxSize"))),
442+
.maxClosureSize = ptrToOwned<uint64_t>(getNullable(valueAt(json, "maxClosureSize"))),
426443
.allowedReferences = ptrToOwned<StringSet>(getNullable(valueAt(json, "allowedReferences"))),
427444
.disallowedReferences = getStringSet(valueAt(json, "disallowedReferences")),
428445
.allowedRequisites = ptrToOwned<StringSet>(getNullable(valueAt(json, "allowedRequisites"))),
@@ -433,6 +450,8 @@ DerivationOptions::OutputChecks adl_serializer<DerivationOptions::OutputChecks>:
433450
void adl_serializer<DerivationOptions::OutputChecks>::to_json(json & json, const DerivationOptions::OutputChecks & c)
434451
{
435452
json["ignoreSelfRefs"] = c.ignoreSelfRefs;
453+
json["maxSize"] = c.maxSize;
454+
json["maxClosureSize"] = c.maxClosureSize;
436455
json["allowedReferences"] = c.allowedReferences;
437456
json["disallowedReferences"] = c.disallowedReferences;
438457
json["allowedRequisites"] = c.allowedRequisites;

src/libstore/derivations.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ void adl_serializer<Derivation>::to_json(json & res, const Derivation & d)
14041404

14051405
{
14061406
auto & inputsList = res["inputSrcs"];
1407-
inputsList = nlohmann::json ::array();
1407+
inputsList = nlohmann::json::array();
14081408
for (auto & input : d.inputSrcs)
14091409
inputsList.emplace_back(input);
14101410
}

0 commit comments

Comments
 (0)