Skip to content

Commit 09a9e08

Browse files
Ericson2314xokdvium
andcommitted
Dedup some derivation initialization logic, and test
`nix derivation add`, and its C API counterpart, now works a bit closer to `builtins.derivation` in that they don't require the user to fill-in input addressed paths correctly ahead of time. The logic for this is carefully deduplicated, between all 3 entry points, and also between the existing `checkInvariants` function. There are some more functional tests, and there are also many more unit tests. Co-authored-by: Sergei Zimmerman <[email protected]>
1 parent d4f34e5 commit 09a9e08

19 files changed

+742
-135
lines changed

src/libexpr/primops.cc

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,28 +1774,7 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
17741774
drv.outputs.insert_or_assign(i, DerivationOutput::Deferred{});
17751775
}
17761776

1777-
auto hashModulo = hashDerivationModulo(*state.store, Derivation(drv), true);
1778-
switch (hashModulo.kind) {
1779-
case DrvHash::Kind::Regular:
1780-
for (auto & i : outputs) {
1781-
auto h = get(hashModulo.hashes, i);
1782-
if (!h)
1783-
state.error<AssertionError>("derivation produced no hash for output '%s'", i).atPos(v).debugThrow();
1784-
auto outPath = state.store->makeOutputPath(i, *h, drvName);
1785-
drv.env[i] = state.store->printStorePath(outPath);
1786-
drv.outputs.insert_or_assign(
1787-
i,
1788-
DerivationOutput::InputAddressed{
1789-
.path = std::move(outPath),
1790-
});
1791-
}
1792-
break;
1793-
;
1794-
case DrvHash::Kind::Deferred:
1795-
for (auto & i : outputs) {
1796-
drv.outputs.insert_or_assign(i, DerivationOutput::Deferred{});
1797-
}
1798-
}
1777+
drv.fillInOutputPaths(*state.store);
17991778
}
18001779

18011780
/* Write the resulting term into the Nix store directory. */

src/libstore-c/nix_api_store.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,7 @@ nix_derivation * nix_derivation_from_json(nix_c_context * context, Store * store
223223
if (context)
224224
context->last_err_code = NIX_OK;
225225
try {
226-
auto drv = static_cast<nix::Derivation>(nlohmann::json::parse(json));
227-
228-
auto drvPath = nix::writeDerivation(*store->ptr, drv, nix::NoRepair, /* read only */ true);
229-
230-
drv.checkInvariants(*store->ptr, drvPath);
231-
232-
return new nix_derivation{drv};
226+
return new nix_derivation{nix::Derivation::parseJsonAndValidate(*store->ptr, nlohmann::json::parse(json))};
233227
}
234228
NIXC_CATCH_ERRS_NULL
235229
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "InputAddressed throws when should be deferred",
6+
"out": ""
7+
},
8+
"inputs": {
9+
"drvs": {
10+
"lg4c4b8r9hlczwprl6kgnzfd9mc1xmkk-dependency.drv": {
11+
"dynamicOutputs": {},
12+
"outputs": [
13+
"out"
14+
]
15+
}
16+
},
17+
"srcs": []
18+
},
19+
"name": "depends-on-drv",
20+
"outputs": {
21+
"out": {
22+
"path": "c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"
23+
}
24+
},
25+
"system": "x86_64-linux",
26+
"version": 4
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Wrong env var value throws error",
6+
"out": "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"
7+
},
8+
"inputs": {
9+
"drvs": {},
10+
"srcs": []
11+
},
12+
"name": "bad-env-var",
13+
"outputs": {
14+
"out": {}
15+
},
16+
"system": "x86_64-linux",
17+
"version": 4
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Wrong InputAddressed path throws error",
6+
"out": "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"
7+
},
8+
"inputs": {
9+
"drvs": {},
10+
"srcs": []
11+
},
12+
"name": "bad-path",
13+
"outputs": {
14+
"out": {
15+
"path": "c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-wrong-name"
16+
}
17+
},
18+
"system": "x86_64-linux",
19+
"version": 4
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Deferred stays deferred with CA dependencies",
6+
"out": ""
7+
},
8+
"inputs": {
9+
"drvs": {
10+
"lg4c4b8r9hlczwprl6kgnzfd9mc1xmkk-dependency.drv": {
11+
"dynamicOutputs": {},
12+
"outputs": [
13+
"out"
14+
]
15+
}
16+
},
17+
"srcs": []
18+
},
19+
"name": "depends-on-drv",
20+
"outputs": {
21+
"out": {}
22+
},
23+
"system": "x86_64-linux",
24+
"version": 4
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Fill in deferred output with empty env var",
6+
"out": "/nix/store/bilpz1nq8qi9r3bzsp72n34yjgqg43ws-filled-in-deferred-empty-env-var"
7+
},
8+
"inputs": {
9+
"drvs": {},
10+
"srcs": []
11+
},
12+
"name": "filled-in-deferred-empty-env-var",
13+
"outputs": {
14+
"out": {
15+
"path": "bilpz1nq8qi9r3bzsp72n34yjgqg43ws-filled-in-deferred-empty-env-var"
16+
}
17+
},
18+
"system": "x86_64-linux",
19+
"version": 4
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Fill in deferred output with empty env var",
6+
"out": ""
7+
},
8+
"inputs": {
9+
"drvs": {},
10+
"srcs": []
11+
},
12+
"name": "filled-in-deferred-empty-env-var",
13+
"outputs": {
14+
"out": {}
15+
},
16+
"system": "x86_64-linux",
17+
"version": 4
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Fill in deferred with missing env var",
6+
"out": "/nix/store/wpk9qrgg77fyswhailap0gicgw98izx9-filled-in-deferred-no-env-var"
7+
},
8+
"inputs": {
9+
"drvs": {},
10+
"srcs": []
11+
},
12+
"name": "filled-in-deferred-no-env-var",
13+
"outputs": {
14+
"out": {
15+
"path": "wpk9qrgg77fyswhailap0gicgw98izx9-filled-in-deferred-no-env-var"
16+
}
17+
},
18+
"system": "x86_64-linux",
19+
"version": 4
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"args": [],
3+
"builder": "/bin/sh",
4+
"env": {
5+
"__doc": "Fill in deferred with missing env var"
6+
},
7+
"inputs": {
8+
"drvs": {},
9+
"srcs": []
10+
},
11+
"name": "filled-in-deferred-no-env-var",
12+
"outputs": {
13+
"out": {}
14+
},
15+
"system": "x86_64-linux",
16+
"version": 4
17+
}

0 commit comments

Comments
 (0)