1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 01:51:47 +02:00

Merge pull request #11058 from hercules-ci/more-nix-shell

Make `#!nix-shell` arguments and options relative to script
This commit is contained in:
Eelco Dolstra 2024-07-17 21:52:34 +02:00 committed by GitHub
commit 1e1a8e8ad1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 133 additions and 3 deletions

View file

@ -214,7 +214,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
auto v = state.allocValue();
std::visit(overloaded {
[&](const AutoArgExpr & arg) {
state.mkThunk_(*v, state.parseExprFromString(arg.expr, state.rootPath(".")));
state.mkThunk_(*v, state.parseExprFromString(arg.expr, compatibilitySettings.nixShellShebangArgumentsRelativeToScript ? state.rootPath(absPath(getCommandBaseDir())) : state.rootPath(".")));
},
[&](const AutoArgString & arg) {
v->mkString(arg.s);

View file

@ -7,12 +7,29 @@ struct CompatibilitySettings : public Config
CompatibilitySettings() = default;
// Added in Nix 2.24, July 2024.
Setting<bool> nixShellAlwaysLooksForShellNix{this, true, "nix-shell-always-looks-for-shell-nix", R"(
Before Nix 2.24, [`nix-shell`](@docroot@/command-ref/nix-shell.md) would only look at `shell.nix` if it was in the working directory - when no file was specified.
Since Nix 2.24, `nix-shell` always looks for a `shell.nix`, whether that's in the working directory, or in a directory that was passed as an argument.
You may set this to `false` to revert to the Nix 2.3 behavior.
You may set this to `false` to temporarily revert to the behavior of Nix 2.23 and older.
Using this setting is not recommended.
It will be deprecated and removed.
)"};
// Added in Nix 2.24, July 2024.
Setting<bool> nixShellShebangArgumentsRelativeToScript{
this, true, "nix-shell-shebang-arguments-relative-to-script", R"(
Before Nix 2.24, relative file path expressions in arguments in a `nix-shell` shebang were resolved relative to the working directory.
Since Nix 2.24, `nix-shell` resolves these paths in a manner that is relative to the [base directory](@docroot@/glossary.md#gloss-base-directory), defined as the script's directory.
You may set this to `false` to temporarily revert to the behavior of Nix 2.23 and older.
Using this setting is not recommended.
It will be deprecated and removed.
)"};
};

View file

@ -29,6 +29,7 @@ struct Completions final : AddCompletions
*/
class RootArgs : virtual public Args
{
protected:
/**
* @brief The command's "working directory", but only set when top level.
*

View file

@ -183,6 +183,9 @@ static void main_nix_build(int argc, char * * argv)
struct MyArgs : LegacyArgs, MixEvalArgs
{
using LegacyArgs::LegacyArgs;
void setBaseDir(Path baseDir) {
commandBaseDir = baseDir;
}
};
MyArgs myArgs(myName, [&](Strings::iterator & arg, const Strings::iterator & end) {
@ -290,6 +293,9 @@ static void main_nix_build(int argc, char * * argv)
state->repair = myArgs.repair;
if (myArgs.repair) buildMode = bmRepair;
if (inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript) {
myArgs.setBaseDir(absPath(dirOf(script)));
}
auto autoArgs = myArgs.getAutoArgs(*state);
auto autoArgsWithInNixShell = autoArgs;
@ -334,8 +340,13 @@ static void main_nix_build(int argc, char * * argv)
exprs = {state->parseStdin()};
else
for (auto i : remainingArgs) {
auto baseDir = inShebang && !packages ? absPath(dirOf(script)) : i;
if (fromArgs)
exprs.push_back(state->parseExprFromString(std::move(i), state->rootPath(".")));
exprs.push_back(state->parseExprFromString(
std::move(i),
(inShebang && compatibilitySettings.nixShellShebangArgumentsRelativeToScript) ? lookupFileArg(*state, baseDir) : state->rootPath(".")
));
else {
auto absolute = i;
try {