diff --git a/src/libstore/builtins/fetchurl.cc b/src/libstore/builtins/fetchurl.cc index 4fb67f933..b9dfeba2f 100644 --- a/src/libstore/builtins/fetchurl.cc +++ b/src/libstore/builtins/fetchurl.cc @@ -23,9 +23,8 @@ void builtinFetchurl( if (!out) throw Error("'builtin:fetchurl' requires an 'out' output"); - auto dof = std::get_if(&out->raw); - if (!dof) - throw Error("'builtin:fetchurl' must be a fixed-output derivation"); + if (!(drv.type().isFixed() || drv.type().isImpure())) + throw Error("'builtin:fetchurl' must be a fixed-output or impure derivation"); auto storePath = outputs.at("out"); auto mainUrl = drv.env.at("url"); @@ -64,7 +63,8 @@ void builtinFetchurl( }; /* Try the hashed mirrors first. */ - if (dof->ca.method.getFileIngestionMethod() == FileIngestionMethod::Flat) + auto dof = std::get_if(&out->raw); + if (dof && dof->ca.method.getFileIngestionMethod() == FileIngestionMethod::Flat) for (auto hashedMirror : settings.hashedMirrors.get()) try { if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/'; diff --git a/tests/functional/fetchurl.sh b/tests/functional/fetchurl.sh index 5a05cc5e1..a3620f52b 100644 --- a/tests/functional/fetchurl.sh +++ b/tests/functional/fetchurl.sh @@ -83,4 +83,4 @@ test -L $outPath/symlink requireDaemonNewerThan "2.20" expected=100 if [[ -v NIX_DAEMON_PACKAGE ]]; then expected=1; fi # work around the daemon not returning a 100 status correctly -expectStderr $expected nix-build --expr '{ url }: builtins.derivation { name = "nix-cache-info"; system = "x86_64-linux"; builder = "builtin:fetchurl"; inherit url; outputHashMode = "flat"; }' --argstr url file://$narxz 2>&1 | grep 'must be a fixed-output derivation' +expectStderr $expected nix-build --expr '{ url }: builtins.derivation { name = "nix-cache-info"; system = "x86_64-linux"; builder = "builtin:fetchurl"; inherit url; outputHashMode = "flat"; }' --argstr url file://$narxz 2>&1 | grep 'must be a fixed-output or impure derivation' diff --git a/tests/functional/impure-derivations.sh b/tests/functional/impure-derivations.sh index 39d053a04..54ed6f5dd 100644 --- a/tests/functional/impure-derivations.sh +++ b/tests/functional/impure-derivations.sh @@ -63,3 +63,7 @@ path5=$(nix build -L --no-link --json --file ./impure-derivations.nix contentAdd path6=$(nix build -L --no-link --json --file ./impure-derivations.nix inputAddressedAfterCA | jq -r .[].outputs.out) [[ $(< $path6) = X ]] [[ $(< $TEST_ROOT/counter) = 5 ]] + +# Test nix/fetchurl.nix. +path7=$(nix build -L --no-link --print-out-paths --expr "import { impure = true; url = file://$PWD/impure-derivations.sh; }") +cmp $path7 $PWD/impure-derivations.sh