summary refs log tree commit diff
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2022-12-13 18:12:04 +0100
committerNaïm Favier <n@monade.li>2022-12-15 13:27:11 +0100
commite14de22618dc6c547f7c89f57eefe98e9e0bb8c4 (patch)
tree253336a7c9e450554a2cbabc414e496c6f3a2197
parent2a740527d6e6b139fa47adb40dce43dc3c371fd3 (diff)
downloadnixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar.gz
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar.bz2
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar.lz
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar.xz
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.tar.zst
nixpkgs-e14de22618dc6c547f7c89f57eefe98e9e0bb8c4.zip
stdenv: handle `env` gracefully
Derivations not using `__structuredAttrs` should not attempt to set
environment variables from `env`.

Derivations using `__structuredAttrs` should fail if `env` is not
exportable.
-rw-r--r--pkgs/stdenv/generic/make-derivation.nix6
-rw-r--r--pkgs/stdenv/generic/setup.sh11
-rw-r--r--pkgs/test/stdenv/default.nix19
3 files changed, 30 insertions, 6 deletions
diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix
index 12aa25ac307..db6d7e83f59 100644
--- a/pkgs/stdenv/generic/make-derivation.nix
+++ b/pkgs/stdenv/generic/make-derivation.nix
@@ -274,7 +274,7 @@ else let
        "__darwinAllowLocalNetworking"
        "__impureHostDeps" "__propagatedImpureHostDeps"
        "sandboxProfile" "propagatedSandboxProfile"]
-       ++ lib.optionals envIsExportable [ "env" ]))
+       ++ lib.optional (__structuredAttrs || envIsExportable) "env"))
     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
       name =
         let
@@ -298,7 +298,7 @@ else let
           then attrs.name + hostSuffix
           else "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}"
         );
-    }) // lib.optionalAttrs (envIsExportable && __structuredAttrs) { env = checkedEnv; } // {
+    }) // lib.optionalAttrs __structuredAttrs { env = checkedEnv; } // {
       builder = attrs.realBuilder or stdenv.shell;
       args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
       inherit stdenv;
@@ -485,6 +485,8 @@ else let
     let
       overlappingNames = lib.intersectLists (lib.attrNames env) (lib.attrNames derivationArg);
     in
+    assert lib.assertMsg envIsExportable
+      "When using structured attributes, `env` must be an attribute set of environment variables.";
     assert lib.assertMsg (overlappingNames == [ ])
       "The ‘env’ attribute set cannot contain any attributes passed to derivation. The following attributes are overlapping: ${lib.concatStringsSep ", " overlappingNames}";
     lib.mapAttrs
diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index 5a64625e10d..b07420bb418 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -383,10 +383,13 @@ printWords() {
 ######################################################################
 # Initialisation.
 
-# export all vars that should be in the ENV
-for envVar in "${!env[@]}"; do
-    declare -x "${envVar}=${env[${envVar}]}"
-done
+# If using structured attributes, export variables from `env` to the environment.
+# When not using structured attributes, those variables are already exported.
+if [[ -n $__structuredAttrs ]]; then
+    for envVar in "${!env[@]}"; do
+        declare -x "${envVar}=${env[${envVar}]}"
+    done
+fi
 
 
 # Set a fallback default value for SOURCE_DATE_EPOCH, used by some build tools
diff --git a/pkgs/test/stdenv/default.nix b/pkgs/test/stdenv/default.nix
index 08e8eed118f..ab5f98890bd 100644
--- a/pkgs/test/stdenv/default.nix
+++ b/pkgs/test/stdenv/default.nix
@@ -95,6 +95,25 @@ in
 {
   test-env-attrset = testEnvAttrset { name = "test-env-attrset"; stdenv' = bootStdenv; };
 
+  # Test compatibility with derivations using `env` as a regular variable.
+  test-env-derivation = bootStdenv.mkDerivation rec {
+    name = "test-env-derivation";
+    env = bootStdenv.mkDerivation {
+      name = "foo";
+      buildCommand = ''
+        mkdir "$out"
+        touch "$out/bar"
+      '';
+    };
+
+    passAsFile = [ "buildCommand" ];
+    buildCommand = ''
+      declare -p env
+      [[ $env == "${env}" ]]
+      touch "$out"
+    '';
+  };
+
   test-prepend-append-to-var = testPrependAndAppendToVar {
     name = "test-prepend-append-to-var";
     stdenv' = bootStdenv;