zsh: add custom dotDir parameter

This commit is contained in:
Silvan Mosberger 2017-09-12 20:01:02 +02:00 committed by Nikita Uvarov
parent cab9237d95
commit de5f902487
No known key found for this signature in database
GPG key ID: F7A5FB3A7C10EF96

View file

@ -6,6 +6,12 @@ let
cfg = config.programs.zsh; cfg = config.programs.zsh;
relToDotDir = file: (optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file;
pluginsDir = if cfg.dotDir != null then
relToDotDir "plugins" else ".zsh/plugins";
historyModule = types.submodule { historyModule = types.submodule {
options = { options = {
size = mkOption { size = mkOption {
@ -16,7 +22,8 @@ let
path = mkOption { path = mkOption {
type = types.str; type = types.str;
default = "$HOME/.zsh_history"; default = relToDotDir ".zsh_history";
defaultText = ".zsh_history"; # Manual fails to build without this
description = "History file location"; description = "History file location";
}; };
@ -108,6 +115,17 @@ in
programs.zsh = { programs.zsh = {
enable = mkEnableOption "Z shell (Zsh)"; enable = mkEnableOption "Z shell (Zsh)";
dotDir = mkOption {
default = null;
example = ".config/zsh";
description = ''
Directory where the zsh configuration and more should be located,
relative to the users home directory. The default is the home
directory.
'';
type = types.nullOr types.str;
};
shellAliases = mkOption { shellAliases = mkOption {
default = {}; default = {};
example = { ll = "ls -l"; ".." = "cd .."; }; example = { ll = "ls -l"; ".." = "cd .."; };
@ -190,19 +208,25 @@ in
envVarsStr = concatStringsSep "\n" ( envVarsStr = concatStringsSep "\n" (
mapAttrsToList export config.home.sessionVariables mapAttrsToList export config.home.sessionVariables
); );
zdotdir = if cfg.dotDir == null then null else "$HOME/" + cfg.dotDir;
in mkIf cfg.enable { in mkIf cfg.enable {
home.packages = with pkgs; [ zsh ] home.packages = with pkgs; [ zsh ]
++ optional cfg.enableCompletion nix-zsh-completions ++ optional cfg.enableCompletion nix-zsh-completions
++ optional cfg.oh-my-zsh.enable oh-my-zsh; ++ optional cfg.oh-my-zsh.enable oh-my-zsh;
home.sessionVariables.${if cfg.dotDir != null then "ZDOTDIR" else null} = zdotdir;
home.file.".zshenv".text = '' home.file.".zshenv".text = ''
${optionalString (config.home.sessionVariableSetter == "zsh") ${optionalString (config.home.sessionVariableSetter == "zsh")
envVarsStr} envVarsStr}
${optionalString (cfg.dotDir != null && config.home.sessionVariableSetter != "zsh")
(export "ZDOTDIR" zdotdir)}
''; '';
home.file.".zshrc".text = '' home.file."${relToDotDir ".zshrc"}".text = ''
${export "HISTSIZE" cfg.history.size} ${export "HISTSIZE" cfg.history.size}
${export "HISTFILE" cfg.history.path} ${export "HISTFILE" ("$HOME/" + cfg.history.path)}
setopt HIST_FCNTL_LOCK setopt HIST_FCNTL_LOCK
${if cfg.history.ignoreDups then "setopt" else "unsetopt"} HIST_IGNORE_DUPS ${if cfg.history.ignoreDups then "setopt" else "unsetopt"} HIST_IGNORE_DUPS
@ -214,12 +238,12 @@ in
HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help" HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help"
${concatStrings (map (plugin: '' ${concatStrings (map (plugin: ''
path+="$HOME/.zsh/plugins/${plugin.name}" path+="$HOME/${pluginsDir}/${plugin.name}"
fpath+="$HOME/.zsh/plugins/${plugin.name}" fpath+="$HOME/${pluginsDir}/${plugin.name}"
'') cfg.plugins)} '') cfg.plugins)}
${optionalString cfg.enableCompletion "autoload -U compinit && compinit"} ${optionalString cfg.enableCompletion "autoload -U compinit && compinit"}
${optionalString (cfg.enableAutosuggestions) ${optionalString cfg.enableAutosuggestions
"source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh" "source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
} }
@ -241,7 +265,7 @@ in
''} ''}
${concatStrings (map (plugin: '' ${concatStrings (map (plugin: ''
source "$HOME/.zsh/plugins/${plugin.name}/${plugin.file}" source "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}"
'') cfg.plugins)} '') cfg.plugins)}
${cfg.initExtra} ${cfg.initExtra}
@ -261,7 +285,7 @@ in
programs.zsh.enableCompletion = mkDefault true; programs.zsh.enableCompletion = mkDefault true;
home.file = map (plugin: { home.file = map (plugin: {
target = ".zsh/plugins/${plugin.name}"; target = "${pluginsDir}/${plugin.name}";
source = plugin.src; source = plugin.src;
}) cfg.plugins; }) cfg.plugins;
}) })