Fish: Difference between revisions

DHCP (talk | contribs)
m update a link to use https; refine highlighted text for the gentoo wiki url
Sapi (talk | contribs)
m Replaced attr def with a inherit, does the same thing.
 
(8 intermediate revisions by 3 users not shown)
Line 71: Line 71:
     grc
     grc
     # Use 3rd-party fish plugins manually packaged.
     # Use 3rd-party fish plugins manually packaged.
     (pkgs.callPackage ../fish-colored-man.nix {buildFishPlugin = pkgs.fishPlugins.buildFishPlugin; } )
     (pkgs.callPackage ../fish-colored-man.nix { inherit (pkgs.fishPlugins) buildFishPlugin; } )
   ];
   ];
</nowiki>
</nowiki>
Line 133: Line 133:
== Tips and tricks ==
== Tips and tricks ==


=== Setting fish as default shell ===
=== Setting fish as the default shell ===
It is possible to set fish as the interactive non-login shell for your terminal emulator without setting it as the login shell (the one in <code>/etc/passwd</code>).


Using fish as the the login shell can cause compatibility issues. For example, certain recovery environments such as systemd's emergency mode to be completely broken when fish was set as the login shell. This  limitation is noted on the [https://wiki.gentoo.org/wiki/Fish#Caveats Gentoo wiki]. There they present an alternative, keeping bash as the system shell but having it exec fish when run interactively.
As this is usually a user-wise setting, to configure the terminal emulator you will need to either modify its config file, or use [[Home Manager]].


{{Expansion}}
==== Setting fish as default for Kitty ====
Using [[Home Manager]]:
{{File|3=programs.kitty = {
    enable = true;
    shellIntegration.enableFishIntegration = true;
    settings = {
      shell = "fish";
    };
  };|name=home.nix|lang=nix}}
Note: the <code>shellIntegration.enableFishIntegration = true;</code> is not required for setting fish as default, but provides other useful quality-of-life features, see https://sw.kovidgoyal.net/kitty/shell-integration/.


Here is one solution, which launches fish unless the parent process is already fish:
For use without home-manager, refer to https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.shell and https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.shell_integration.
 
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
programs.bash = {
  interactiveShellInit = ''
    if [[ $(${pkgs.procps}/bin/ps --no-header --pid=$PPID --format=comm) != "fish" && -z ''${BASH_EXECUTION_STRING} ]]
    then
      shopt -q login_shell && LOGIN_OPTION='--login' || LOGIN_OPTION=""
      exec ${pkgs.fish}/bin/fish $LOGIN_OPTION
    fi
  '';
};
</nowiki>
}}
 
Note that if you use the copy button to the right of the above code block, there will be a unicode space before the <code>!=</code> on line 3 of the snippet that you will need to change to a non-unicode space before executing this in your shell.


'''Setting fish as default for Gnome Console'''
'''Setting fish as default for Gnome Console'''


It is possible to set fish as the interactive non-login shell for Gnome Console without setting fish as the login shell (the login shell in /etc/passwd for your user will not be fish).
Using [[Home-manager|Home Manager]]:


{{File|3=home-manager.users.myuser = {
{{File|3=home-manager.users.myuser = {
Line 170: Line 164:
};|name=home.nix|lang=nix}}
};|name=home.nix|lang=nix}}


=== Setting fish as the login shell ===
Using fish as the the login shell can cause compatibility issues. For example, certain recovery environments such as systemd's emergency mode to be completely broken when fish was set as the login shell. ArchWiki presents an [https://wiki.archlinux.org/title/Fish#Modify_.bashrc_to_drop_into_fish alternative solution], keeping bash as the system shell but having it exec fish when run interactively.


If you still want to set fish as the login shell, see [[Command Shell#Changing the default shell]].
Do note that even the following code is not full-proof, and should only be used with a thorough understanding. Prefer [[Fish#Setting fish as default shell|Setting fish as the default shell]].


==== Disable man page generation ====
Here is one solution, which launches fish unless the parent process is already fish:
Some users suffer from slow build due to fish enabling `documentation.man.generateCaches`. You may force false.
 
documentation.man.generateCaches = false'';''
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
programs.bash = {
  interactiveShellInit = ''
    # "check if parent process is not fish" && "make nested shells work properly"
    if grep -qv fish /proc/$PPID/comm && [[ $SHLVL == [12] ]]; then
        # set $SHELL for better integration with programs like nix shell, tmux, etc.
        SHELL=${pkgs.fish}/bin/fish exec fish
    fi
  '';
};
</nowiki>
}}
 
For a more detailed explanation, please see the [https://wiki.archlinux.org/title/Fish#Modify_.bashrc_to_drop_into_fish aforementioned ArchWiki page].


For home-manager users, man cache need to be disabled in programs
If you are using Lix, you can change the grep condition to <code>"fish\|nix-shell"</code> so the <code>nix-shell</code> command still works, this is necessary since Lix forks before spawning the shell process so the parent process ends up being nix-shell and not fish.<ref>https://git.lix.systems/lix-project/lix/issues/1131#issuecomment-18341</ref>


programs.man.generateCaches = false'';''
↵If you still want to set fish as the login shell, see [[Command Shell#Changing the default shell]].


==== Running fish interactively with zsh as system shell on darwin ====
==== Running fish interactively with zsh as system shell on darwin ====
{{outdated|date=May 14, 2026|reason=I've updated the interactiveShellInit snippet that this section refers to. Either the following paragraph should be reworded or the provided zsh config be modified. For more context, please see my corresponding [https://wiki.nixos.org/w/index.php?title=Fish&oldid=31760 edit] and the linked talk page.|talk=Talk:Fish#Suggested_bashrc_in_Fish#Setting_fish_as_default_shell_needs_revision}}


Zsh users on darwin will need to use a modified version of the above snippet. As written, it presents two incompatibilities. First, being BSD-derived, MacOS's <code>ps</code> command accepts different options. Second, this is a script intended for bash, not zsh. MacOS uses zsh as its default shell.
Zsh users on darwin will need to use a modified version of the above snippet. As written, it presents two incompatibilities. First, being BSD-derived, MacOS's <code>ps</code> command accepts different options. Second, this is a script intended for bash, not zsh. MacOS uses zsh as its default shell.
Line 195: Line 207:
};
};
</syntaxhighlight>
</syntaxhighlight>
=== Disable man page generation ===
Some users suffer from slow build due to fish enabling `documentation.man.generateCaches`. You may force false.
documentation.man.generateCaches = false'';''
For home-manager users, man cache needs to be disabled in programs
programs.man.generateCaches = false'';''


=== Show that you are in a nix-shell ===
=== Show that you are in a nix-shell ===