Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-shell shebang does not interpret -E relative to script dir #4232

Open
lilyball opened this issue Nov 7, 2020 · 11 comments · May be fixed by #5088
Open

nix-shell shebang does not interpret -E relative to script dir #4232

lilyball opened this issue Nov 7, 2020 · 11 comments · May be fixed by #5088
Labels

Comments

@lilyball
Copy link
Member

lilyball commented Nov 7, 2020

Describe the bug

When using nix-shell in a shebang, it interprets filenames given to it relative to the script dir, but it interprets the -E flag relative to the cwd. This is rather problematic. I can't think of any reason to want the -E flag interpreted relative to the current dir and doing so means I'm forced to write a deps.nix file to disk next to my script if I need to import some other file at a known path.

More generally, I should be able to replace #!nix-shell deps.nix -i bash with #!nix-shell -E 'import ./deps.nix' -i bash.

Steps To Reproduce

Write a script that looks something like

#!/usr/bin/env nix-shell
#!nix-shell -i bash -E "with import ./deps.nix {}; mkShell { buildInputs = [bash]; }"
echo it works

Expected behavior

It shouldn't matter what my cwd is when I execute this, it should behave the same and import the deps.nix file that lives right next to the script.

Actual behavior

It tries to import ./deps.nix from whatever my cwd is.

nix-env --version output

nix-env (Nix) 2.3.7

@lilyball lilyball added the bug label Nov 7, 2020
@lilyball
Copy link
Member Author

lilyball commented Nov 7, 2020

On a related note, relative paths given to -I are interpreted relative to cwd but should also be script-relative when used in a shebang.

@expipiplus1
Copy link
Contributor

expipiplus1 commented Nov 7, 2020

I ran into this a couple of days ago too, here is my workaround, a Haskell/Bash polyglot

#! /usr/bin/env bash
{- 2>/dev/null
# vim: set ft=haskell
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
exec nix-shell \
  "$DIR/shell.nix" \
  --pure \
  --run "runghc $(printf "%q " "$0" "$@")"
-}
-- haskell code...

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/relative-paths-when-using-nix-shell-as-an-interpreter/1180/5

@lilyball
Copy link
Member Author

lilyball commented Nov 8, 2020

@expipiplus1 Neat, but in my case I need to support bash, tcl, perl, python, and ruby scripts, and I need a solution that my non-Nix-savvy coworkers can copy&paste, so the polyglot route is kind of problematic.

@stale
Copy link

stale bot commented Jun 2, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jun 2, 2021
@lilyball
Copy link
Member Author

Still a problem last I checked

@stale stale bot removed the stale label Jul 27, 2021
matthewbauer added a commit to matthewbauer/nix that referenced this issue Aug 3, 2021
When writing a shebang script, you expect your path to be relative to
the script, not the cwd. We previously handled this correctly for
relative file paths, but not for expressions.

This handles both -p & -E args. My understanding is this should be
what we want in any cases I can think of - people run scripts from
many different working directories. @edolstra is there any reason to
handle -p args differently in this case?

Fixes NixOS#4232
@matthewbauer matthewbauer linked a pull request Aug 3, 2021 that will close this issue
@matthewbauer
Copy link
Member

PR: #5088

@stale
Copy link

stale bot commented Apr 17, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Apr 17, 2022
@andersk
Copy link
Contributor

andersk commented Apr 29, 2022

Still an issue in Nix 2.8.0.

@stale stale bot removed the stale label Apr 29, 2022
@DavidDTA
Copy link

This is also an issue for positional arguments. The existing behavior directly contradicts the documentation here:

For example, the Python example could have been written as:

#! /usr/bin/env nix-shell
#! nix-shell deps.nix -i python

where the file deps.nix in the same directory as the #!-script contains:

@lf-

This comment was marked as outdated.

Ericson2314 pushed a commit to matthewbauer/nix that referenced this issue Jun 14, 2023
When writing a shebang script, you expect your path to be relative to
the script, not the cwd. We previously handled this correctly for
relative file paths, but not for expressions.

This handles both -p & -E args. My understanding is this should be
what we want in any cases I can think of - people run scripts from
many different working directories. @edolstra is there any reason to
handle -p args differently in this case?

Fixes NixOS#4232
tomberek pushed a commit to matthewbauer/nix that referenced this issue Nov 26, 2023
When writing a shebang script, you expect your path to be relative to
the script, not the cwd. We previously handled this correctly for
relative file paths, but not for expressions.

This handles both -p & -E args. My understanding is this should be
what we want in any cases I can think of - people run scripts from
many different working directories. @edolstra is there any reason to
handle -p args differently in this case?

Fixes NixOS#4232
tomberek pushed a commit to matthewbauer/nix that referenced this issue Nov 27, 2023
When writing a shebang script, you expect your path to be relative to
the script, not the cwd. We previously handled this correctly for
relative file paths, but not for expressions.

This handles both -p & -E args. My understanding is this should be
what we want in any cases I can think of - people run scripts from
many different working directories. @edolstra is there any reason to
handle -p args differently in this case?

Fixes NixOS#4232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants