haslersn / any-nix-shell

fish and zsh support for the nix run and nix-shell environments of the Nix package manager
MIT License
253 stars 17 forks source link

Doesn't seem to work with fish #4

Closed golddranks closed 3 years ago

golddranks commented 4 years ago

I'm on MacOS and fish, installed from homebrew.

I installed Nix, but it doesn't work on fish by default, because the init script for the shell is bash.

I installed this package and added any-nix-shell fish --info-right | source to my ~/.config/fish/config.fish, and restarted the shell, but it doesn't seem to do anything:

~ $ nix-shell
env: nix-shell: No such file or directory

The output of $HOME/.nix-profile/bin/any-nix-shell fish --info-right seems to produce sensible output though:

# If you see this output, you probably forgot to pipe it into 'source':
# any-nix-shell --info-right | source

# Overwrite the nix-shell command
function nix-shell
    /nix/store/1rr6rz9gjgirfw639f033agz6arp36d2-any-nix-shell-1.1.0/bin/.any-nix-shell-wrapper fish $argv
    set -gx ANY_NIX_SHELL_EXIT_STATUS $status
end

# Overwrite the nix command
function nix
    if test $argv[1] = run
        set argv[1] fish
        /nix/store/1rr6rz9gjgirfw639f033agz6arp36d2-any-nix-shell-1.1.0/bin/.any-nix-run-wrapper $argv
        set -gx ANY_NIX_SHELL_EXIT_STATUS $status
    else
        command nix $argv
    end
end

# Print additional information inside a nix-shell environment
function fish_right_prompt
    /nix/store/1rr6rz9gjgirfw639f033agz6arp36d2-any-nix-shell-1.1.0/bin/nix-shell-info
    printf " "
    set -e ANY_NIX_SHELL_EXIT_STATUS
end

If I launch bash and source ~/.nix-profile/etc/profile.d/nix.sh everything seems to work, so the Nix itself isn't broken.

haslersn commented 4 years ago

Unfortunately I can't debug on MacOS.

By the way: Did you try lorri? It's much better than any-nix-shell.

malob commented 4 years ago

@golddranks, it looks like the main issue you are having isn't related to any-nix-shell but rather that you haven't sourced the ~/.nix-profile/etc/profile.d/nix.sh from Fish. You need to do that before any-nix-shell can do it's thing.

As you point out, it's not straight forward to do that since it's a Bash shell script. The easiest way I've found to do this is to use the Fish plugin fenv. With the plugin installed, you can run fenv source ~/.nix-profile/etc/profile.d/nix.sh to get Nix working in Fish.

On my setup, I install fenv through Nix, https://github.com/malob/nixpkgs/blob/27b6d933eb08487ce8fa76e1cd76b2804535bc4e/overlays/myenv.nix#L18

and add it to my Fish config and source the Nix profile like so: https://github.com/malob/config.fish/blob/master/conf.d/00-nix.fish

hedefalk commented 3 years ago

Adding to @malob if anyone comes by later:

I've used fenv, bax and now replay fish functions do be able to source bash files from fish. Since nix doesn't give a way to start from fish natively, we need to source a bash file from fish and keep the env. In my config.fish I now have:

# Nix
if test -e '/Users/viktor/.nix-profile/etc/profile.d/nix.sh'
  set -x NIX_PATH $NIX_PATH $HOME/.nix-defexpr/channels
  replay source '/Users/viktor/.nix-profile/etc/profile.d/nix.sh'
end

This bootstraps nix within my fish shell. And I think this is what OP is really asking about.

This project here however is to continue to use fish or whatever besides bash when running nix-shell which is another thing entirely.

I came here now because I want just that. I'm trying out lorri now but I have the need to debug by just running a simple nix-shell at times and I still want to stay in fish. So thanks!

I think this issue can be closed :)