push sheeet
Some checks failed
Periodic Merges (6h) / master → staging-nixos (push) Failing after 12m50s
Periodic Merges (6h) / master → staging-next (push) Failing after 12m54s
Periodic Merges (24h) / merge-base(master,staging) → haskell-updates (push) Failing after 11m54s
Periodic Merges (6h) / staging-next → staging (push) Failing after 12m13s
Periodic Merges (24h) / staging-next-25.05 → staging-25.05 (push) Failing after 13m24s
Periodic Merges (24h) / release-25.05 → staging-next-25.05 (push) Failing after 14m28s

This commit is contained in:
Dark Steveneq
2025-10-09 14:15:47 +02:00
commit 646b892680
49168 changed files with 5897842 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# `devShellTools`
This directory implements the `pkgs.devShellTools` library.
# Contributing to `devShellTools`
- Documentation should be contributed to the Nixpkgs manual, not here.
- Tests are available in the `tests` directory.
You may run them with `nix-build -A tests.devShellTools`.
- See [../../README.md](../../README.md) for more information on contributing to Nixpkgs.

View File

@@ -0,0 +1,75 @@
{
lib,
writeTextFile,
}:
let
inherit (builtins) typeOf;
in
rec {
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
# Tests: ./tests/default.nix
# 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
valueToString =
value:
# 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}"
else if typeOf value == "list" then
toString (map valueToString value)
else
toString value;
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
# Tests: ./tests/default.nix
# 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
let
nameHash =
if builtins ? convertHash then
builtins.convertHash {
hash = "sha256:" + builtins.hashString "sha256" name;
toHashFormat = "nix32";
}
else
builtins.hashString "sha256" name;
basename = ".attr-${nameHash}";
in
lib.nameValuePair "${name}Path" "${
writeTextFile {
name = "shell-passAsFile-${name}";
text = str;
destination = "/${basename}";
}
}/${basename}"
else
lib.nameValuePair name str
)
(
removeAttrs drvAttrs [
# TODO: there may be more of these
"args"
]
);
# Docs: doc/build-helpers/dev-shell-tools.chapter.md
# Tests: ./tests/default.nix
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);
}

View File

@@ -0,0 +1,190 @@
{
devShellTools,
emptyFile,
lib,
stdenv,
hello,
writeText,
zlib,
nixosTests,
}:
let
inherit (lib)
concatLines
escapeShellArg
isString
mapAttrsToList
;
in
lib.recurseIntoAttrs {
# nix-build -A tests.devShellTools.nixos
nixos = nixosTests.docker-tools-nix-shell;
# nix-build -A tests.devShellTools.valueToString
valueToString =
let
inherit (devShellTools) valueToString;
in
stdenv.mkDerivation {
name = "devShellTools-valueToString-built-tests";
# Test inputs
inherit emptyFile hello;
one = 1;
boolTrue = true;
boolFalse = false;
foo = "foo";
list = [
1
2
3
];
pathDefaultNix = ./default.nix;
packages = [
hello
emptyFile
];
# TODO: nested lists
buildCommand = ''
touch $out
( set -x
[[ "$one" = ${escapeShellArg (valueToString 1)} ]]
[[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]]
[[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]]
[[ "$foo" = ${escapeShellArg (valueToString "foo")} ]]
[[ "$hello" = ${escapeShellArg (valueToString hello)} ]]
[[ "$list" = ${
escapeShellArg (valueToString [
1
2
3
])
} ]]
[[ "$packages" = ${
escapeShellArg (valueToString [
hello
emptyFile
])
} ]]
[[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]]
[[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]]
) >log 2>&1 || { cat log; exit 1; }
'';
};
# nix-build -A tests.devShellTools.valueToString
unstructuredDerivationInputEnv =
let
inherit (devShellTools) unstructuredDerivationInputEnv;
drvAttrs = {
one = 1;
boolTrue = true;
boolFalse = false;
foo = "foo";
list = [
1
2
3
];
pathDefaultNix = ./default.nix;
stringWithDep = "Exe: ${hello}/bin/hello";
aPackageAttrSet = hello;
anOutPath = hello.outPath;
anAnAlternateOutput = zlib.dev;
args = [
"args must not be added to the environment"
"Nix doesn't do it."
];
passAsFile = [ "bar" ];
bar = ''
bar
${writeText "qux" "yea"}
'';
};
result = unstructuredDerivationInputEnv { inherit drvAttrs; };
in
assert
result // { barPath = "<check later>"; } == {
one = "1";
boolTrue = "1";
boolFalse = "";
foo = "foo";
list = "1 2 3";
pathDefaultNix = "${./default.nix}";
stringWithDep = "Exe: ${hello}/bin/hello";
aPackageAttrSet = "${hello}";
anOutPath = "${hello.outPath}";
anAnAlternateOutput = "${zlib.dev}";
passAsFile = "bar";
barPath = "<check later>";
};
# Not runCommand, because it alters `passAsFile`
stdenv.mkDerivation (
{
name = "devShellTools-unstructuredDerivationInputEnv-built-tests";
exampleBarPathString =
assert isString result.barPath;
result.barPath;
dontUnpack = true;
dontBuild = true;
dontFixup = true;
doCheck = true;
installPhase = "touch $out";
checkPhase = ''
fail() {
echo "$@" >&2
exit 1
}
checkAttr() {
echo checking attribute $1...
if [[ "$2" != "$3" ]]; then
echo "expected: $3"
echo "actual: $2"
exit 1
fi
}
${concatLines (
mapAttrsToList (name: value: "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}") (
removeAttrs result [
"args"
# Nix puts it in workdir, which is not a concept for
# unstructuredDerivationInputEnv, so we have to put it in the
# store instead. This means the full path won't match.
"barPath"
]
)
)}
(
set -x
diff $exampleBarPathString $barPath
${lib.optionalString (builtins ? convertHash) ''
[[ "$(basename $exampleBarPathString)" = "$(basename $barPath)" ]]
''}
)
''${args:+fail "args should not be set by Nix. We don't expect it to and unstructuredDerivationInputEnv removes it."}
if [[ "''${builder:-x}" == x ]]; then
fail "builder should be set by Nix. We don't remove it in unstructuredDerivationInputEnv."
fi
'';
}
// removeAttrs drvAttrs [
# This would break the derivation. Instead, we have a check in the derivation to make sure Nix doesn't set it.
"args"
]
);
}