1 { config, lib, pkgs, ... }:
5 cfge = config.environment;
7 cfg = config.programs.fish;
9 fishAbbrs = lib.concatStringsSep "\n" (
10 lib.mapAttrsToList (k: v: "abbr -ag ${k} ${lib.escapeShellArg v}")
14 fishAliases = lib.concatStringsSep "\n" (
15 lib.mapAttrsToList (k: v: "alias ${k} ${lib.escapeShellArg v}")
16 (lib.filterAttrs (k: v: v != null) cfg.shellAliases)
19 envShellInit = pkgs.writeText "shellInit" cfge.shellInit;
21 envLoginShellInit = pkgs.writeText "loginShellInit" cfge.loginShellInit;
23 envInteractiveShellInit = pkgs.writeText "interactiveShellInit" cfge.interactiveShellInit;
26 if cfg.useBabelfish then
27 "source /etc/fish/${file}.fish"
30 set fish_function_path ${pkgs.fishPlugins.foreign-env}/share/fish/vendor_functions.d $fish_function_path
31 fenv source /etc/fish/foreign-env/${file} > /dev/null
32 set -e fish_function_path[1]
35 babelfishTranslate = path: name:
36 pkgs.runCommandLocal "${name}.fish" {
37 nativeBuildInputs = [ pkgs.babelfish ];
38 } "babelfish < ${path} > $out;";
48 enable = lib.mkOption {
51 Whether to configure fish as an interactive shell.
53 type = lib.types.bool;
56 package = lib.mkPackageOption pkgs "fish" { };
58 useBabelfish = lib.mkOption {
59 type = lib.types.bool;
62 If enabled, the configured environment will be translated to native fish using [babelfish](https://github.com/bouk/babelfish).
63 Otherwise, [foreign-env](https://github.com/oh-my-fish/plugin-foreign-env) will be used.
67 vendor.config.enable = lib.mkOption {
68 type = lib.types.bool;
71 Whether fish should source configuration snippets provided by other packages.
75 vendor.completions.enable = lib.mkOption {
76 type = lib.types.bool;
79 Whether fish should use completion files provided by other packages.
83 vendor.functions.enable = lib.mkOption {
84 type = lib.types.bool;
87 Whether fish should autoload fish functions provided by other packages.
91 shellAbbrs = lib.mkOption {
95 npu = "nix-prefetch-url";
98 Set of fish abbreviations.
100 type = with lib.types; attrsOf str;
103 shellAliases = lib.mkOption {
106 Set of aliases for fish shell, which overrides {option}`environment.shellAliases`.
107 See {option}`environment.shellAliases` for an option format description.
109 type = with lib.types; attrsOf (nullOr (either str path));
112 shellInit = lib.mkOption {
115 Shell script code called during fish shell initialisation.
117 type = lib.types.lines;
120 loginShellInit = lib.mkOption {
123 Shell script code called during fish login shell initialisation.
125 type = lib.types.lines;
128 interactiveShellInit = lib.mkOption {
131 Shell script code called during interactive fish shell initialisation.
133 type = lib.types.lines;
136 promptInit = lib.mkOption {
139 Shell script code used to initialise fish prompt.
141 type = lib.types.lines;
148 config = lib.mkIf cfg.enable {
150 programs.fish.shellAliases = lib.mapAttrs (name: lib.mkDefault) cfge.shellAliases;
152 # Required for man completions
153 documentation.man.generateCaches = lib.mkDefault true;
155 environment = lib.mkMerge [
156 (lib.mkIf cfg.useBabelfish
158 etc."fish/setEnvironment.fish".source = babelfishTranslate config.system.build.setEnvironment "setEnvironment";
159 etc."fish/shellInit.fish".source = babelfishTranslate envShellInit "shellInit";
160 etc."fish/loginShellInit.fish".source = babelfishTranslate envLoginShellInit "loginShellInit";
161 etc."fish/interactiveShellInit.fish".source = babelfishTranslate envInteractiveShellInit "interactiveShellInit";
164 (lib.mkIf (!cfg.useBabelfish)
166 etc."fish/foreign-env/shellInit".source = envShellInit;
167 etc."fish/foreign-env/loginShellInit".source = envLoginShellInit;
168 etc."fish/foreign-env/interactiveShellInit".source = envInteractiveShellInit;
172 etc."fish/nixos-env-preinit.fish".text =
175 # source the NixOS environment config
176 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]
177 source /etc/fish/setEnvironment.fish
181 # This happens before $__fish_datadir/config.fish sets fish_function_path, so it is currently
182 # unset. We set it and then completely erase it, leaving its configuration to $__fish_datadir/config.fish
183 set fish_function_path ${pkgs.fishPlugins.foreign-env}/share/fish/vendor_functions.d $__fish_datadir/functions
185 # source the NixOS environment config
186 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]
187 fenv source ${config.system.build.setEnvironment}
190 # clear fish_function_path so that it will be correctly set when we return to $__fish_datadir/config.fish
191 set -e fish_function_path
196 etc."fish/config.fish".text = ''
197 # /etc/fish/config.fish: DO NOT EDIT -- this file has been generated automatically.
199 # if we haven't sourced the general config, do it
200 if not set -q __fish_nixos_general_config_sourced
201 ${sourceEnv "shellInit"}
205 # and leave a note so we don't source this config section again from
206 # this very shell (children will source the general config anew)
207 set -g __fish_nixos_general_config_sourced 1
210 # if we haven't sourced the login config, do it
211 status is-login; and not set -q __fish_nixos_login_config_sourced
213 ${sourceEnv "loginShellInit"}
215 ${cfg.loginShellInit}
217 # and leave a note so we don't source this config section again from
218 # this very shell (children will source the general config anew)
219 set -g __fish_nixos_login_config_sourced 1
222 # if we haven't sourced the interactive config, do it
223 status is-interactive; and not set -q __fish_nixos_interactive_config_sourced
228 ${sourceEnv "interactiveShellInit"}
231 ${cfg.interactiveShellInit}
233 # and leave a note so we don't source this config section again from
234 # this very shell (children will source the general config anew,
235 # allowing configuration changes in, e.g, aliases, to propagate)
236 set -g __fish_nixos_interactive_config_sourced 1
242 etc."fish/generated_completions".source =
244 patchedGenerator = pkgs.stdenv.mkDerivation {
245 name = "fish_patched-completion-generator";
247 "${cfg.package}/share/fish/tools/create_manpage_completions.py"
248 "${cfg.package}/share/fish/tools/deroff.py"
250 unpackCmd = "cp $curSrc $(basename $curSrc)";
252 patches = [ ./fish_completion-generator.patch ]; # to prevent collisions of identical completion files
258 preferLocalBuild = true;
259 allowSubstitutes = false;
261 generateCompletions = package: pkgs.runCommandLocal
262 ( with lib.strings; let
263 storeLength = stringLength storeDir + 34; # Nix' StorePath::HashLen + 2 for the separating slash and dash
264 pathName = substring storeLength (stringLength package - storeLength) package;
265 in (package.name or pathName) + "_fish-completions")
266 ( { inherit package; } //
267 lib.optionalAttrs (package ? meta.priority) { meta.priority = package.meta.priority; })
270 if [ -d $package/share/man ]; then
271 find $package/share/man -type f | xargs ${pkgs.python3.pythonOnBuildForHost.interpreter} ${patchedGenerator}/create_manpage_completions.py --directory $out >/dev/null
276 name = "system_fish-completions";
277 ignoreCollisions = true;
278 paths = builtins.map generateCompletions config.environment.systemPackages;
282 # include programs that bring their own completions
285 ++ lib.optional cfg.vendor.config.enable "/share/fish/vendor_conf.d"
286 ++ lib.optional cfg.vendor.completions.enable "/share/fish/vendor_completions.d"
287 ++ lib.optional cfg.vendor.functions.enable "/share/fish/vendor_functions.d";
290 { systemPackages = [ cfg.package ]; }
294 "/run/current-system/sw/bin/fish"
295 (lib.getExe cfg.package)
300 programs.fish.interactiveShellInit = ''
301 # add completions generated by NixOS to $fish_complete_path
303 # joins with null byte to accommodate all characters in paths, then respectively gets all paths before (exclusive) / after (inclusive) the first one including "generated_completions",
304 # splits by null byte, and then removes all empty lines produced by using 'string'
305 set -l prev (string join0 $fish_complete_path | string match --regex "^.*?(?=\x00[^\x00]*generated_completions.*)" | string split0 | string match -er ".")
306 set -l post (string join0 $fish_complete_path | string match --regex "[^\x00]*generated_completions.*" | string split0 | string match -er ".")
307 set fish_complete_path $prev "/etc/fish/generated_completions" $post
309 # prevent fish from generating completions on first run
310 if not test -d $__fish_user_data_dir/generated_completions
311 ${pkgs.coreutils}/bin/mkdir $__fish_user_data_dir/generated_completions