Nixpkgs: $PATH is incorrect when running fish in a nix-shell

Created on 25 Sep 2016  Â·  6Comments  Â·  Source: NixOS/nixpkgs

Issue description

When starting a fish shell after dropping in a nix-shell, the PATH environment variable is improperly set. This makes fish unusable for my use case with nix-shell.

Steps to reproduce

Say I have the following default.nix file in directory :

with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "whatever";
  buildInputs = [
    whatever-dependency
  ];
}

In the same directory, if I do nix-shell, I drop into a bash shell where the bin path of whatever-dependency is added to the PATH environment variable (if applicable).
However, if I start a fish shell from this bash instance, the PATH variable reverts to its default value.
The problem is the same when doing directly nix-shell --run fish or nix-shell --command fish. This doesn't cause any issue with zsh.

In the fish instance, other environment variables are properly updated. The IN_NIX_SHELL variable is indeed set to 1 and if you add some shellPrompt code in the derivation, you do inherit the proper environment. The PATH variable seems to be the only one affected.

I suspect that somewhere in fish-foreign-env we recreate the PATH from scratch instead of using the value of the parent shell. I might be mistaken ; I just tried fish on NixOS today and don't know much.

Technical details

  • System: 17.03.git.2b0eace (Gorilla)
  • Nix version: nix-env (Nix) 1.11.4
  • Nixpkgs version: "17.03.git.2b0eace"
bug nixos

All 6 comments

The issue doesn't seem to be related to nix-shell, as it's reproducible outside of it:

rasen@Larry ~ % export PATH=/my/dir/:$PATH
rasen@Larry ~ % echo $PATH
/my/dir/:/home/rasen/.local/bin:/home/rasen/bin:/usr/local/bin:/usr/local/sbin:/home/rasen/bin:/var/setuid-wrappers:/home/rasen/.nix-profile/bin:/home/rasen/.nix-profile/sbin:/home/rasen/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/run/current-system/sw/bin:/run/current-system/sw/sbin:/run/current-system/sw/lib/kde4/libexec
rasen@Larry ~ % fish
rasen@Larry ~> echo $PATH
/home/rasen/bin /var/setuid-wrappers /home/rasen/.nix-profile/bin /home/rasen/.nix-profile/sbin /home/rasen/.nix-profile/lib/kde4/libexec /nix/var/nix/profiles/default/bin /nix/var/nix/profiles/default/sbin /nix/var/nix/profiles/default/lib/kde4/libexec /run/current-system/sw/bin /run/current-system/sw/sbin /run/current-system/sw/lib/kde4/libexec

The issue seems to be NixOS-specific, as nixpkgs-fish works fine outside of NixOS (Gentoo).

I've noticed a very strange problem, potentially related, in NixOS 17.03. When I use fish as a login shell at a virtual console, I get a ton of errors and login hangs. I do not get this behavior when I open a terminal in the GUI. I also do not get this behavior if I disable my oh-my-fish profile— even if I then go on to source my OMF config manually after logging in at the virtual console.

The particular errors I see from OMF, though, are about binaries (system dependencies of my OMF plugins) being missing, which sounds very much like a path problem.

The plugin has had some changes since we last updated it.

I'm going to update our fish-foreign-env package right now and see where that takes us, as a start.

That didn't seem to help. I am now able to log in (looks like the omf plugin 'local-config' was causing the hang. Lots of things are getting errors. I checked my path and it's set to /bin and /usr/bin.

I think maybe the user config is being read before the PATH is set.

@rasendubi , are you using fish as a login shell (or do you at least have programs.fish.enable = true; set)?

The foreign-env plugin for fish by design simply replaces the values of the environment variables in fish with whatever they were set to in the bash script, if they've changed at all.

What's happening on NixOS is that every fish invocation sources ${config.system.build.setEnvironment} through fenv. The sourced file totally clobbers the PATH (as it should), and foreign-env is faithfully reproducing that clobbering (as it should). Here is the relevant line from that file on my system:

export PATH="$HOME/.nix-profile/bin:$HOME/.nix-profile/sbin:$HOME/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/run/current-system/sw/bin:/run/current-system/sw/sbin:/run/current-system/sw/lib/kde4/libexec"

Notice that there are no instances of ‘$PATH’ on the right-hand side there.

I took a look at the module for zsh, and it looks like what we want is likely just to prevent NixOS' /etc/fish/config.fish script from being sourced multiple times by the same shell or its descendants.

I did a thing, you guys! Let me know what you think.

I fixed redundant sourcing (both partial and for the whole file) and got it to work so that it can detect when another shell, like bash or zsh, has already done the basic Nix initialization. :-)

fixed in #45784

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  Â·  3Comments

rzetterberg picture rzetterberg  Â·  3Comments

tomberek picture tomberek  Â·  3Comments

sid-kap picture sid-kap  Â·  3Comments

matthiasbeyer picture matthiasbeyer  Â·  3Comments