Fish: Difference between revisions

From NixOS Wiki
imported>Legendofmiracles
adds a bit of examples of how to configure fish with home-manager
imported>Legendofmiracles
m fixed the small mess i created ;-;
Line 23: Line 23:
Then you can add new ones by adding them to the list of submodules of <code>programs.fish.plugins</code>. So for example for the plugin <code>z</code> from jethrokuan on github.
Then you can add new ones by adding them to the list of submodules of <code>programs.fish.plugins</code>. So for example for the plugin <code>z</code> from jethrokuan on github.


<code>
<syntaxhighlight lang="nix">
programs.fish.enable = true;
  programs.fish.enable = true;
   programs.fish.plugins = [{
   programs.fish.plugins = [{
     name = "z";
     name = "z";
Line 34: Line 34:
     };
     };
   }];
   }];
</code>
</syntaxhighlight>
 


== Useful scripts ==
== Useful scripts ==
Line 63: Line 62:


=== Environments ===
=== Environments ===
Helper functions that put you in a nix-shell with the given packages installed. You can either put these in <code>programs.fish.function</code> with home-manager or in <code>~/.config/fish/functions/fish_prompt.fish</code> without.
Helper functions that put you in a nix-shell with the given packages installed.  
 
You can either put these in <code>programs.fish.functions</code> with home-manager or in <code>~/.config/fish/functions/fish_prompt.fish</code> without.


==== haskellEnv ====
==== haskellEnv ====
Line 76: Line 77:


==== pythonEnv ====
==== pythonEnv ====
<syntaxhighlight lang="fish">
<syntaxhighlight lang="fish">
function pythonEnv --description 'start a nix-shell with the given python packages' --argument pythonVersion
function pythonEnv --description 'start a nix-shell with the given python packages' --argument pythonVersion

Revision as of 18:10, 28 March 2021

fish is the Friendly Interactive SHell.

Setting fish as the login shell

The following /etc/nixos/configuration.nix fragment demonstrates how to enable fish and set it as the default shell for user foo.

{
  ...
  programs.fish.enable = true;

  users.users.foo = {
    shell = pkgs.fish;
  };
  ...
}

Managing fish plugins with Home Manager

In order to manage fish with home manager you also have to enable it in your home.nix.

Then you can add new ones by adding them to the list of submodules of programs.fish.plugins. So for example for the plugin z from jethrokuan on github.

  programs.fish.enable = true;
  programs.fish.plugins = [{
    name = "z";
    src = pkgs.fetchFromGitHub {
      owner = "jethrokuan";
      repo = "z";
      rev = "e0e1b9dfdba362f8ab1ae8c1afc7ccf62b89f7eb";
      sha256 = "0dbnir6jbwjpjalz14snzd3cgdysgcs3raznsijd6savad3qhijc";
    };
  }];

Useful scripts

Show that you are in a nix-shell

Add this to the fish_prompt function (usually placed in ~/.config/fish/functions/fish_prompt.fish):

set -l nix_shell_info (
  if test -n "$IN_NIX_SHELL"
    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.

You can either put these in programs.fish.functions with home-manager or in ~/.config/fish/functions/fish_prompt.fish without.

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