diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index 523d737d9bf8f5fa3373235e6c1505b049118c72..55066205db3a57827fe608268b97a9a65007bf55 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -2933,8 +2933,13 @@ void DerivationGoal::runChild()
         if (drv->isBuiltin()) {
             try {
                 logger = makeJSONLogger(*logger);
+
+                BasicDerivation drv2(*drv);
+                for (auto & e : drv2.env)
+                    e.second = rewriteStrings(e.second, inputRewrites);
+
                 if (drv->builder == "builtin:fetchurl")
-                    builtinFetchurl(*drv, netrcData);
+                    builtinFetchurl(drv2, netrcData);
                 else
                     throw Error(format("unsupported builtin function '%1%'") % string(drv->builder, 8));
                 _exit(0);
diff --git a/tests/check.nix b/tests/check.nix
index b330ab9c984c3a8d153bbb729b3a8722bb3c024b..585d43032a91b459b2314cbb6beaecfa99ad9b6a 100644
--- a/tests/check.nix
+++ b/tests/check.nix
@@ -9,4 +9,9 @@ with import ./config.nix;
         date +%s.%N > $out/date
       '';
   };
+
+  fetchurl = import <nix/fetchurl.nix> {
+    url = "file://" + toString ./lang/eval-okay-xml.out;
+    sha256 = "426fefcd2430e986551db13fcc2b1e45eeec17e68ffeb6ff155be2f8aaf5407e";
+  };
 }
diff --git a/tests/check.sh b/tests/check.sh
index 3efcef04492138792c30fba573910dd763400fa3..a3067e06f1490e70f39e810c9676e8d16cde7720 100644
--- a/tests/check.sh
+++ b/tests/check.sh
@@ -16,3 +16,17 @@ nix-build dependencies.nix --no-out-link --repeat 3
 (! nix-build check.nix -A nondeterministic --no-out-link --repeat 1 2> $TEST_ROOT/log)
 grep 'differs from previous round' $TEST_ROOT/log
 
+path=$(nix-build check.nix -A fetchurl --no-out-link)
+
+chmod +w $path
+echo foo > $path
+chmod -w $path
+
+nix-build check.nix -A fetchurl --no-out-link --check
+
+# Note: "check" doesn't repair anything, it just compares to the hash stored in the database.
+[[ $(cat $path) = foo ]]
+
+nix-build check.nix -A fetchurl --no-out-link --repair
+
+[[ $(cat $path) != foo ]]