Skip to content

Commit 87c3451

Browse files
Ericson2314roberth
andcommitted
Create outputOf primop.
In the Nix language, given a drv path, we should be able to construct another string referencing to one of its output. We can do this today with `(import drvPath).output`, but this only works for derivations we already have. With dynamic derivations, however, that doesn't work well because the `drvPath` isn't yet built: importing it like would need to trigger IFD, when the whole point of this feature is to do "dynamic build graph" without IFD! Instead, what we want to do is create a placeholder value with the right string context to refer to the output of the as-yet unbuilt derivation. A new primop in the language, analogous to `builtins.placeholder` can be used to create one. This will achieve all the right properties. The placeholder machinery also will match out the `outPath` attribute for CA derivations works. In 60b7121 we added that type of placeholder, and the derived path and string holder changes necessary to support it. Then in the previous commit we cleaned up the code (inspiration finally hit me!) to deduplicate the code and expose exactly what we need. Now, we can wire up the primop trivally! Part of RFC 92: dynamic derivations (tracking issue NixOS#6316) Co-authored-by: Robert Hensing <[email protected]>
1 parent 55dffce commit 87c3451

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

src/libexpr/primops.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,45 @@ static RegisterPrimOp primop_readDir({
18381838
.fun = prim_readDir,
18391839
});
18401840

1841+
/* Extend single element string context with another output. */
1842+
static void prim_outputOf(EvalState & state, const PosIdx pos, Value * * args, Value & v)
1843+
{
1844+
SingleDerivedPath drvPath = state.coerceToSingleDerivedPath(pos, *args[0], "while evaluating the first argument to builtins.outputOf");
1845+
1846+
std::string_view outputName = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument to builtins.outputOf");
1847+
1848+
state.mkSingleDerivedPathString(
1849+
SingleDerivedPath::Built {
1850+
.drvPath = make_ref<SingleDerivedPath>(drvPath),
1851+
.output = std::string { outputName },
1852+
},
1853+
v);
1854+
}
1855+
1856+
static RegisterPrimOp primop_outputOf({
1857+
.name = "__outputOf",
1858+
.args = {"derivation reference", "output name"},
1859+
.doc = R"(
1860+
Return the output path of a derivation, literally or using a placeholder if needed.
1861+
1862+
If the derivation has a statically-known output path (i.e. the derivation output is input-addressed, or fixed content-addresed), the output path will just be used.
1863+
But if the derivation is content-addressed or if the derivation is itself not-statically produced (i.e. is the output of another derivation), a placeholder will be returned instead.
1864+
1865+
*`derivation reference`* must be a string that may contain a regular store path to a derivation, or may be a placeholder reference. If the derivation is produced by a derivation, you must explicitly select `drv.outPath`.
1866+
This primop can be chained arbitrarily deeply.
1867+
For instance,
1868+
```nix
1869+
builtins.outputOf
1870+
(builtins.outputOf myDrv "out)
1871+
"out"
1872+
```
1873+
will return a placeholder for the output of the output of `myDrv`.
1874+
1875+
This primop corresponds to the `^` sigil for derivable paths, e.g. as part of installable syntax on the command line.
1876+
)",
1877+
.fun = prim_outputOf,
1878+
.experimentalFeature = Xp::DynamicDerivations,
1879+
});
18411880

18421881
/*************************************************************
18431882
* Creating files

tests/dyn-drv/dep-built-drv.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
source common.sh
4+
5+
out1=$(nix-build ./text-hashed-output.nix -A hello --no-out-link)
6+
7+
clearStore
8+
9+
expectStderr 1 nix-build ./text-hashed-output.nix -A wrapper --no-out-link | grepQuiet "Dependencies on the outputs of dynamic derivations are not yet supported"

tests/dyn-drv/eval-outputOf.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
source ./common.sh
4+
5+
# Without the dynamic-derivations XP feature, we don't have the builtin.
6+
nix --experimental-features 'nix-command' eval --impure --expr \
7+
'assert ! (builtins ? outputOf); ""'
8+
9+
# We currently require a string to be passed, rather than a derivation
10+
# object that could be coerced to a string. We might liberalise this in
11+
# the future so it does work, but there are some design questions to
12+
# resolve first. Adding a test so we don't liberalise it by accident.
13+
expectStderr 1 nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \
14+
'builtins.outputOf (import ../dependencies.nix).drvPath "out"' \
15+
"value is a set while a string was expected"
16+
17+
# Test with a regular old input-addresed derivation: works without
18+
# ca-derivations and doesn't create a placeholder but just returns the
19+
# output path.
20+
nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \
21+
'let
22+
item = import ../dependencies.nix;
23+
a = item.outPath;
24+
b = builtins.outputOf (builtins.unsafeDiscardOutputDependency item.drvPath) "out";
25+
in builtins.trace a
26+
(builtins.trace b
27+
(assert a == b; null))'
28+
29+
# Test with content addressed derivation.
30+
nix eval --impure --expr \
31+
'with (import ./text-hashed-output.nix); let
32+
a = hello.outPath;
33+
b = builtins.outputOf (builtins.unsafeDiscardOutputDependency hello.drvPath) "out";
34+
in builtins.trace a
35+
(builtins.trace b
36+
(assert a == b; null))'
37+
38+
# Test with derivation-producing derivation.
39+
nix eval --impure --expr \
40+
'with (import ./text-hashed-output.nix); let
41+
a = producingDrv.outPath;
42+
b = builtins.outputOf (builtins.builtins.unsafeDiscardOutputDependency producingDrv.drvPath) "out";
43+
in builtins.trace a
44+
(builtins.trace b
45+
(assert a == b; null))'
46+
47+
# Test with unbuilt output of derivation-producing derivation.
48+
# This is similar to the previous test, but instead of checking the property on a constant derivation, we check it on a derivation that's from another derivation's output (outPath).
49+
nix eval --impure --expr \
50+
'with (import ./text-hashed-output.nix); let
51+
a = builtins.outputOf producingDrv.out.outPath "out";
52+
b = builtins.outputOf (builtins.outputOf (builtins.unsafeDiscardOutputDependency producingDrv.drvPath) "out") "out";
53+
in builtins.trace a
54+
(builtins.trace b
55+
(assert a == b; null))'

tests/dyn-drv/local.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
dyn-drv-tests := \
22
$(d)/text-hashed-output.sh \
33
$(d)/recursive-mod-json.sh \
4-
$(d)/build-built-drv.sh
4+
$(d)/build-built-drv.sh \
5+
$(d)/eval-outputOf.sh \
6+
$(d)/dep-built-drv.sh
57

68
install-tests-groups += dyn-drv
79

tests/dyn-drv/text-hashed-output.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ rec {
2626
outputHashMode = "text";
2727
outputHashAlgo = "sha256";
2828
};
29+
wrapper = mkDerivation {
30+
name = "use-dynamic-drv-in-ca-drv";
31+
buildCommand = ''
32+
echo "Copying the output of the dynamic derivation"
33+
cp -r ${builtins.outputOf producingDrv.outPath "out"} $out
34+
'';
35+
__contentAddressed = true;
36+
outputHashMode = "recursive";
37+
outputHashAlgo = "sha256";
38+
};
2939
}

0 commit comments

Comments
 (0)