1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

zsh: group plugins in a separate directory

Make it more scalable to prevent crowding the main folder.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman 2025-07-11 23:17:30 -05:00
parent 26b987cf88
commit 196487c54f
7 changed files with 145 additions and 112 deletions

View file

@ -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
);
})
]
);
}

View file

@ -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
)
))
];
};
};
}

View file

@ -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