at 24.11-pre 11 kB view raw
1{ config, lib, pkgs, ... }: 2 3let 4 5 cfge = config.environment; 6 7 cfg = config.programs.fish; 8 9 fishAbbrs = lib.concatStringsSep "\n" ( 10 lib.mapAttrsFlatten (k: v: "abbr -ag ${k} ${lib.escapeShellArg v}") 11 cfg.shellAbbrs 12 ); 13 14 fishAliases = lib.concatStringsSep "\n" ( 15 lib.mapAttrsFlatten (k: v: "alias ${k} ${lib.escapeShellArg v}") 16 (lib.filterAttrs (k: v: v != null) cfg.shellAliases) 17 ); 18 19 envShellInit = pkgs.writeText "shellInit" cfge.shellInit; 20 21 envLoginShellInit = pkgs.writeText "loginShellInit" cfge.loginShellInit; 22 23 envInteractiveShellInit = pkgs.writeText "interactiveShellInit" cfge.interactiveShellInit; 24 25 sourceEnv = file: 26 if cfg.useBabelfish then 27 "source /etc/fish/${file}.fish" 28 else 29 '' 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] 33 ''; 34 35 babelfishTranslate = path: name: 36 pkgs.runCommandLocal "${name}.fish" { 37 nativeBuildInputs = [ pkgs.babelfish ]; 38 } "babelfish < ${path} > $out;"; 39 40in 41 42{ 43 44 options = { 45 46 programs.fish = { 47 48 enable = lib.mkOption { 49 default = false; 50 description = '' 51 Whether to configure fish as an interactive shell. 52 ''; 53 type = lib.types.bool; 54 }; 55 56 package = lib.mkPackageOption pkgs "fish" { }; 57 58 useBabelfish = lib.mkOption { 59 type = lib.types.bool; 60 default = false; 61 description = '' 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 = lib.mkOption { 68 type = lib.types.bool; 69 default = true; 70 description = '' 71 Whether fish should source configuration snippets provided by other packages. 72 ''; 73 }; 74 75 vendor.completions.enable = lib.mkOption { 76 type = lib.types.bool; 77 default = true; 78 description = '' 79 Whether fish should use completion files provided by other packages. 80 ''; 81 }; 82 83 vendor.functions.enable = lib.mkOption { 84 type = lib.types.bool; 85 default = true; 86 description = '' 87 Whether fish should autoload fish functions provided by other packages. 88 ''; 89 }; 90 91 shellAbbrs = lib.mkOption { 92 default = {}; 93 example = { 94 gco = "git checkout"; 95 npu = "nix-prefetch-url"; 96 }; 97 description = '' 98 Set of fish abbreviations. 99 ''; 100 type = with lib.types; attrsOf str; 101 }; 102 103 shellAliases = lib.mkOption { 104 default = {}; 105 description = '' 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 lib.types; attrsOf (nullOr (either str path)); 110 }; 111 112 shellInit = lib.mkOption { 113 default = ""; 114 description = '' 115 Shell script code called during fish shell initialisation. 116 ''; 117 type = lib.types.lines; 118 }; 119 120 loginShellInit = lib.mkOption { 121 default = ""; 122 description = '' 123 Shell script code called during fish login shell initialisation. 124 ''; 125 type = lib.types.lines; 126 }; 127 128 interactiveShellInit = lib.mkOption { 129 default = ""; 130 description = '' 131 Shell script code called during interactive fish shell initialisation. 132 ''; 133 type = lib.types.lines; 134 }; 135 136 promptInit = lib.mkOption { 137 default = ""; 138 description = '' 139 Shell script code used to initialise fish prompt. 140 ''; 141 type = lib.types.lines; 142 }; 143 144 }; 145 146 }; 147 148 config = lib.mkIf cfg.enable { 149 150 programs.fish.shellAliases = lib.mapAttrs (name: lib.mkDefault) cfge.shellAliases; 151 152 # Required for man completions 153 documentation.man.generateCaches = lib.mkDefault true; 154 155 environment = lib.mkMerge [ 156 (lib.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 (lib.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 "${cfg.package}/share/fish/tools/create_manpage_completions.py" 248 "${cfg.package}/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.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; }) 268 '' 269 mkdir -p $out 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 272 fi 273 ''; 274 in 275 pkgs.buildEnv { 276 name = "system_fish-completions"; 277 ignoreCollisions = true; 278 paths = builtins.map generateCompletions config.environment.systemPackages; 279 }; 280 } 281 282 # include programs that bring their own completions 283 { 284 pathsToLink = [] 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"; 288 } 289 290 { systemPackages = [ cfg.package ]; } 291 292 { 293 shells = [ 294 "/run/current-system/sw/bin/fish" 295 (lib.getExe cfg.package) 296 ]; 297 } 298 ]; 299 300 programs.fish.interactiveShellInit = '' 301 # add completions generated by NixOS to $fish_complete_path 302 begin 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 308 end 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 312 end 313 ''; 314 315 }; 316 317}