diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index 82ea0863a70..452a80eadc7 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -370,7 +370,7 @@ substitute() {
         content="${content//"$pattern"/$replacement}"
     done
 
-    chmod -f +w "$output" || true
+    if [ -e "$output" ]; then chmod +w "$output".tmp; fi
     printf "%s" "$content" > "$output"
 }