0

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?

  • 1
    In `nix shell ... --command echo $PATH`, the `$PATH` is expanded by the shell you typed the command into before that shell ever got a chance to start `nix`. Compare to `--command env` – Charles Duffy Dec 22 '22 at 22:10
  • 1
    BTW, as a general rule not specific to your problem at hand, you should always `echo "$PATH"`, not `echo $PATH`. See [I just assigned a variable, but `echo $variable` shows something different!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Dec 22 '22 at 22:17
  • (If you come from the Windows world, you might be accustomed to the program that's being started deciding how to interpret its command-line arguments. That's not the case on UNIX: Programs are not passed a single command-line string, but rather an "argument vector" -- an array of individual arguments already parsed out from the original string with shell expansions already performed) – Charles Duffy Dec 22 '22 at 22:20

1 Answers1

2

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='
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441