1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfge = config.environment;
8
9 cfg = config.programs.fish;
10
11 fishAliases = concatStringsSep "\n" (
12 mapAttrsFlatten (k: v: "alias ${k} '${v}'") cfg.shellAliases
13 );
14
15in
16
17{
18
19 options = {
20
21 programs.fish = {
22
23 enable = mkOption {
24 default = false;
25 description = ''
26 Whether to configure fish as an interactive shell.
27 '';
28 type = types.bool;
29 };
30
31 vendor.config.enable = mkOption {
32 type = types.bool;
33 default = true;
34 description = ''
35 Whether fish should source configuration snippets provided by other packages.
36 '';
37 };
38
39 vendor.completions.enable = mkOption {
40 type = types.bool;
41 default = true;
42 description = ''
43 Whether fish should use completion files provided by other packages.
44 '';
45 };
46
47 vendor.functions.enable = mkOption {
48 type = types.bool;
49 default = true;
50 description = ''
51 Whether fish should autoload fish functions provided by other packages.
52 '';
53 };
54
55 shellAliases = mkOption {
56 default = config.environment.shellAliases;
57 description = ''
58 Set of aliases for fish shell. See <option>environment.shellAliases</option>
59 for an option format description.
60 '';
61 type = types.attrs;
62 };
63
64 shellInit = mkOption {
65 default = "";
66 description = ''
67 Shell script code called during fish shell initialisation.
68 '';
69 type = types.lines;
70 };
71
72 loginShellInit = mkOption {
73 default = "";
74 description = ''
75 Shell script code called during fish login shell initialisation.
76 '';
77 type = types.lines;
78 };
79
80 interactiveShellInit = mkOption {
81 default = "";
82 description = ''
83 Shell script code called during interactive fish shell initialisation.
84 '';
85 type = types.lines;
86 };
87
88 promptInit = mkOption {
89 default = "";
90 description = ''
91 Shell script code used to initialise fish prompt.
92 '';
93 type = types.lines;
94 };
95
96 };
97
98 };
99
100 config = mkIf cfg.enable {
101
102 environment.etc."fish/foreign-env/shellInit".text = cfge.shellInit;
103 environment.etc."fish/foreign-env/loginShellInit".text = cfge.loginShellInit;
104 environment.etc."fish/foreign-env/interactiveShellInit".text = cfge.interactiveShellInit;
105
106 environment.etc."fish/nixos-env-preinit.fish".text = ''
107 # This happens before $__fish_datadir/config.fish sets fish_function_path, so it is currently
108 # unset. We set it and then completely erase it, leaving its configuration to $__fish_datadir/config.fish
109 set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $__fish_datadir/functions
110
111 # source the NixOS environment config
112 fenv source ${config.system.build.setEnvironment}
113
114 # clear fish_function_path so that it will be correctly set when we return to $__fish_datadir/config.fish
115 set -e fish_function_path
116 '';
117
118 environment.etc."fish/config.fish".text = ''
119 # /etc/fish/config.fish: DO NOT EDIT -- this file has been generated automatically.
120
121 # if we haven't sourced the general config, do it
122 if not set -q __fish_nixos_general_config_sourced
123 set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $fish_function_path
124 fenv source /etc/fish/foreign-env/shellInit > /dev/null
125 set -e fish_function_path[1]
126
127 ${cfg.shellInit}
128
129 # and leave a note so we don't source this config section again from
130 # this very shell (children will source the general config anew)
131 set -g __fish_nixos_general_config_sourced 1
132 end
133
134 # if we haven't sourced the login config, do it
135 status --is-login; and not set -q __fish_nixos_login_config_sourced
136 and begin
137 set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $fish_function_path
138 fenv source /etc/fish/foreign-env/loginShellInit > /dev/null
139 set -e fish_function_path[1]
140
141 ${cfg.loginShellInit}
142
143 # and leave a note so we don't source this config section again from
144 # this very shell (children will source the general config anew)
145 set -g __fish_nixos_login_config_sourced 1
146 end
147
148 # if we haven't sourced the interactive config, do it
149 status --is-interactive; and not set -q __fish_nixos_interactive_config_sourced
150 and begin
151 ${fishAliases}
152
153
154 set fish_function_path ${pkgs.fish-foreign-env}/share/fish-foreign-env/functions $fish_function_path
155 fenv source /etc/fish/foreign-env/interactiveShellInit > /dev/null
156 set -e fish_function_path[1]
157
158 ${cfg.promptInit}
159 ${cfg.interactiveShellInit}
160
161 # and leave a note so we don't source this config section again from
162 # this very shell (children will source the general config anew,
163 # allowing configuration changes in, e.g, aliases, to propagate)
164 set -g __fish_nixos_interactive_config_sourced 1
165 end
166 '';
167
168 # include programs that bring their own completions
169 environment.pathsToLink = []
170 ++ optional cfg.vendor.config.enable "/share/fish/vendor_conf.d"
171 ++ optional cfg.vendor.completions.enable "/share/fish/vendor_completions.d"
172 ++ optional cfg.vendor.functions.enable "/share/fish/vendor_functions.d";
173
174 environment.systemPackages = [ pkgs.fish ];
175
176 environment.shells = [
177 "/run/current-system/sw/bin/fish"
178 "/var/run/current-system/sw/bin/fish"
179 "${pkgs.fish}/bin/fish"
180 ];
181
182 };
183
184}