nix icon indicating copy to clipboard operation
nix copied to clipboard

nix-shell #!-interpreter breaks when executed from a working directory that contains `shell.nix` file

Open Kreyren opened this issue 4 years ago • 4 comments

Describe the issue

Executing file with shebang #!/usr/bin/env nix-shell from a working directory that contains shell.nix file results in error: getting status of '/home/user/marco/polo/shell.nix': No such file or directory

Steps To Reproduce

  1. Create a new directory called e.g. ~/marco
  2. Create a new file called ~/marco/shell.nix
  3. Create a new file in ~/marco/polo/default.nix with content (shebang is important):
#!/usr/bin/env nix-shell
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/360d65e180403b940afcc93d68d5329488af14eb.tar.gz") {} }:
with nixpkgs;
	stdenv.mkDerivation {
		name = "Polo";

		shellHook = ''
			printf "\\n%s\\n" "Polo"
		'';
	}
  1. Make the ~/marco/polo/default.nix executable and execute it while the working directory is set to ~/marco and expect:
[kreyren@leonid:~/marco]$ ./polo/default.nix 
error: getting status of '/home/user/marco/polo/shell.nix': No such file or directory
  1. Remove the shell.nix file from ~/marco/shell.nix and notice that the ./polo/default.nix now works

Expected behavior

Successfully execute ./polo/default.nix from working directory that contains shell.nix

Additional context

Relevant Matrix discussion

In #nixos:nixos.org: https://matrix.to/#/!KqkRjyTEzAGRiZFBYT:nixos.org/$r5T9u3PIkI5SoGEB9SKKj9XEdtic36zf3rS5lW_-bh0?via=nixos.org&via=matrix.org&via=nixos.dev

Works with -v argument

Using /usr/bin/env nix-shell -v ./polo/default.nix and/or nix-shell -v ./polo/default.nix is not affected by the presence of shell.nix file in the working directory.

Motivation

Usage of nix to deploy standard environment in complicated monorepositories where the mentioned issue creates a complications like adapting a repository management in https://git.dotya.ml/RiXotStudio/RiXotStudio/src/commit/8e1c76835eec3d41c726eafe02b41487c6c324bb that executes ./20-editors/vscodium/nix/default.nix to provide an environment with standard vscodium which results in

[kreyren@leonid:~/Repositories/RiXotStudio]$ ./20-editors/vscodium/nix/default.nix 
error: getting status of '/home/kreyren/Repositories/RiXotStudio/20-editors/vscodium/nix/shell.nix': No such file or directory

and requires additional steps to change directory and checking for the presence of shell.nix file

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[kreyren@leonid:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 5.10.64, NixOS, 21.05.3915.95eed9b64ee (Okapi)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.4pre20211006_53e4794`
 - channels(root): `"nixos-21.05.3915.95eed9b64ee"`
 - channels(kreyren): `"home-manager-21.05, master, nixos-21.05.3915.95eed9b64ee"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Kreyren avatar Oct 25 '21 23:10 Kreyren

I understood that nix-shell is part of nix-build in NixOS/nix (https://github.com/NixOS/nix/blob/27444d40cf726129334899a42d68d69f73baa988/src/nix-build/nix-build.cc#L80)

In good faith i CC the relevant maintainers @Ericson2314 @shlevy

Kreyren avatar Oct 25 '21 23:10 Kreyren

Thanks for the report. I'm relocating to NixOS/nix since I'm pretty sure this is bug is a bug in Nix and not Nixpkgs.

grahamc avatar Oct 26 '21 01:10 grahamc

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

stale[bot] avatar Apr 27 '22 23:04 stale[bot]

Similarly, there is an annoying problem:

Say you have a directory scripts and a nix file nix/runhaskell.nix, there is no way of importing nix/runhaskell.nix from the shebang, because the paths are interpreted relative to the path the user is executing the file from, not the path of the file being executed. This is needless to say quite problematic.

#! /usr/bin/env nix-shell
#! nix-shell -i runhaskell --pure -E "import ./nix/runhaskell.nix (p: [ p.countable-inflections p.text-manipulate p.typed-process ])"

hm my issue is actually #4232

lf- avatar Aug 09 '22 22:08 lf-