fork/home-manager
git clone https://git.julia.blue/fork/home-manager
9bc7d84b
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . zsh ; 8 9relToDotDir = file :( optionalString ( cfg . dotDir != null ) ( cfg . dotDir + "/" )) + file ; 10 11pluginsDir = if cfg . dotDir != null then 12relToDotDir "plugins" else ".zsh/plugins" ; 13 14envVarsStr = config . lib . zsh . exportAll cfg . sessionVariables ; 15localVarsStr = config . lib . zsh . defineAll cfg . localVariables ; 16 17aliasesStr = concatStringsSep "\n" ( 18mapAttrsToList ( k :v :"alias ${ k } = ${ lib . escapeShellArg v } " ) cfg . shellAliases 19); 20 21dirHashesStr = concatStringsSep "\n" ( 22mapAttrsToList ( k :v :''hash -d ${ k } =" ${ v } "'' ) cfg . dirHashes 23); 24 25zdotdir = "$HOME/" + cfg . dotDir ; 26 27bindkeyCommands = { 28emacs = "bindkey -e" ; 29viins = "bindkey -v" ; 30vicmd = "bindkey -a" ; 31}; 32 33stateVersion = config . home . stateVersion ; 34 35historyModule = types . submodule ({ config , ...} :{ 36options = { 37size = mkOption { 38type = types . int ; 39default = 10000 ; 40description = "Number of history lines to keep." ; 41}; 42 43save = mkOption { 44type = types . int ; 45defaultText = 10000 ; 46default = config . size ; 47description = "Number of history lines to save." ; 48}; 49 50path = mkOption { 51type = types . str ; 52default = if versionAtLeast stateVersion "20.03" 53then "$HOME/.zsh_history" 54else relToDotDir ".zsh_history" ; 55defaultText = literalExpression '' 56"$HOME/.zsh_history" if state version ≥ 20.03, 57"$ZDOTDIR/.zsh_history" otherwise 58'' ; 59example = literalExpression ''"''${config.xdg.dataHome}/zsh/zsh_history"'' ; 60description = "History file location" ; 61}; 62 63ignorePatterns = mkOption { 64type = types . listOf types . str ; 65default = []; 66example = literalExpression ''[ "rm *" "pkill *" ]'' ; 67description = '' 68Do not enter command lines into the history list 69if they match any one of the given shell patterns. 70'' ; 71}; 72 73ignoreDups = mkOption { 74type = types . bool ; 75default = true ; 76description = '' 77Do not enter command lines into the history list 78if they are duplicates of the previous event. 79'' ; 80}; 81 82ignoreAllDups = mkOption { 83type = types . bool ; 84default = false ; 85description = '' 86If a new command line being added to the history list 87duplicates an older one, the older command is removed 88from the list (even if it is not the previous event). 89'' ; 90}; 91 92ignoreSpace = mkOption { 93type = types . bool ; 94default = true ; 95description = '' 96Do not enter command lines into the history list 97if the first character is a space. 98'' ; 99}; 100 101expireDuplicatesFirst = mkOption { 102type = types . bool ; 103default = false ; 104description = "Expire duplicates first." ; 105}; 106 107extended = mkOption { 108type = types . bool ; 109default = false ; 110description = "Save timestamp into the history file." ; 111}; 112 113share = mkOption { 114type = types . bool ; 115default = true ; 116description = "Share command history between zsh sessions." ; 117}; 118}; 119}); 120 121pluginModule = types . submodule ({ config , ...} :{ 122options = { 123src = mkOption { 124type = types . path ; 125description = '' 126Path to the plugin folder. 127 128Will be added to {env}`fpath` and {env}`PATH`. 129'' ; 130}; 131 132name = mkOption { 133type = types . str ; 134description = '' 135The name of the plugin. 136 137Don't forget to add {option}`file` 138if the script name does not follow convention. 139'' ; 140}; 141 142file = mkOption { 143type = types . str ; 144description = "The plugin script to source." ; 145}; 146}; 147 148config . file = mkDefault " ${ config . name } .plugin.zsh" ; 149}); 150 151ohMyZshModule = types . submodule { 152options = { 153enable = mkEnableOption "oh-my-zsh" ; 154 155package = mkPackageOption pkgs "oh-my-zsh" { }; 156 157plugins = mkOption { 158default = []; 159example = [ "git" "sudo" ]; 160type = types . listOf types . str ; 161description = '' 162List of oh-my-zsh plugins 163'' ; 164}; 165 166custom = mkOption { 167default = "" ; 168type = types . str ; 169example = "$HOME/my_customizations" ; 170description = '' 171Path to a custom oh-my-zsh package to override config of 172oh-my-zsh. See <https://github.com/robbyrussell/oh-my-zsh/wiki/Customization> 173for more information. 174'' ; 175}; 176 177theme = mkOption { 178default = "" ; 179example = "robbyrussell" ; 180type = types . str ; 181description = '' 182Name of the theme to be used by oh-my-zsh. 183'' ; 184}; 185 186extraConfig = mkOption { 187default = "" ; 188example = '' 189zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github 190'' ; 191type = types . lines ; 192description = '' 193Extra settings for plugins. 194'' ; 195}; 196}; 197}; 198 199historySubstringSearchModule = types . submodule { 200options = { 201enable = mkEnableOption "history substring search" ; 202searchUpKey = mkOption { 203type = with types ; either ( listOf str ) str ; 204default = [ "^[[A" ]; 205description = '' 206The key codes to be used when searching up. 207The default of `^[[A` may correspond to the UP key -- if not, try 208`$terminfo[kcuu1]`. 209'' ; 210}; 211searchDownKey = mkOption { 212type = with types ; either ( listOf str ) str ; 213default = [ "^[[B" ]; 214description = '' 215The key codes to be used when searching down. 216The default of `^[[B` may correspond to the DOWN key -- if not, try 217`$terminfo[kcud1]`. 218'' ; 219}; 220}; 221}; 222 223syntaxHighlightingModule = types . submodule { 224options = { 225enable = mkEnableOption "zsh syntax highlighting" ; 226 227package = mkPackageOption pkgs "zsh-syntax-highlighting" { }; 228 229styles = mkOption { 230type = types . attrsOf types . str ; 231default = {}; 232description = '' 233Custom styles for syntax highlighting. 234See each highlighter's options: <https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md> 235'' ; 236}; 237}; 238}; 239 240in 241 242{ 243imports = [ 244( mkRenamedOptionModule [ "programs" "zsh" "enableSyntaxHighlighting" ] [ "programs" "zsh" "syntaxHighlighting" "enable" ]) 245]; 246 247options = { 248programs . zsh = { 249enable = mkEnableOption "Z shell (Zsh)" ; 250 251package = mkPackageOption pkgs "zsh" { }; 252 253autocd = mkOption { 254default = null ; 255description = '' 256Automatically enter into a directory if typed directly into shell. 257'' ; 258type = types . nullOr types . bool ; 259}; 260 261cdpath = mkOption { 262default = []; 263description = '' 264List of paths to autocomplete calls to {command}`cd`. 265'' ; 266type = types . listOf types . str ; 267}; 268 269dotDir = mkOption { 270default = null ; 271example = ".config/zsh" ; 272description = '' 273Directory where the zsh configuration and more should be located, 274relative to the users home directory. The default is the home 275directory. 276'' ; 277type = types . nullOr types . str ; 278}; 279 280shellAliases = mkOption { 281default = {}; 282example = literalExpression '' 283{ 284ll = "ls -l"; 285".." = "cd .."; 286} 287'' ; 288description = '' 289An attribute set that maps aliases (the top level attribute names in 290this option) to command strings or directly to build outputs. 291'' ; 292type = types . attrsOf types . str ; 293}; 294 295shellGlobalAliases = mkOption { 296default = {}; 297example = literalExpression '' 298{ 299UUID = "$(uuidgen | tr -d \\n)"; 300G = "| grep"; 301} 302'' ; 303description = '' 304Similar to [](#opt-programs.zsh.shellAliases), 305but are substituted anywhere on a line. 306'' ; 307type = types . attrsOf types . str ; 308}; 309 310dirHashes = mkOption { 311default = {}; 312example = literalExpression '' 313{ 314docs = "$HOME/Documents"; 315vids = "$HOME/Videos"; 316dl = "$HOME/Downloads"; 317} 318'' ; 319description = '' 320An attribute set that adds to named directory hash table. 321'' ; 322type = types . attrsOf types . str ; 323}; 324 325enableCompletion = mkOption { 326default = true ; 327description = '' 328Enable zsh completion. Don't forget to add 329```nix 330environment.pathsToLink = [ "/share/zsh" ]; 331``` 332to your system configuration to get completion for system packages (e.g. systemd). 333'' ; 334type = types . bool ; 335}; 336 337completionInit = mkOption { 338default = "autoload -U compinit && compinit" ; 339description = "Initialization commands to run when completion is enabled." ; 340type = types . lines ; 341}; 342 343enableAutosuggestions = mkOption { 344default = false ; 345description = "Enable zsh autosuggestions" ; 346}; 347 348syntaxHighlighting = mkOption { 349type = syntaxHighlightingModule ; 350default = {}; 351description = "Options related to zsh-syntax-highlighting." ; 352}; 353 354historySubstringSearch = mkOption { 355type = historySubstringSearchModule ; 356default = {}; 357description = "Options related to zsh-history-substring-search." ; 358}; 359 360history = mkOption { 361type = historyModule ; 362default = {}; 363description = "Options related to commands history configuration." ; 364}; 365 366defaultKeymap = mkOption { 367type = types . nullOr ( types . enum ( attrNames bindkeyCommands )); 368default = null ; 369example = "emacs" ; 370description = "The default base keymap to use." ; 371}; 372 373sessionVariables = mkOption { 374default = {}; 375type = types . attrs ; 376example = { MAILCHECK = 30 ; }; 377description = "Environment variables that will be set for zsh session." ; 378}; 379 380initExtraBeforeCompInit = mkOption { 381default = "" ; 382type = types . lines ; 383description = "Extra commands that should be added to {file}`.zshrc` before compinit." ; 384}; 385 386initExtra = mkOption { 387default = "" ; 388type = types . lines ; 389description = "Extra commands that should be added to {file}`.zshrc`." ; 390}; 391 392initExtraFirst = mkOption { 393default = "" ; 394type = types . lines ; 395description = "Commands that should be added to top of {file}`.zshrc`." ; 396}; 397 398envExtra = mkOption { 399default = "" ; 400type = types . lines ; 401description = "Extra commands that should be added to {file}`.zshenv`." ; 402}; 403 404profileExtra = mkOption { 405default = "" ; 406type = types . lines ; 407description = "Extra commands that should be added to {file}`.zprofile`." ; 408}; 409 410loginExtra = mkOption { 411default = "" ; 412type = types . lines ; 413description = "Extra commands that should be added to {file}`.zlogin`." ; 414}; 415 416logoutExtra = mkOption { 417default = "" ; 418type = types . lines ; 419description = "Extra commands that should be added to {file}`.zlogout`." ; 420}; 421 422plugins = mkOption { 423type = types . listOf pluginModule ; 424default = []; 425example = literalExpression '' 426[ 427{ 428# will source zsh-autosuggestions.plugin.zsh 429name = "zsh-autosuggestions"; 430src = pkgs.fetchFromGitHub { 431owner = "zsh-users"; 432repo = "zsh-autosuggestions"; 433rev = "v0.4.0"; 434sha256 = "0z6i9wjjklb4lvr7zjhbphibsyx51psv50gm07mbb0kj9058j6kc"; 435}; 436} 437{ 438name = "enhancd"; 439file = "init.sh"; 440src = pkgs.fetchFromGitHub { 441owner = "b4b4r07"; 442repo = "enhancd"; 443rev = "v2.2.1"; 444sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g"; 445}; 446} 447] 448'' ; 449description = "Plugins to source in {file}`.zshrc`." ; 450}; 451 452oh-my-zsh = mkOption { 453type = ohMyZshModule ; 454default = {}; 455description = "Options to configure oh-my-zsh." ; 456}; 457 458localVariables = mkOption { 459type = types . attrs ; 460default = {}; 461example = { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS =[ "dir" "vcs" ]; }; 462description = '' 463Extra local variables defined at the top of {file}`.zshrc`. 464'' ; 465}; 466}; 467}; 468 469config = mkIf cfg . enable ( mkMerge [ 470( mkIf ( cfg . envExtra != "" ) { 471home . file . " ${ relToDotDir ".zshenv" } " . text = cfg . envExtra ; 472}) 473 474( mkIf ( cfg . profileExtra != "" ) { 475home . file . " ${ relToDotDir ".zprofile" } " . text = cfg . profileExtra ; 476}) 477 478( mkIf ( cfg . loginExtra != "" ) { 479home . file . " ${ relToDotDir ".zlogin" } " . text = cfg . loginExtra ; 480}) 481 482( mkIf ( cfg . logoutExtra != "" ) { 483home . file . " ${ relToDotDir ".zlogout" } " . text = cfg . logoutExtra ; 484}) 485 486( mkIf cfg . oh-my-zsh . enable { 487home . file . " ${ relToDotDir ".zshenv" } " . text = '' 488ZSH=" ${ cfg . oh-my-zsh . package } /share/oh-my-zsh"; 489ZSH_CACHE_DIR=" ${ config . xdg . cacheHome } /oh-my-zsh"; 490'' ; 491}) 492 493( mkIf ( cfg . dotDir != null ) { 494home . file . " ${ relToDotDir ".zshenv" } " . text = '' 495export ZDOTDIR= ${ zdotdir } 496'' ; 497 498# When dotDir is set, only use ~/.zshenv to source ZDOTDIR/.zshenv, 499# This is so that if ZDOTDIR happens to be 500# already set correctly (by e.g. spawning a zsh inside a zsh), all env 501# vars still get exported 502home . file . ".zshenv" . text = '' 503source ${ zdotdir } /.zshenv 504'' ; 505}) 506 507{ 508home . file . " ${ relToDotDir ".zshenv" } " . text = '' 509# Environment variables 510. " ${ config . home . profileDirectory } /etc/profile.d/hm-session-vars.sh" 511 512# Only source this once 513if [[ -z "$__HM_ZSH_SESS_VARS_SOURCED" ]]; then 514export __HM_ZSH_SESS_VARS_SOURCED=1 515${ envVarsStr } 516fi 517'' ; 518} 519 520{ 521home . packages = [ cfg . package ] 522++ optional cfg . enableCompletion pkgs . nix-zsh-completions 523++ optional cfg . oh-my-zsh . enable cfg . oh-my-zsh . package ; 524 525home . file . " ${ relToDotDir ".zshrc" } " . text = concatStringsSep "\n" ([ 526cfg . initExtraFirst 527"typeset -U path cdpath fpath manpath" 528 529( optionalString ( cfg . cdpath != []) '' 530cdpath+=( ${ concatStringsSep " " cfg . cdpath } ) 531'' ) 532 533'' 534for profile in ''${(z)NIX_PROFILES}; do 535fpath+=($profile/share/zsh/site-functions $profile/share/zsh/$ZSH_VERSION/functions $profile/share/zsh/vendor-completions) 536done 537 538HELPDIR=" ${ cfg . package } /share/zsh/$ZSH_VERSION/help" 539'' 540 541( optionalString ( cfg . defaultKeymap != null ) '' 542# Use ${ cfg . defaultKeymap } keymap as the default. 543${ getAttr cfg . defaultKeymap bindkeyCommands } 544'' ) 545localVarsStr 546 547cfg . initExtraBeforeCompInit 548 549( concatStrings ( map ( plugin :'' 550path+="$HOME/ ${ pluginsDir } / ${ plugin . name } " 551fpath+="$HOME/ ${ pluginsDir } / ${ plugin . name } " 552'' ) cfg . plugins )) 553 554'' 555# Oh-My-Zsh/Prezto calls compinit during initialization, 556# calling it twice causes slight start up slowdown 557# as all $fpath entries will be traversed again. 558${ optionalString ( cfg . enableCompletion && ! cfg . oh-my-zsh . enable && ! cfg . prezto . enable ) 559cfg . completionInit 560} 561 562${ optionalString cfg . enableAutosuggestions 563"source ${ pkgs . zsh-autosuggestions } /share/zsh-autosuggestions/zsh-autosuggestions.zsh" 564} 565 566${ optionalString cfg . oh-my-zsh . enable '' 567# oh-my-zsh extra settings for plugins 568${ cfg . oh-my-zsh . extraConfig } 569# oh-my-zsh configuration generated by NixOS 570${ optionalString ( cfg . oh-my-zsh . plugins != []) 571"plugins=( ${ concatStringsSep " " cfg . oh-my-zsh . plugins } )" 572} 573${ optionalString ( cfg . oh-my-zsh . custom != "" ) 574"ZSH_CUSTOM=\" ${ cfg . oh-my-zsh . custom } \"" 575} 576${ optionalString ( cfg . oh-my-zsh . theme != "" ) 577"ZSH_THEME=\" ${ cfg . oh-my-zsh . theme } \"" 578} 579source $ZSH/oh-my-zsh.sh 580'' } 581 582${ optionalString cfg . prezto . enable 583( builtins . readFile " ${ pkgs . zsh-prezto } /share/zsh-prezto/runcoms/zshrc" )} 584 585${ concatStrings ( map ( plugin : '' 586if [[ -f "$HOME/ ${ pluginsDir } / ${ plugin . name } / ${ plugin . file } " ]]; then 587source "$HOME/ ${ pluginsDir } / ${ plugin . name } / ${ plugin . file } " 588fi 589'' ) cfg . plugins )} 590 591# History options should be set in .zshrc and after oh-my-zsh sourcing. 592# See https://github.com/nix-community/home-manager/issues/177. 593HISTSIZE=" ${ toString cfg . history . size } " 594SAVEHIST=" ${ toString cfg . history . save } " 595${ optionalString ( cfg . history . ignorePatterns != []) "HISTORY_IGNORE= ${ lib . escapeShellArg "( ${ lib . concatStringsSep "|" cfg . history . ignorePatterns } )" } " } 596${ if versionAtLeast config . home . stateVersion "20.03" 597then ''HISTFILE=" ${ cfg . history . path } "'' 598else ''HISTFILE="$HOME/ ${ cfg . history . path } "'' } 599mkdir -p "$(dirname "$HISTFILE")" 600 601setopt HIST_FCNTL_LOCK 602${ if cfg . history . ignoreDups then "setopt" else "unsetopt" } HIST_IGNORE_DUPS 603${ if cfg . history . ignoreAllDups then "setopt" else "unsetopt" } HIST_IGNORE_ALL_DUPS 604${ if cfg . history . ignoreSpace then "setopt" else "unsetopt" } HIST_IGNORE_SPACE 605${ if cfg . history . expireDuplicatesFirst then "setopt" else "unsetopt" } HIST_EXPIRE_DUPS_FIRST 606${ if cfg . history . share then "setopt" else "unsetopt" } SHARE_HISTORY 607${ if cfg . history . extended then "setopt" else "unsetopt" } EXTENDED_HISTORY 608${ if cfg . autocd != null then " ${ if cfg . autocd then "setopt" else "unsetopt" } autocd" else "" } 609 610${ cfg . initExtra } 611 612# Aliases 613${ aliasesStr } 614'' 615] 616++ ( mapAttrsToList ( k :v :"alias -g ${ k } = ${ lib . escapeShellArg v } " ) cfg . shellGlobalAliases ) 617++ [ ( '' 618# Named Directory Hashes 619${ dirHashesStr } 620'' ) 621 622( optionalString cfg . syntaxHighlighting . enable 623# Load zsh-syntax-highlighting after all custom widgets have been created 624# https://github.com/zsh-users/zsh-syntax-highlighting#faq 625'' 626source ${ cfg . syntaxHighlighting . package } /share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 627${ lib . concatStringsSep "\n" ( 628lib . mapAttrsToList 629( name : value : "ZSH_HIGHLIGHT_STYLES+=( ${ lib . escapeShellArg name } ${ lib . escapeShellArg value } )" ) 630cfg . syntaxHighlighting . styles 631)} 632'' ) 633 634( optionalString ( cfg . historySubstringSearch . enable or false ) 635# Load zsh-history-substring-search after zsh-syntax-highlighting 636# https://github.com/zsh-users/zsh-history-substring-search#usage 637'' 638source ${ pkgs . zsh-history-substring-search } /share/zsh-history-substring-search/zsh-history-substring-search.zsh 639${ lib . concatMapStringsSep "\n" 640( upKey : "bindkey \" ${ upKey } \" history-substring-search-up" ) 641( lib . toList cfg . historySubstringSearch . searchUpKey ) 642} 643${ lib . concatMapStringsSep "\n" 644( downKey : "bindkey \" ${ downKey } \" history-substring-search-down" ) 645( lib . toList cfg . historySubstringSearch . searchDownKey ) 646} 647'' ) 648]); 649} 650 651( mkIf cfg . oh-my-zsh . enable { 652# Make sure we create a cache directory since some plugins expect it to exist 653# See: https://github.com/nix-community/home-manager/issues/761 654home . file . " ${ config . xdg . cacheHome } /oh-my-zsh/.keep" . text = "" ; 655}) 656 657( mkIf ( cfg . plugins != []) { 658# Many plugins require compinit to be called 659# but allow the user to opt out. 660programs . zsh . enableCompletion = mkDefault true ; 661 662home . file = 663foldl' ( a :b :a // b ) {} 664( map ( plugin :{ " ${ pluginsDir } / ${ plugin . name } " . source = plugin . src ; }) 665cfg . plugins ); 666}) 667]); 668}