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