fork/home-manager
git clone https://git.julia.blue/fork/home-manager
31a27e48
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . fish ; 8 9pluginModule = types . submodule ({ config , ...} :{ 10options = { 11src = mkOption { 12type = types . path ; 13description = '' 14Path to the plugin folder. 15 16Relevant pieces will be added to the fish function path and 17the completion path. The {file}`init.fish` and 18{file}`key_binding.fish` files are sourced if 19they exist. 20'' ; 21}; 22 23name = mkOption { 24type = types . str ; 25description = '' 26The name of the plugin. 27'' ; 28}; 29}; 30}); 31 32functionModule = types . submodule { 33options = { 34body = mkOption { 35type = types . lines ; 36description = '' 37The function body. 38'' ; 39}; 40 41argumentNames = mkOption { 42type = with types ; nullOr ( either str ( listOf str )); 43default = null ; 44description = '' 45Assigns the value of successive command line arguments to the names 46given. 47'' ; 48}; 49 50description = mkOption { 51type = with types ; nullOr str ; 52default = null ; 53description = '' 54A description of what the function does, suitable as a completion 55description. 56'' ; 57}; 58 59wraps = mkOption { 60type = with types ; nullOr str ; 61default = null ; 62description = '' 63Causes the function to inherit completions from the given wrapped 64command. 65'' ; 66}; 67 68onEvent = mkOption { 69type = with types ; nullOr str ; 70default = null ; 71description = '' 72Tells fish to run this function when the specified named event is 73emitted. Fish internally generates named events e.g. when showing the 74prompt. 75'' ; 76}; 77 78onVariable = mkOption { 79type = with types ; nullOr str ; 80default = null ; 81description = '' 82Tells fish to run this function when the specified variable changes 83value. 84'' ; 85}; 86 87onJobExit = mkOption { 88type = with types ; nullOr ( either str int ); 89default = null ; 90description = '' 91Tells fish to run this function when the job with the specified group 92ID exits. Instead of a PID, the stringer `caller` can 93be specified. This is only legal when in a command substitution, and 94will result in the handler being triggered by the exit of the job 95which created this command substitution. 96'' ; 97}; 98 99onProcessExit = mkOption { 100type = with types ; nullOr ( either str int ); 101default = null ; 102example = "$fish_pid" ; 103description = '' 104Tells fish to run this function when the fish child process with the 105specified process ID exits. Instead of a PID, for backwards 106compatibility, `%self` can be specified as an alias 107for `$fish_pid`, and the function will be run when 108the current fish instance exits. 109'' ; 110}; 111 112onSignal = mkOption { 113type = with types ; nullOr ( either str int ); 114default = null ; 115example = [ "SIGHUP" "HUP" 1 ]; 116description = '' 117Tells fish to run this function when the specified signal is 118delievered. The signal can be a signal number or signal name. 119'' ; 120}; 121 122noScopeShadowing = mkOption { 123type = types . bool ; 124default = false ; 125description = '' 126Allows the function to access the variables of calling functions. 127'' ; 128}; 129 130inheritVariable = mkOption { 131type = with types ; nullOr str ; 132default = null ; 133description = '' 134Snapshots the value of the specified variable and defines a local 135variable with that same name and value when the function is defined. 136'' ; 137}; 138}; 139}; 140 141abbrsStr = concatStringsSep "\n" 142( mapAttrsToList ( k :v :"abbr --add --global -- ${ k } ${ escapeShellArg v } " ) 143cfg . shellAbbrs ); 144 145aliasesStr = concatStringsSep "\n" 146( mapAttrsToList ( k :v :"alias ${ k } ${ escapeShellArg v } " ) cfg . shellAliases ); 147 148fishIndent = name :text : 149pkgs . runCommand name { 150nativeBuildInputs = [ cfg . package ]; 151inherit text ; 152passAsFile = [ "text" ]; 153} "env HOME=$(mktemp -d) fish_indent < $textPath > $out" ; 154 155translatedSessionVariables = 156pkgs . runCommandLocal "hm-session-vars.fish" { } '' 157${ pkgs . buildPackages . babelfish } /bin/babelfish \ 158< ${ config . home . sessionVariablesPackage } /etc/profile.d/hm-session-vars.sh \ 159>$ out 160'' ; 161 162in { 163imports = [ 164( mkRemovedOptionModule [ "programs" "fish" "promptInit" ] '' 165Prompt is now configured through the 166 167programs.fish.interactiveShellInit 168 169option. Please change to use that instead. 170'' ) 171]; 172 173options = { 174programs . fish = { 175enable = mkEnableOption "fish, the friendly interactive shell" ; 176 177package = mkOption { 178type = types . package ; 179default = pkgs . fish ; 180defaultText = literalExpression "pkgs.fish" ; 181description = '' 182The fish package to install. May be used to change the version. 183'' ; 184}; 185 186shellAliases = mkOption { 187type = with types ; attrsOf str ; 188default = { }; 189example = literalExpression '' 190{ 191g = "git"; 192"..." = "cd ../.."; 193} 194'' ; 195description = '' 196An attribute set that maps aliases (the top level attribute names 197in this option) to command strings or directly to build outputs. 198'' ; 199}; 200 201shellAbbrs = mkOption { 202type = with types ; attrsOf str ; 203default = { }; 204example = { 205l = "less" ; 206gco = "git checkout" ; 207}; 208description = '' 209An attribute set that maps aliases (the top level attribute names 210in this option) to abbreviations. Abbreviations are expanded with 211the longer phrase after they are entered. 212'' ; 213}; 214 215shellInit = mkOption { 216type = types . lines ; 217default = "" ; 218description = '' 219Shell script code called during fish shell 220initialisation. 221'' ; 222}; 223 224loginShellInit = mkOption { 225type = types . lines ; 226default = "" ; 227description = '' 228Shell script code called during fish login shell 229initialisation. 230'' ; 231}; 232 233interactiveShellInit = mkOption { 234type = types . lines ; 235default = "" ; 236description = '' 237Shell script code called during interactive fish shell 238initialisation. 239'' ; 240}; 241}; 242 243programs . fish . plugins = mkOption { 244type = types . listOf pluginModule ; 245default = [ ]; 246example = literalExpression '' 247[ 248{ 249name = "z"; 250src = pkgs.fetchFromGitHub { 251owner = "jethrokuan"; 252repo = "z"; 253rev = "ddeb28a7b6a1f0ec6dae40c636e5ca4908ad160a"; 254sha256 = "0c5i7sdrsp0q3vbziqzdyqn4fmp235ax4mn4zslrswvn8g3fvdyh"; 255}; 256} 257 258# oh-my-fish plugins are stored in their own repositories, which 259# makes them simple to import into home-manager. 260{ 261name = "fasd"; 262src = pkgs.fetchFromGitHub { 263owner = "oh-my-fish"; 264repo = "plugin-fasd"; 265rev = "38a5b6b6011106092009549e52249c6d6f501fba"; 266sha256 = "06v37hqy5yrv5a6ssd1p3cjd9y3hnp19d3ab7dag56fs1qmgyhbs"; 267}; 268} 269] 270'' ; 271description = '' 272The plugins to source in 273{file}`conf.d/99plugins.fish`. 274'' ; 275}; 276 277programs . fish . functions = mkOption { 278type = with types ; attrsOf ( either lines functionModule ); 279default = { }; 280example = literalExpression '' 281{ 282__fish_command_not_found_handler = { 283body = "__fish_default_command_not_found_handler $argv[1]"; 284onEvent = "fish_command_not_found"; 285}; 286 287gitignore = "curl -sL https://www.gitignore.io/api/$argv"; 288} 289'' ; 290description = '' 291Basic functions to add to fish. For more information see 292<https://fishshell.com/docs/current/cmds/function.html>. 293'' ; 294}; 295}; 296 297config = mkIf cfg . enable ( mkMerge [ 298{ 299home . packages = [ cfg . package ]; 300 301# Support completion for `man` by building a cache for `apropos`. 302programs . man . generateCaches = mkDefault true ; 303 304xdg . dataFile . "fish/home-manager_generated_completions" . source = let 305# paths later in the list will overwrite those already linked 306destructiveSymlinkJoin = args_ @{ name , paths , preferLocalBuild ? true 307, allowSubstitutes ? false , postBuild ? "" , ...} : 308let 309args = removeAttrs args_ [ "name" "postBuild" ] // { 310# pass the defaults 311inherit preferLocalBuild allowSubstitutes ; 312}; 313in pkgs . runCommand name args '' 314mkdir -p $ out 315for i in $ paths ; do 316if [ -z " $( find $ i -prune -empty ) " ]; then 317cp -srf $ i /* $ out 318fi 319done 320${ postBuild } 321'' ; 322 323generateCompletions = let 324getName = attrs : 325attrs . name or " ${ attrs . pname or "«pname-missing»" } - ${ 326attrs . version or "«version-missing»" 327} " ; 328in package : 329pkgs . runCommand " ${ getName package } -fish-completions" { 330srcs = [ package ] ++ filter ( p :p != null ) 331( builtins . map ( outName :package .${ outName } or null ) 332config . home . extraOutputsToInstall ); 333nativeBuildInputs = [ pkgs . python3 ]; 334buildInputs = [ cfg . package ]; 335preferLocalBuild = true ; 336} '' 337mkdir -p $ out 338for src in $ srcs ; do 339if [ -d $ src /share/man ]; then 340find -L $ src /share/man -type f \ 341| xargs python ${ cfg . package } /share/fish/tools/create_manpage_completions.py --directory $ out \ 342> /dev/null 343fi 344done 345'' ; 346in destructiveSymlinkJoin { 347name = " ${ config . home . username } -fish-completions" ; 348paths = 349let cmp = ( a :b :( a . meta . priority or 0 ) > ( b . meta . priority or 0 )); 350in map generateCompletions ( sort cmp config . home . packages ); 351}; 352 353programs . fish . interactiveShellInit = '' 354# add completions generated by Home Manager to $fish_complete_path 355begin 356set -l joined (string join " " $fish_complete_path) 357set -l prev_joined (string replace --regex "[^\s]*generated_completions.*" "" $joined) 358set -l post_joined (string replace $prev_joined "" $joined) 359set -l prev (string split " " (string trim $prev_joined)) 360set -l post (string split " " (string trim $post_joined)) 361set fish_complete_path $prev " ${ config . xdg . dataHome } /fish/home-manager_generated_completions" $post 362end 363'' ; 364 365xdg . configFile . "fish/config.fish" . source = fishIndent "config.fish" '' 366# ~/.config/fish/config.fish: DO NOT EDIT -- this file has been generated 367# automatically by home-manager. 368 369# Only execute this file once per shell. 370set -q __fish_home_manager_config_sourced; and exit 371set -g __fish_home_manager_config_sourced 1 372 373source ${ translatedSessionVariables } 374 375${ cfg . shellInit } 376 377status --is-login; and begin 378 379# Login shell initialisation 380${ cfg . loginShellInit } 381 382end 383 384status --is-interactive; and begin 385 386# Abbreviations 387${ abbrsStr } 388 389# Aliases 390${ aliasesStr } 391 392# Interactive shell initialisation 393${ cfg . interactiveShellInit } 394 395end 396'' ; 397} 398{ 399xdg . configFile = mapAttrs' ( name :def :{ 400name = "fish/functions/ ${ name } .fish" ; 401value = { 402source = let 403modifierStr = n :v :optional ( v != null ) ''-- ${ n } =" ${ toString v } "'' ; 404modifierStrs = n :v :optional ( v != null ) "-- ${ n } = ${ toString v } " ; 405modifierBool = n :v :optional ( v != null && v ) "-- ${ n } " ; 406 407mods = with def ; 408modifierStr "description" description ++ modifierStr "wraps" wraps 409++ modifierStr "on-event" onEvent 410++ modifierStr "on-variable" onVariable 411++ modifierStr "on-job-exit" onJobExit 412++ modifierStr "on-process-exit" onProcessExit 413++ modifierStr "on-signal" onSignal 414++ modifierBool "no-scope-shadowing" noScopeShadowing 415++ modifierStr "inherit-variable" inheritVariable 416++ modifierStrs "argument-names" argumentNames ; 417 418modifiers = if isAttrs def then " ${ toString mods } " else "" ; 419body = if isAttrs def then def . body else def ; 420in fishIndent " ${ name } .fish" '' 421function ${ name }${ modifiers } 422${ lib . strings . removeSuffix "\n" body } 423end 424'' ; 425}; 426}) cfg . functions ; 427} 428 429# Each plugin gets a corresponding conf.d/plugin-NAME.fish file to load 430# in the paths and any initialization scripts. 431( mkIf ( length cfg . plugins > 0 ) { 432xdg . configFile = mkMerge (( map ( plugin :{ 433"fish/conf.d/plugin- ${ plugin . name } .fish" . source = 434fishIndent " ${ plugin . name } .fish" '' 435# Plugin ${ plugin . name } 436set -l plugin_dir ${ plugin . src } 437 438# Set paths to import plugin components 439if test -d $plugin_dir/functions 440set fish_function_path $fish_function_path[1] $plugin_dir/functions $fish_function_path[2..-1] 441end 442 443if test -d $plugin_dir/completions 444set fish_complete_path $fish_complete_path[1] $plugin_dir/completions $fish_complete_path[2..-1] 445end 446 447# Source initialization code if it exists. 448if test -d $plugin_dir/conf.d 449for f in $plugin_dir/conf.d/*.fish 450source $f 451end 452end 453 454if test -f $plugin_dir/key_bindings.fish 455source $plugin_dir/key_bindings.fish 456end 457 458if test -f $plugin_dir/init.fish 459source $plugin_dir/init.fish 460end 461'' ; 462}) cfg . plugins )); 463}) 464]); 465}