diff --git a/src/libexpr/eval-error.cc b/src/libexpr/eval-error.cc index cdb0b4772..145d29609 100644 --- a/src/libexpr/eval-error.cc +++ b/src/libexpr/eval-error.cc @@ -110,5 +110,6 @@ template class EvalErrorBuilder; template class EvalErrorBuilder; template class EvalErrorBuilder; template class EvalErrorBuilder; +template class EvalErrorBuilder; } diff --git a/src/libexpr/eval-error.hh b/src/libexpr/eval-error.hh index ed004eb53..b4e5007b0 100644 --- a/src/libexpr/eval-error.hh +++ b/src/libexpr/eval-error.hh @@ -54,6 +54,7 @@ MakeError(TypeError, EvalError); MakeError(UndefinedVarError, EvalError); MakeError(MissingArgumentError, EvalError); MakeError(InfiniteRecursionError, EvalError); +MakeError(IFDError, EvalBaseError); struct InvalidPathError : public EvalError { diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index a2ea029ea..ed794540b 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -91,7 +91,7 @@ StringMap EvalState::realiseContext(const NixStringContext & context, StorePathS if (drvs.empty()) return {}; if (isIFD && !settings.enableImportFromDerivation) - error( + error( "cannot build '%1%' during evaluation because the option 'allow-import-from-derivation' is disabled", drvs.begin()->to_string(*store) ).debugThrow(); diff --git a/src/nix/flake.cc b/src/nix/flake.cc index e2099c401..ca87773f3 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1329,18 +1329,34 @@ struct CmdFlakeShow : FlakeCommand, MixJSON logger->warn(fmt("%s omitted (use '--all-systems' to show)", concatStringsSep(".", attrPathS))); } } else { - if (visitor.isDerivation()) - showDerivation(); - else - throw Error("expected a derivation"); + try { + if (visitor.isDerivation()) + showDerivation(); + else + throw Error("expected a derivation"); + } catch (IFDError & e) { + if (!json) { + logger->cout(fmt("%s " ANSI_WARNING "omitted due to use of import from derivation" ANSI_NORMAL, headerPrefix)); + } else { + logger->warn(fmt("%s omitted due to use of import from derivation", concatStringsSep(".", attrPathS))); + } + } } } else if (attrPath.size() > 0 && attrPathS[0] == "hydraJobs") { - if (visitor.isDerivation()) - showDerivation(); - else - recurse(); + try { + if (visitor.isDerivation()) + showDerivation(); + else + recurse(); + } catch (IFDError & e) { + if (!json) { + logger->cout(fmt("%s " ANSI_WARNING "omitted due to use of import from derivation" ANSI_NORMAL, headerPrefix)); + } else { + logger->warn(fmt("%s omitted due to use of import from derivation", concatStringsSep(".", attrPathS))); + } + } } else if (attrPath.size() > 0 && attrPathS[0] == "legacyPackages") { @@ -1359,11 +1375,19 @@ struct CmdFlakeShow : FlakeCommand, MixJSON logger->warn(fmt("%s omitted (use '--all-systems' to show)", concatStringsSep(".", attrPathS))); } } else { - if (visitor.isDerivation()) - showDerivation(); - else if (attrPath.size() <= 2) - // FIXME: handle recurseIntoAttrs - recurse(); + try { + if (visitor.isDerivation()) + showDerivation(); + else if (attrPath.size() <= 2) + // FIXME: handle recurseIntoAttrs + recurse(); + } catch (IFDError & e) { + if (!json) { + logger->cout(fmt("%s " ANSI_WARNING "omitted due to use of import from derivation" ANSI_NORMAL, headerPrefix)); + } else { + logger->warn(fmt("%s omitted due to use of import from derivation", concatStringsSep(".", attrPathS))); + } + } } } diff --git a/tests/functional/flakes/common.sh b/tests/functional/flakes/common.sh index 06e414e9d..422cab96c 100644 --- a/tests/functional/flakes/common.sh +++ b/tests/functional/flakes/common.sh @@ -88,6 +88,19 @@ writeDependentFlake() { EOF } +writeIfdFlake() { + local flakeDir="$1" + cat > "$flakeDir/flake.nix" < "$flakeDir/flake.nix" < show-output.json +nix eval --impure --expr ' +let show_output = builtins.fromJSON (builtins.readFile ./show-output.json); +in +assert show_output.packages.${builtins.currentSystem}.default == { }; +true +'