1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfge = config.environment;
8
9 cfg = config.programs.fish;
10
11 fishAbbrs = concatStringsSep "\n" (
12 mapAttrsFlatten (k: v: "abbr -ag ${k} ${escapeShellArg v}")
13 cfg.shellAbbrs
14 );
15
16 fishAliases = concatStringsSep "\n" (
17 mapAttrsFlatten (k: v: "alias ${k} ${escapeShellArg v}")
18 (filterAttrs (k: v: v != null) cfg.shellAliases)
19 );
20
21 envShellInit = pkgs.writeText "shellInit" cfge.shellInit;
22
23 envLoginShellInit = pkgs.writeText "loginShellInit" cfge.loginShellInit;
24
25 envInteractiveShellInit = pkgs.writeText "interactiveShellInit" cfge.interactiveShellInit;
26
27 sourceEnv = file:
28 if cfg.useBabelfish then
29 "source /etc/fish/${file}.fish"
30 else
31 ''
32 set fish_function_path ${pkgs.fishPlugins.foreign-env}/share/fish/vendor_functions.d $fish_function_path
33 fenv source /etc/fish/foreign-env/${file} > /dev/null
34 set -e fish_function_path[1]
35 '';
36
37 babelfishTranslate = path: name:
38 pkgs.runCommandLocal "${name}.fish" {
39 nativeBuildInputs = [ pkgs.babelfish ];
40 } "${pkgs.babelfish}/bin/babelfish < ${path} > $out;";
41
42in
43
44{
45
46 options = {
47
48 programs.fish = {
49
50 enable = mkOption {
51 default = false;
52 description = lib.mdDoc ''
53 Whether to configure fish as an interactive shell.
54 '';
55 type = types.bool;
56 };
57
58 useBabelfish = mkOption {
59 type = types.bool;
60 default = false;
61 description = lib.mdDoc ''
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.
64 '';
65 };
66
67 vendor.config.enable = mkOption {
68 type = types.bool;
69 default = true;
70 description = lib.mdDoc ''
71 Whether fish should source configuration snippets provided by other packages.
72 '';
73 };
74
75 vendor.completions.enable = mkOption {
76 type = types.bool;
77 default = true;
78 description = lib.mdDoc ''
79 Whether fish should use completion files provided by other packages.
80 '';
81 };
82
83 vendor.functions.enable = mkOption {
84 type = types.bool;
85 default = true;
86 description = lib.mdDoc ''
87 Whether fish should autoload fish functions provided by other packages.
88 '';
89 };
90
91 shellAbbrs = mkOption {
92 default = {};
93 example = {
94 gco = "git checkout";
95 npu = "nix-prefetch-url";
96 };
97 description = lib.mdDoc ''
98 Set of fish abbreviations.
99 '';
100 type = with types; attrsOf str;
101 };
102
103 shellAliases = mkOption {
104 default = {};
105 description = lib.mdDoc ''
106 Set of aliases for fish shell, which overrides {option}`environment.shellAliases`.
107 See {option}`environment.shellAliases` for an option format description.
108 '';
109 type = with types; attrsOf (nullOr (either str path));
110 };
111
112 shellInit = mkOption {
113 default = "";
114 description = lib.mdDoc ''
115 Shell script code called during fish shell initialisation.
116 '';
117 type = types.lines;
118 };
119
120 loginShellInit = mkOption {
121 default = "";
122 description = lib.mdDoc ''
123 Shell script code called during fish login shell initialisation.
124 '';
125 type = types.lines;
126 };
127
128 interactiveShellInit = mkOption {
129 default = "";
130 description = lib.mdDoc ''
131 Shell script code called during interactive fish shell initialisation.
132 '';
133 type = types.lines;
134 };
135
136 promptInit = mkOption {
137 default = "";
138 description = lib.mdDoc ''
139 Shell script code used to initialise fish prompt.
140 '';
141 type = types.lines;
142 };
143
144 };
145
146 };
147
148 config = mkIf cfg.enable {
149
150 programs.fish.shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
151
152 # Required for man completions
153 documentation.man.generateCaches = lib.mkDefault true;
154
155 environment = mkMerge [
156 (mkIf cfg.useBabelfish
157 {
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";
162 })
163
164 (mkIf (!cfg.useBabelfish)
165 {
166 etc."fish/foreign-env/shellInit".source = envShellInit;
167 etc."fish/foreign-env/loginShellInit".source = envLoginShellInit;
168 etc."fish/foreign-env/interactiveShellInit".source = envInteractiveShellInit;
169 })
170
171 {
172 etc."fish/nixos-env-preinit.fish".text =
173 if cfg.useBabelfish
174 then ''
175 # source the NixOS environment config
176 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]
177 source /etc/fish/setEnvironment.fish
178 end
179 ''
180 else ''
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
184
185 # source the NixOS environment config
186 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]
187 fenv source ${config.system.build.setEnvironment}
188 end
189
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
192 '';
193 }
194
195 {
196 etc."fish/config.fish".text = ''
197 # /etc/fish/config.fish: DO NOT EDIT -- this file has been generated automatically.
198
199 # if we haven't sourced the general config, do it
200 if not set -q __fish_nixos_general_config_sourced
201 ${sourceEnv "shellInit"}
202
203 ${cfg.shellInit}
204
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
208 end
209
210 # if we haven't sourced the login config, do it
211 status --is-login; and not set -q __fish_nixos_login_config_sourced
212 and begin
213 ${sourceEnv "loginShellInit"}
214
215 ${cfg.loginShellInit}
216
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
220 end
221
222 # if we haven't sourced the interactive config, do it
223 status --is-interactive; and not set -q __fish_nixos_interactive_config_sourced
224 and begin
225 ${fishAbbrs}
226 ${fishAliases}
227
228 ${sourceEnv "interactiveShellInit"}
229
230 ${cfg.promptInit}
231 ${cfg.interactiveShellInit}
232
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
237 end
238 '';
239 }
240
241 {
242 etc."fish/generated_completions".source =
243 let
244 patchedGenerator = pkgs.stdenv.mkDerivation {
245 name = "fish_patched-completion-generator";
246 srcs = [
247 "${pkgs.fish}/share/fish/tools/create_manpage_completions.py"
248 "${pkgs.fish}/share/fish/tools/deroff.py"
249 ];
250 unpackCmd = "cp $curSrc $(basename $curSrc)";
251 sourceRoot = ".";
252 patches = [ ./fish_completion-generator.patch ]; # to prevent collisions of identical completion files
253 dontBuild = true;
254 installPhase = ''
255 mkdir -p $out
256 cp * $out/
257 '';
258 preferLocalBuild = true;
259 allowSubstitutes = false;
260 };
261 generateCompletions = package: pkgs.runCommand
262 "${package.name}_fish-completions"
263 (
264 {
265 inherit package;
266 preferLocalBuild = true;
267 allowSubstitutes = false;
268 }
269 // optionalAttrs (package ? meta.priority) { meta.priority = package.meta.priority; }
270 )
271 ''
272 mkdir -p $out
273 if [ -d $package/share/man ]; then
274 find $package/share/man -type f | xargs ${pkgs.python3.interpreter} ${patchedGenerator}/create_manpage_completions.py --directory $out >/dev/null
275 fi
276 '';
277 in
278 pkgs.buildEnv {
279 name = "system_fish-completions";
280 ignoreCollisions = true;
281 paths = map generateCompletions config.environment.systemPackages;
282 };
283 }
284
285 # include programs that bring their own completions
286 {
287 pathsToLink = []
288 ++ optional cfg.vendor.config.enable "/share/fish/vendor_conf.d"
289 ++ optional cfg.vendor.completions.enable "/share/fish/vendor_completions.d"
290 ++ optional cfg.vendor.functions.enable "/share/fish/vendor_functions.d";
291 }
292
293 { systemPackages = [ pkgs.fish ]; }
294
295 {
296 shells = [
297 "/run/current-system/sw/bin/fish"
298 "${pkgs.fish}/bin/fish"
299 ];
300 }
301 ];
302
303 programs.fish.interactiveShellInit = ''
304 # add completions generated by NixOS to $fish_complete_path
305 begin
306 # 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",
307 # splits by null byte, and then removes all empty lines produced by using 'string'
308 set -l prev (string join0 $fish_complete_path | string match --regex "^.*?(?=\x00[^\x00]*generated_completions.*)" | string split0 | string match -er ".")
309 set -l post (string join0 $fish_complete_path | string match --regex "[^\x00]*generated_completions.*" | string split0 | string match -er ".")
310 set fish_complete_path $prev "/etc/fish/generated_completions" $post
311 end
312 # prevent fish from generating completions on first run
313 if not test -d $__fish_user_data_dir/generated_completions
314 ${pkgs.coreutils}/bin/mkdir $__fish_user_data_dir/generated_completions
315 end
316 '';
317
318 };
319
320}