Skip to content

Commit f996779

Browse files
authored
Merge pull request #70 from DeterminateSystems/gustavderdrache/trace-ifd
Emit warnings when using import-from-derivation by setting the `trace-import-from-derivation` option to `true`
2 parents ea2f882 + 0b66fd3 commit f996779

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/libexpr/include/nix/expr/eval-settings.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ struct EvalSettings : Config
151151
)"
152152
};
153153

154+
Setting<bool> traceImportFromDerivation{
155+
this, false, "trace-import-from-derivation",
156+
R"(
157+
By default, Nix allows [Import from Derivation](@docroot@/language/import-from-derivation.md).
158+
159+
When this setting is `true`, Nix will log a warning indicating that it performed such an import.
160+
This option has no effect if `allow-import-from-derivation` is disabled.
161+
)"
162+
};
163+
154164
Setting<bool> enableImportFromDerivation{
155165
this, true, "allow-import-from-derivation",
156166
R"(

src/libexpr/primops.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,19 @@ StringMap EvalState::realiseContext(const NixStringContext & context, StorePathS
9797

9898
if (drvs.empty()) return {};
9999

100-
if (isIFD && !settings.enableImportFromDerivation)
101-
error<IFDError>(
102-
"cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled",
103-
drvs.begin()->to_string(*store)
104-
).debugThrow();
100+
if (isIFD) {
101+
if (!settings.enableImportFromDerivation)
102+
error<IFDError>(
103+
"cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled",
104+
drvs.begin()->to_string(*store)
105+
).debugThrow();
106+
107+
if (settings.traceImportFromDerivation)
108+
warn(
109+
"built '%1%' during evaluation due to an import from derivation",
110+
drvs.begin()->to_string(*store)
111+
);
112+
}
105113

106114
/* Build/substitute the context. */
107115
std::vector<DerivedPath> buildReqs;

tests/functional/flakes/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ suites += {
3333
'debugger.sh',
3434
'source-paths.sh',
3535
'old-lockfiles.sh',
36+
'trace-ifd.sh',
3637
],
3738
'workdir': meson.current_source_dir(),
3839
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
source ./common.sh
4+
5+
requireGit
6+
7+
flake1Dir="$TEST_ROOT/flake"
8+
9+
createGitRepo "$flake1Dir"
10+
createSimpleGitFlake "$flake1Dir"
11+
12+
cat > "$flake1Dir/flake.nix" <<'EOF'
13+
{
14+
outputs = { self }: let inherit (import ./config.nix) mkDerivation; in {
15+
drv = mkDerivation {
16+
name = "drv";
17+
buildCommand = ''
18+
echo drv >$out
19+
'';
20+
};
21+
22+
ifd = mkDerivation {
23+
name = "ifd";
24+
buildCommand = ''
25+
echo ${builtins.readFile self.drv} >$out
26+
'';
27+
};
28+
};
29+
}
30+
EOF
31+
32+
nix build "$flake1Dir#ifd" --option trace-import-from-derivation true 2>&1 \
33+
| grepQuiet 'warning: built .* during evaluation due to an import from derivation'

0 commit comments

Comments
 (0)