diff --git a/modules/programs/zsh/default.nix b/modules/programs/zsh/default.nix index 459ca7333..282022b76 100644 --- a/modules/programs/zsh/default.nix +++ b/modules/programs/zsh/default.nix @@ -28,56 +28,13 @@ let in { imports = [ + ./plugins ./deprecated.nix ./history.nix - ./oh-my-zsh.nix - ./prezto.nix - ./zprof.nix - ./zsh-abbr.nix ]; options = let - pluginModule = types.submodule ( - { config, ... }: - { - options = { - src = mkOption { - type = types.path; - description = '' - Path to the plugin folder. - - Will be added to {env}`fpath` and {env}`PATH`. - ''; - }; - - name = mkOption { - type = types.str; - description = '' - The name of the plugin. - ''; - }; - - file = mkOption { - type = types.str; - description = '' - The plugin script to source. - Required if the script name does not match {file}`name.plugin.zsh` - using the plugin {option}`name` from the plugin {option}`src`. - ''; - }; - - completions = mkOption { - default = [ ]; - type = types.listOf types.str; - description = "Paths of additional functions to add to {env}`fpath`."; - }; - }; - - config.file = lib.mkDefault "${config.name}.plugin.zsh"; - } - ); - syntaxHighlightingModule = types.submodule { options = { enable = mkEnableOption "zsh syntax highlighting"; @@ -336,32 +293,6 @@ in description = "Extra commands that should be added to {file}`.zlogout`."; }; - plugins = mkOption { - type = types.listOf pluginModule; - default = [ ]; - example = literalExpression '' - [ - { - name = "enhancd"; - file = "init.sh"; - src = pkgs.fetchFromGitHub { - owner = "b4b4r07"; - repo = "enhancd"; - rev = "v2.2.1"; - sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g"; - }; - } - { - name = "wd"; - src = pkgs.zsh-wd; - file = "share/wd/wd.plugin.zsh"; - completions = [ "share/zsh/site-functions" ]; - } - ] - ''; - description = "Plugins to source in {file}`.zshrc`."; - }; - localVariables = mkOption { type = types.attrs; default = { }; @@ -396,8 +327,6 @@ in config = let - pluginsDir = if cfg.dotDir != null then relToDotDir "plugins" else ".zsh/plugins"; - envVarsStr = config.lib.zsh.exportAll cfg.sessionVariables; localVarsStr = config.lib.zsh.defineAll cfg.localVariables; @@ -487,26 +416,6 @@ in (lib.mkIf (localVarsStr != "") (mkOrder 540 localVarsStr)) - (lib.mkIf (cfg.plugins != [ ]) ( - mkOrder 560 ( - lib.concatStrings ( - map (plugin: '' - path+="$HOME/${pluginsDir}/${plugin.name}" - fpath+="$HOME/${pluginsDir}/${plugin.name}" - ${ - (optionalString (plugin.completions != [ ]) '' - fpath+=(${ - lib.concatMapStringsSep " " ( - completion: "\"$HOME/${pluginsDir}/${plugin.name}/${completion}\"" - ) plugin.completions - }) - '') - } - '') cfg.plugins - ) - ) - )) - # NOTE: Oh-My-Zsh/Prezto calls compinit during initialization, # calling it twice causes slight start up slowdown # as all $fpath entries will be traversed again. @@ -529,16 +438,6 @@ in '' )) - (mkOrder 900 ( - lib.concatStrings ( - map (plugin: '' - if [[ -f "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" ]]; then - source "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" - fi - '') cfg.plugins - ) - )) - (lib.mkIf (cfg.setOptions != [ ]) ( mkOrder 950 '' ${concatStringsSep "\n" (map (option: "setopt ${option}") cfg.setOptions)} @@ -589,16 +488,6 @@ in home.file."${relToDotDir ".zshrc"}".text = cfg.initContent; } - - (mkIf (cfg.plugins != [ ]) { - # Many plugins require compinit to be called - # but allow the user to opt out. - programs.zsh.enableCompletion = lib.mkDefault true; - - home.file = lib.foldl' (a: b: a // b) { } ( - map (plugin: { "${pluginsDir}/${plugin.name}".source = plugin.src; }) cfg.plugins - ); - }) ] ); } diff --git a/modules/programs/zsh/plugins/default.nix b/modules/programs/zsh/plugins/default.nix new file mode 100644 index 000000000..f51918c27 --- /dev/null +++ b/modules/programs/zsh/plugins/default.nix @@ -0,0 +1,136 @@ +{ + config, + lib, + ... +}: +let + inherit (lib) mkOption types; + + cfg = config.programs.zsh; + + relToDotDir = file: (lib.optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file; +in +{ + imports = [ + ./oh-my-zsh.nix + ./prezto.nix + ./zprof.nix + ./zsh-abbr.nix + ]; + + options = + let + pluginModule = types.submodule ( + { config, ... }: + { + options = { + src = mkOption { + type = types.path; + description = '' + Path to the plugin folder. + + Will be added to {env}`fpath` and {env}`PATH`. + ''; + }; + + name = mkOption { + type = types.str; + description = '' + The name of the plugin. + ''; + }; + + file = mkOption { + type = types.str; + description = '' + The plugin script to source. + Required if the script name does not match {file}`name.plugin.zsh` + using the plugin {option}`name` from the plugin {option}`src`. + ''; + }; + + completions = mkOption { + default = [ ]; + type = types.listOf types.str; + description = "Paths of additional functions to add to {env}`fpath`."; + }; + }; + + config.file = lib.mkDefault "${config.name}.plugin.zsh"; + } + ); + in + { + programs.zsh.plugins = mkOption { + type = types.listOf pluginModule; + default = [ ]; + example = lib.literalExpression '' + [ + { + name = "enhancd"; + file = "init.sh"; + src = pkgs.fetchFromGitHub { + owner = "b4b4r07"; + repo = "enhancd"; + rev = "v2.2.1"; + sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g"; + }; + } + { + name = "wd"; + src = pkgs.zsh-wd; + file = "share/wd/wd.plugin.zsh"; + completions = [ "share/zsh/site-functions" ]; + } + ] + ''; + description = "Plugins to source in {file}`.zshrc`."; + }; + }; + + config = + let + pluginsDir = if cfg.dotDir != null then relToDotDir "plugins" else ".zsh/plugins"; + in + lib.mkIf (cfg.plugins != [ ]) { + home.file = lib.foldl' (a: b: a // b) { } ( + map (plugin: { "${pluginsDir}/${plugin.name}".source = plugin.src; }) cfg.plugins + ); + + programs.zsh = { + # Many plugins require compinit to be called + # but allow the user to opt out. + enableCompletion = lib.mkDefault true; + + initContent = lib.mkMerge [ + (lib.mkOrder 560 ( + lib.concatStrings ( + map (plugin: '' + path+="$HOME/${pluginsDir}/${plugin.name}" + fpath+="$HOME/${pluginsDir}/${plugin.name}" + ${ + (lib.optionalString (plugin.completions != [ ]) '' + fpath+=(${ + lib.concatMapStringsSep " " ( + completion: "\"$HOME/${pluginsDir}/${plugin.name}/${completion}\"" + ) plugin.completions + }) + '') + } + '') cfg.plugins + ) + )) + + (lib.mkOrder 900 ( + lib.concatStrings ( + map (plugin: '' + if [[ -f "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" ]]; then + source "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" + fi + '') cfg.plugins + ) + )) + ]; + }; + }; +} diff --git a/modules/programs/zsh/oh-my-zsh.nix b/modules/programs/zsh/plugins/oh-my-zsh.nix similarity index 100% rename from modules/programs/zsh/oh-my-zsh.nix rename to modules/programs/zsh/plugins/oh-my-zsh.nix diff --git a/modules/programs/zsh/prezto.nix b/modules/programs/zsh/plugins/prezto.nix similarity index 100% rename from modules/programs/zsh/prezto.nix rename to modules/programs/zsh/plugins/prezto.nix diff --git a/modules/programs/zsh/zprof.nix b/modules/programs/zsh/plugins/zprof.nix similarity index 100% rename from modules/programs/zsh/zprof.nix rename to modules/programs/zsh/plugins/zprof.nix diff --git a/modules/programs/zsh/zsh-abbr.nix b/modules/programs/zsh/plugins/zsh-abbr.nix similarity index 100% rename from modules/programs/zsh/zsh-abbr.nix rename to modules/programs/zsh/plugins/zsh-abbr.nix diff --git a/tests/modules/programs/zsh/zsh-abbr.nix b/tests/modules/programs/zsh/zsh-abbr.nix index e0e092521..29f898f5f 100644 --- a/tests/modules/programs/zsh/zsh-abbr.nix +++ b/tests/modules/programs/zsh/zsh-abbr.nix @@ -6,6 +6,14 @@ }; }; + test.stubs = { + zsh-abbr = { + outPath = null; + buildScript = '' + mkdir -p $out/share/zsh-abbr/runcoms + ''; + }; + }; nmt.script = '' abbreviations=home-files/.config/zsh-abbr/user-abbreviations