1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00
This commit is contained in:
Théophane Hufschmitt 2025-06-13 05:04:43 +00:00 committed by GitHub
commit fde5294e1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 0 deletions

View file

@ -62,6 +62,7 @@ struct BuildEnvironment
std::map<std::string, Value> vars; std::map<std::string, Value> vars;
StringMap bashFunctions; StringMap bashFunctions;
std::optional<std::pair<std::string, std::string>> structuredAttrs; std::optional<std::pair<std::string, std::string>> structuredAttrs;
std::optional<int> minimalBashVersion;
static BuildEnvironment fromJSON(const nlohmann::json & json) static BuildEnvironment fromJSON(const nlohmann::json & json)
{ {
@ -87,6 +88,10 @@ struct BuildEnvironment
res.structuredAttrs = {json["structuredAttrs"][".attrs.json"], json["structuredAttrs"][".attrs.sh"]}; res.structuredAttrs = {json["structuredAttrs"][".attrs.json"], json["structuredAttrs"][".attrs.sh"]};
} }
if (json.contains("meta") && json["meta"].contains("min-bash-version")) {
res.minimalBashVersion = std::optional<int>{ json["meta"]["min-bash-version"] };
}
return res; return res;
} }
@ -153,6 +158,18 @@ struct BuildEnvironment
void toBash(std::ostream & out, const StringSet & ignoreVars) const void toBash(std::ostream & out, const StringSet & ignoreVars) const
{ {
if (minimalBashVersion.has_value()) {
out << stripIndentation(fmt(R"__NIX_STR(
if [[ -n "${BASH_VERSINFO-}" && "${BASH_VERSINFO-}" -lt %d ]]; then
echo "Detected Bash version that isn't supported by this derivation (${BASH_VERSION})"
echo "Please install Bash %d or greater to continue."
exit 1
fi
)__NIX_STR",
minimalBashVersion.value(),
minimalBashVersion.value()));
}
for (auto & [name, value] : vars) { for (auto & [name, value] : vars) {
if (!ignoreVars.count(name)) { if (!ignoreVars.count(name)) {
if (auto str = std::get_if<String>(&value)) { if (auto str = std::get_if<String>(&value)) {

View file

@ -115,6 +115,16 @@ __dumpEnv() {
printf '\n }' printf '\n }'
fi fi
printf ',\n "meta": {\n '
# min-bash-version is supposed to be an integer.
# If not, just ignore it.
if [[ -n "${NIX_ENV_MIN_BASH_VERSION-}" && "$NIX_ENV_MIN_BASH_VERSION" -eq "$NIX_ENV_MIN_BASH_VERSION" ]]; then
__escapeString "min-bash-version"
printf ': '
printf '%d' "${NIX_ENV_MIN_BASH_VERSION}"
fi
printf '\n }'
printf '\n}' printf '\n}'
} }

View file

@ -143,3 +143,20 @@ EOF
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]] )" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
clearStore clearStore
# Use a newer bash version than the current one
cat <<EOF >$TEST_HOME/flake.nix
{
inputs.nixpkgs.url = "$TEST_HOME/nixpkgs";
outputs = {self, nixpkgs}: {
packages.$system.hello = (import ./config.nix).mkDerivation {
name = "hello";
outputs = [ "out" "dev" ];
meta.outputsToInstall = [ "out" ];
buildCommand = "";
NIX_ENV_MIN_BASH_VERSION=99;
};
};
}
EOF
expect 1 nix develop .#hello --command true