Fish: Difference between revisions

From NixOS Wiki
imported>Mic92
No edit summary
imported>Mic92
No edit summary
Line 3: Line 3:
== Fish as login shell ==
== Fish as login shell ==


The following set up fish as a login shell for the user <code>myuser</code>
The following lines in <code>configuration.nix</code> set up fish as a login shell for the user <code>myuser</code>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 16: Line 16:
Add this to <code>fish/fish_prompt.fish</code>:
Add this to <code>fish/fish_prompt.fish</code>:


  set -l nix_shell_info (
<syntaxhighlight lang="fish">
    if test "$IN_NIX_SHELL" = "1"
set -l nix_shell_info (
      echo -n "<nix-shell> "
  if test "$IN_NIX_SHELL" = "1"
    end
    echo -n "<nix-shell> "
  )
  end
)
</syntaxhighlight>
 
and <code>$nix_shell_info</code> to the echo in that function, e.g.:
and <code>$nix_shell_info</code> to the echo in that function, e.g.:


echo -n -s "$nix_shell_info ~>"
<syntaxhighlight lang="fish">
echo -n -s "$nix_shell_info ~>"
</syntaxhighlight>


Now your prompt looks like this
Now your prompt looks like this
Line 36: Line 41:


==== haskellEnv ====
==== haskellEnv ====
function haskellEnv
 
nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ $argv ])"
<syntaxhighlight lang="fish">
end
function haskellEnv
  nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ $argv ])"
end
</syntaxhighlight>
   
   
  # Invocation: haskellEnv package1 packages2 .. packageN
  # Invocation: haskellEnv package1 packages2 .. packageN


==== pythonEnv ====
==== pythonEnv ====
function pythonEnv --description 'start a nix-shell with the given python packages' --argument pythonVersion
<syntaxhighlight lang="fish">
if set -q argv[2]
function pythonEnv --description 'start a nix-shell with the given python packages' --argument pythonVersion
    set argv $argv[2..-1]
  if set -q argv[2]
  end
    set argv $argv[2..-1]
  end
   
   
for el in $argv
  for el in $argv
    set ppkgs $ppkgs "python"$pythonVersion"Packages.$el"
    set ppkgs $ppkgs "python"$pythonVersion"Packages.$el"
  end
  end
   
   
nix-shell -p $ppkgs
  nix-shell -p $ppkgs
end
end
 
# Invocation: pythonEnv 3 package1 package2 .. packageN
# Invocation: pythonEnv 3 package1 package2 .. packageN
# or:        pythonEnv 2 ..
# or:        pythonEnv 2 ..
</syntaxhighlight>

Revision as of 22:06, 22 August 2017

Fish is the Friendly Interactive SHell.

Fish as login shell

The following lines in configuration.nix set up fish as a login shell for the user myuser

programs.fish.enable = true;
users.extraUsers.myuser = {
  shell = "/run/current-system/sw/bin/fish";
};

Useful scripts

Show that you are in a nix-shell

Add this to fish/fish_prompt.fish:

set -l nix_shell_info (
  if test "$IN_NIX_SHELL" = "1"
    echo -n "<nix-shell> "
  end
)

and $nix_shell_info to the echo in that function, e.g.:

echo -n -s "$nix_shell_info ~>"

Now your prompt looks like this

  • outside: ~>
  • inside: <nix-shell> ~>

btw. you can directly start nix-shell in fish with nix-shell --run fish, but (FIXME) the normal build functions are not available there.

Environments

Helper functions that put you in a nix-shell with the given packages installed.

haskellEnv

function haskellEnv
  nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ $argv ])"
end
# Invocation: haskellEnv package1 packages2 .. packageN

pythonEnv

function pythonEnv --description 'start a nix-shell with the given python packages' --argument pythonVersion
  if set -q argv[2]
    set argv $argv[2..-1]
  end
 
  for el in $argv
    set ppkgs $ppkgs "python"$pythonVersion"Packages.$el"
  end
 
  nix-shell -p $ppkgs
end

# Invocation: pythonEnv 3 package1 package2 .. packageN
# or:         pythonEnv 2 ..