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