Skip to content

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

@Kreyren

Description

@Kreyren

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`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions