1{
2 lib,
3 writeTextFile,
4}:
5let
6 inherit (builtins) typeOf;
7in
8rec {
9 # Docs: doc/build-helpers/dev-shell-tools.chapter.md
10 # Tests: ./tests/default.nix
11 # This function closely mirrors what this Nix code does:
12 # https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1102
13 # https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/eval.cc#L1981-L2036
14 valueToString = value:
15 # We can't just use `toString` on all derivation attributes because that
16 # would not put path literals in the closure. So we explicitly copy
17 # those into the store here
18 if typeOf value == "path" then "${value}"
19 else if typeOf value == "list" then toString (map valueToString value)
20 else toString value;
21
22
23 # Docs: doc/build-helpers/dev-shell-tools.chapter.md
24 # Tests: ./tests/default.nix
25 # https://github.com/NixOS/nix/blob/2.8.0/src/libstore/build/local-derivation-goal.cc#L992-L1004
26 unstructuredDerivationInputEnv = { drvAttrs }:
27 # FIXME: this should be `normalAttrs // passAsFileAttrs`
28 lib.mapAttrs'
29 (name: value:
30 let str = valueToString value;
31 in if lib.elem name (drvAttrs.passAsFile or [])
32 then
33 let
34 nameHash =
35 if builtins?convertHash
36 then builtins.convertHash {
37 hash = "sha256:" + builtins.hashString "sha256" name;
38 toHashFormat = "nix32";
39 }
40 else
41 builtins.hashString "sha256" name;
42 basename = ".attr-${nameHash}";
43 in
44 lib.nameValuePair "${name}Path" "${
45 writeTextFile {
46 name = "shell-passAsFile-${name}";
47 text = str;
48 destination = "/${basename}";
49 }
50 }/${basename}"
51 else lib.nameValuePair name str
52 )
53 (removeAttrs drvAttrs [
54 # TODO: there may be more of these
55 "args"
56 ]);
57
58 # Docs: doc/build-helpers/dev-shell-tools.chapter.md
59 # Tests: ./tests/default.nix
60 derivationOutputEnv = { outputList, outputMap }:
61 # A mapping from output name to the nix store path where they should end up
62 # https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1253
63 lib.genAttrs outputList (output: builtins.unsafeDiscardStringContext outputMap.${output}.outPath);
64
65}