2024-07-05 10:58:55 +01:00
|
|
|
{
|
|
|
|
lib,
|
2024-07-05 11:49:55 +01:00
|
|
|
writeTextFile,
|
2024-07-05 10:58:55 +01:00
|
|
|
}:
|
2024-06-29 16:07:17 +01:00
|
|
|
let
|
|
|
|
inherit (builtins) typeOf;
|
|
|
|
in
|
|
|
|
rec {
|
2024-07-05 11:33:57 +01:00
|
|
|
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
|
|
|
|
# Tests: ./tests/default.nix
|
2024-06-29 16:07:17 +01:00
|
|
|
# This function closely mirrors what this Nix code does:
|
|
|
|
# https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/primops.cc#L1102
|
|
|
|
# https://github.com/NixOS/nix/blob/2.8.0/src/libexpr/eval.cc#L1981-L2036
|
2024-06-29 16:22:57 +01:00
|
|
|
valueToString = value:
|
2024-06-29 16:07:17 +01:00
|
|
|
# We can't just use `toString` on all derivation attributes because that
|
|
|
|
# would not put path literals in the closure. So we explicitly copy
|
|
|
|
# those into the store here
|
|
|
|
if typeOf value == "path" then "${value}"
|
2024-06-29 16:22:57 +01:00
|
|
|
else if typeOf value == "list" then toString (map valueToString value)
|
2024-06-29 16:07:17 +01:00
|
|
|
else toString value;
|
2024-07-05 10:58:55 +01:00
|
|
|
|
|
|
|
|
2024-07-05 11:33:57 +01:00
|
|
|
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
|
|
|
|
# Tests: ./tests/default.nix
|
2024-07-05 10:58:55 +01:00
|
|
|
# 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 [])
|
2024-07-05 11:49:55 +01:00
|
|
|
then
|
|
|
|
let
|
2024-07-05 12:35:31 +01:00
|
|
|
nameHash =
|
|
|
|
if builtins?convertHash
|
|
|
|
then builtins.convertHash {
|
|
|
|
hash = "sha256:" + builtins.hashString "sha256" name;
|
|
|
|
toHashFormat = "nix32";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
builtins.hashString "sha256" name;
|
2024-07-05 11:49:55 +01:00
|
|
|
basename = ".attr-${nameHash}";
|
|
|
|
in
|
|
|
|
lib.nameValuePair "${name}Path" "${
|
|
|
|
writeTextFile {
|
|
|
|
name = "shell-passAsFile-${name}";
|
|
|
|
text = str;
|
|
|
|
destination = "/${basename}";
|
|
|
|
}
|
|
|
|
}/${basename}"
|
2024-07-05 10:58:55 +01:00
|
|
|
else lib.nameValuePair name str
|
|
|
|
)
|
2024-07-05 11:17:38 +01:00
|
|
|
(removeAttrs drvAttrs [
|
2024-07-05 11:33:57 +01:00
|
|
|
# TODO: there may be more of these
|
2024-07-05 11:17:38 +01:00
|
|
|
"args"
|
|
|
|
]);
|
2024-07-05 10:58:55 +01:00
|
|
|
|
2024-07-05 11:33:57 +01:00
|
|
|
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
|
|
|
|
# Tests: ./tests/default.nix
|
2024-07-05 10:58:55 +01:00
|
|
|
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);
|
|
|
|
|
2024-06-29 15:25:25 +01:00
|
|
|
}
|