From 43df2b50f94cd7528fd1d20c552140aeeb3cbf21 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 5 Jul 2024 11:58:55 +0200 Subject: [PATCH] devShellTools.{unstructuredDerivationInputEnv,derivationOutputEnv}: extract --- .../build-support/dev-shell-tools/default.nix | 25 ++++++++++++++++++- pkgs/build-support/docker/default.nix | 13 +++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/pkgs/build-support/dev-shell-tools/default.nix b/pkgs/build-support/dev-shell-tools/default.nix index cd5fa5f5937e..87432b42f360 100644 --- a/pkgs/build-support/dev-shell-tools/default.nix +++ b/pkgs/build-support/dev-shell-tools/default.nix @@ -1,4 +1,7 @@ -{ lib }: +{ + lib, + writeText, +}: let inherit (builtins) typeOf; in @@ -13,4 +16,24 @@ rec { if typeOf value == "path" then "${value}" else if typeOf value == "list" then toString (map valueToString value) else toString value; + + + # https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L992-L1004 + unstructuredDerivationInputEnv = { drvAttrs }: + # FIXME: this should be `normalAttrs // passAsFileAttrs` + lib.mapAttrs' + (name: value: + let str = valueToString value; + in if lib.elem name (drvAttrs.passAsFile or []) + then lib.nameValuePair "${name}Path" (writeText "pass-as-text-${name}" str) + else lib.nameValuePair name str + ) + drvAttrs; + + derivationOutputEnv = { outputList, outputMap }: + # A mapping from output name to the nix store path where they should end up + # https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1253 + lib.genAttrs outputList (output: builtins.unsafeDiscardStringContext outputMap.${output}.outPath); + + } diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 3c580fafe974..efc5054be887 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -1190,16 +1190,9 @@ rec { # https://github.com/NixOS/nix/blob/2.8.0/src/libstore/globals.hh#L464-L465 sandboxBuildDir = "/build"; - # https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L992-L1004 - drvEnv = lib.mapAttrs' (name: value: - let str = valueToString value; - in if lib.elem name (drv.drvAttrs.passAsFile or []) - then lib.nameValuePair "${name}Path" (writeText "pass-as-text-${name}" str) - else lib.nameValuePair name str - ) drv.drvAttrs // - # A mapping from output name to the nix store path where they should end up - # https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1253 - lib.genAttrs drv.outputs (output: builtins.unsafeDiscardStringContext drv.${output}.outPath); + drvEnv = + devShellTools.unstructuredDerivationInputEnv { inherit (drv) drvAttrs; } + // devShellTools.derivationOutputEnv { outputList = drv.outputs; outputMap = drv; }; # Environment variables set in the image envVars = {