Pierre-olivier Gendraud
Pierre-olivier Gendraud

Reputation: 1927

How to see the PATH inside a shell without opening a shell

Use the command flag looked like a solution but it doesn't work

Inside the following shell:

nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello 

the path contain a directory with an executable hello

I've tried this:

nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello --command echo $PATH

I can't see the hello executable

My eyes are not the problem.

diff <( echo $PATH ) <( nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello --command echo $PATH)

It see no difference. It means that the printed path doesn't not contains hello.

Why?

Upvotes: 0

Views: 270

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 296049

The printed path does not contain hello because if your starting PATH was /nix/var/nix/profiles/default/bin:/run/current-system/sw/bin, then you just ran:

nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
  echo /nix/var/nix/profiles/default/bin:/run/current-system/sw/bin

That is to say, you passed your original path as an argument to the nix shell command, instead of passing it a reference to a variable for it to expand later.


The easiest way to accomplish what you're looking for is:

nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
  sh -c 'echo "$PATH"'

The single quotes prevent your shell from expanding $PATH before a copy of sh invoked by nix is started.


Of course, if you really don't want to start any kind of child shell, then you can run a non-shell tool to print environment variables:

nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
  env | grep '^PATH='

Upvotes: 2

Related Questions