Skip to content

$PATH is incorrect when running fish in a nix-shell #18946

Closed

Description

@yacinehmito

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"

Activity

  1. added
    0.kind: bug
    on Sep 25, 2016
  2. rasendubi commented on Sep 27, 2016

    @rasendubi
    Member

    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).

  3. added
    6.topic: nixos
    on Sep 27, 2016
  4. therealpxc commented on Mar 25, 2017

    @therealpxc
    Contributor

    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.

  5. therealpxc commented on Mar 25, 2017

    @therealpxc
    Contributor

    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.

  6. therealpxc commented on Mar 25, 2017

    @therealpxc
    Contributor

    @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.

  7. therealpxc commented on Mar 25, 2017

    @therealpxc
    Contributor

    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. :-)

  8. xeji commented on Sep 6, 2018

    @xeji
    Contributor

    fixed in #45784

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug6.topic: nixos

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions