2017-08-15 13:54:33 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.zsh;
|
|
|
|
|
2017-09-12 20:01:02 +02:00
|
|
|
relToDotDir = file: (optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file;
|
|
|
|
|
|
|
|
pluginsDir = if cfg.dotDir != null then
|
|
|
|
relToDotDir "plugins" else ".zsh/plugins";
|
|
|
|
|
2018-12-13 15:23:27 +01:00
|
|
|
envVarsStr = config.lib.zsh.exportAll cfg.sessionVariables;
|
2018-12-13 15:25:09 +01:00
|
|
|
localVarsStr = config.lib.zsh.defineAll cfg.localVariables;
|
2017-09-18 00:26:42 +02:00
|
|
|
|
|
|
|
aliasesStr = concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (k: v: "alias ${k}='${v}'") cfg.shellAliases
|
|
|
|
);
|
|
|
|
|
|
|
|
zdotdir = "$HOME/" + cfg.dotDir;
|
2017-09-12 20:01:02 +02:00
|
|
|
|
2018-12-16 15:23:08 +01:00
|
|
|
bindkeyCommands = {
|
|
|
|
emacs = "bindkey -e";
|
|
|
|
viins = "bindkey -v";
|
|
|
|
vicmd = "bindkey -a";
|
|
|
|
};
|
|
|
|
|
2018-01-12 17:23:58 +01:00
|
|
|
historyModule = types.submodule ({ config, ... }: {
|
2017-08-23 12:51:54 +02:00
|
|
|
options = {
|
|
|
|
size = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 10000;
|
|
|
|
description = "Number of history lines to keep.";
|
|
|
|
};
|
|
|
|
|
2018-01-12 17:23:58 +01:00
|
|
|
save = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
defaultText = 10000;
|
|
|
|
default = config.size;
|
|
|
|
description = "Number of history lines to save.";
|
|
|
|
};
|
|
|
|
|
2017-08-23 12:51:54 +02:00
|
|
|
path = mkOption {
|
|
|
|
type = types.str;
|
2017-09-12 20:01:02 +02:00
|
|
|
default = relToDotDir ".zsh_history";
|
2017-10-12 13:20:23 +02:00
|
|
|
defaultText = ".zsh_history";
|
2017-08-23 12:51:54 +02:00
|
|
|
description = "History file location";
|
|
|
|
};
|
|
|
|
|
|
|
|
ignoreDups = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Do not enter command lines into the history list
|
|
|
|
if they are duplicates of the previous event.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-06-06 22:32:46 +02:00
|
|
|
expireDuplicatesFirst = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2018-06-09 10:29:02 +02:00
|
|
|
description = "Expire duplicates first.";
|
2018-06-06 22:32:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extended = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2018-06-09 10:29:02 +02:00
|
|
|
description = "Save timestamp into the history file.";
|
2018-06-06 22:32:46 +02:00
|
|
|
};
|
|
|
|
|
2017-08-23 12:51:54 +02:00
|
|
|
share = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Share command history between zsh sessions.";
|
|
|
|
};
|
|
|
|
};
|
2018-01-12 17:23:58 +01:00
|
|
|
});
|
2017-08-23 12:51:54 +02:00
|
|
|
|
2017-09-04 16:39:14 +02:00
|
|
|
pluginModule = types.submodule ({ config, ... }: {
|
|
|
|
options = {
|
|
|
|
src = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = ''
|
|
|
|
Path to the plugin folder.
|
|
|
|
|
|
|
|
Will be added to <envar>fpath</envar> and <envar>PATH</envar>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The name of the plugin.
|
|
|
|
|
|
|
|
Don't forget to add <option>file</option>
|
|
|
|
if the script name does not follow convention.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
file = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The plugin script to source.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config.file = mkDefault "${config.name}.plugin.zsh";
|
|
|
|
});
|
|
|
|
|
2017-09-07 23:57:13 +02:00
|
|
|
ohMyZshModule = types.submodule {
|
|
|
|
options = {
|
|
|
|
enable = mkEnableOption "oh-my-zsh";
|
|
|
|
|
|
|
|
plugins = mkOption {
|
|
|
|
default = [];
|
|
|
|
example = [ "git" "sudo" ];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
description = ''
|
|
|
|
List of oh-my-zsh plugins
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
custom = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.str;
|
|
|
|
example = "$HOME/my_customizations";
|
|
|
|
description = ''
|
2017-09-30 12:10:52 +02:00
|
|
|
Path to a custom oh-my-zsh package to override config of
|
|
|
|
oh-my-zsh. See <link xlink:href="https://github.com/robbyrussell/oh-my-zsh/wiki/Customization"/>
|
|
|
|
for more information.
|
2017-09-07 23:57:13 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
theme = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "robbyrussell";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Name of the theme to be used by oh-my-zsh.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-08-15 13:54:33 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
programs.zsh = {
|
|
|
|
enable = mkEnableOption "Z shell (Zsh)";
|
|
|
|
|
2017-09-12 20:01:02 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-08-15 13:54:33 +02:00
|
|
|
shellAliases = mkOption {
|
|
|
|
default = {};
|
|
|
|
example = { ll = "ls -l"; ".." = "cd .."; };
|
|
|
|
description = ''
|
|
|
|
An attribute set that maps aliases (the top level attribute names in
|
|
|
|
this option) to command strings or directly to build outputs.
|
|
|
|
'';
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
enableCompletion = mkOption {
|
|
|
|
default = true;
|
2018-01-29 11:30:26 +01:00
|
|
|
description = ''
|
|
|
|
Enable zsh completion. Don't forget to add
|
|
|
|
<programlisting>
|
|
|
|
environment.pathsToLink = [ "/share/zsh" ];
|
|
|
|
</programlisting>
|
|
|
|
to your system configuration to get completion for system packages (e.g. systemd).
|
|
|
|
'';
|
2017-08-15 13:54:33 +02:00
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
enableAutosuggestions = mkOption {
|
|
|
|
default = false;
|
2017-08-23 12:51:54 +02:00
|
|
|
description = "Enable zsh autosuggestions";
|
|
|
|
};
|
|
|
|
|
|
|
|
history = mkOption {
|
|
|
|
type = historyModule;
|
|
|
|
default = {};
|
|
|
|
description = "Options related to commands history configuration.";
|
2017-08-15 13:54:33 +02:00
|
|
|
};
|
|
|
|
|
2018-12-16 15:23:08 +01:00
|
|
|
defaultKeymap = mkOption {
|
2018-12-23 11:53:01 +01:00
|
|
|
type = types.nullOr (types.enum (attrNames bindkeyCommands));
|
2018-12-16 15:23:08 +01:00
|
|
|
default = null;
|
|
|
|
example = "emacs";
|
2018-12-23 11:53:01 +01:00
|
|
|
description = "The default base keymap to use.";
|
2018-12-16 15:23:08 +01:00
|
|
|
};
|
|
|
|
|
2017-10-12 13:20:23 +02:00
|
|
|
sessionVariables = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = types.attrs;
|
2018-01-25 14:13:44 +01:00
|
|
|
example = { MAILCHECK = 30; };
|
|
|
|
description = "Environment variables that will be set for zsh session.";
|
2017-10-12 13:20:23 +02:00
|
|
|
};
|
|
|
|
|
2017-08-15 13:54:33 +02:00
|
|
|
initExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2017-09-04 16:39:14 +02:00
|
|
|
description = "Extra commands that should be added to <filename>.zshrc</filename>.";
|
|
|
|
};
|
|
|
|
|
2017-10-31 18:12:46 +01:00
|
|
|
profileExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = "Extra commands that should be added to <filename>.zprofile</filename>.";
|
|
|
|
};
|
|
|
|
|
|
|
|
loginExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = "Extra commands that should be added to <filename>.zlogin</filename>.";
|
|
|
|
};
|
|
|
|
|
|
|
|
logoutExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = "Extra commands that should be added to <filename>.zlogout</filename>.";
|
|
|
|
};
|
|
|
|
|
2017-09-04 16:39:14 +02:00
|
|
|
plugins = mkOption {
|
|
|
|
type = types.listOf pluginModule;
|
|
|
|
default = [];
|
|
|
|
example = literalExample ''
|
|
|
|
[
|
|
|
|
{
|
|
|
|
# will source zsh-autosuggestions.plugin.zsh
|
|
|
|
name = "zsh-autosuggestions";
|
|
|
|
src = pkgs.fetchFromGitHub {
|
|
|
|
owner = "zsh-users";
|
|
|
|
repo = "zsh-autosuggestions";
|
|
|
|
rev = "v0.4.0";
|
|
|
|
sha256 = "0z6i9wjjklb4lvr7zjhbphibsyx51psv50gm07mbb0kj9058j6kc";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "enhancd";
|
|
|
|
file = "init.sh";
|
|
|
|
src = pkgs.fetchFromGitHub {
|
|
|
|
owner = "b4b4r07";
|
|
|
|
repo = "enhancd";
|
|
|
|
rev = "v2.2.1";
|
|
|
|
sha256 = "0iqa9j09fwm6nj5rpip87x3hnvbbz9w9ajgm6wkrd5fls8fn8i5g";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
description = "Plugins to source in <filename>.zshrc</filename>.";
|
2017-08-15 13:54:33 +02:00
|
|
|
};
|
2017-09-07 23:57:13 +02:00
|
|
|
|
|
|
|
oh-my-zsh = mkOption {
|
|
|
|
type = ohMyZshModule;
|
|
|
|
default = {};
|
|
|
|
description = "Options to configure oh-my-zsh.";
|
|
|
|
};
|
2018-12-13 15:25:09 +01:00
|
|
|
|
|
|
|
localVariables = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
example = { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=["dir" "vcs"]; };
|
|
|
|
description = ''
|
|
|
|
Extra local variables defined at the top of <filename>.zshrc</filename>.
|
|
|
|
'';
|
|
|
|
};
|
2017-08-15 13:54:33 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-09-18 00:26:42 +02:00
|
|
|
config = mkIf cfg.enable (mkMerge [
|
2018-01-25 14:13:44 +01:00
|
|
|
(mkIf (cfg.profileExtra != "") {
|
|
|
|
home.file."${relToDotDir ".zprofile"}".text = cfg.profileExtra;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.loginExtra != "") {
|
|
|
|
home.file."${relToDotDir ".zlogin"}".text = cfg.loginExtra;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.logoutExtra != "") {
|
|
|
|
home.file."${relToDotDir ".zlogout"}".text = cfg.logoutExtra;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.oh-my-zsh.enable {
|
|
|
|
home.file."${relToDotDir ".zshenv"}".text = ''
|
|
|
|
ZSH="${pkgs.oh-my-zsh}/share/oh-my-zsh";
|
|
|
|
ZSH_CACHE_DIR="''${XDG_CACHE_HOME:-''$HOME/.cache}/oh-my-zsh";
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.dotDir != null) {
|
|
|
|
home.file."${relToDotDir ".zshenv"}".text = ''
|
|
|
|
ZDOTDIR=${zdotdir}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# When dotDir is set, only use ~/.zshenv to source ZDOTDIR/.zshenv,
|
|
|
|
# This is so that if ZDOTDIR happens to be
|
|
|
|
# already set correctly (by e.g. spawning a zsh inside a zsh), all env
|
|
|
|
# vars still get exported
|
|
|
|
home.file.".zshenv".text = ''
|
|
|
|
source ${zdotdir}/.zshenv
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
|
2017-09-18 00:26:42 +02:00
|
|
|
{
|
2017-09-07 23:57:13 +02:00
|
|
|
home.packages = with pkgs; [ zsh ]
|
|
|
|
++ optional cfg.enableCompletion nix-zsh-completions
|
|
|
|
++ optional cfg.oh-my-zsh.enable oh-my-zsh;
|
2017-08-15 13:54:33 +02:00
|
|
|
|
2017-09-12 20:01:02 +02:00
|
|
|
home.file."${relToDotDir ".zshrc"}".text = ''
|
2018-01-25 14:13:44 +01:00
|
|
|
typeset -U path cdpath fpath manpath
|
2018-01-29 11:30:26 +01:00
|
|
|
|
|
|
|
for profile in ''${(z)NIX_PROFILES}; do
|
|
|
|
fpath+=($profile/share/zsh/site-functions $profile/share/zsh/$ZSH_VERSION/functions $profile/share/zsh/vendor-completions)
|
|
|
|
done
|
2017-09-05 14:21:57 +02:00
|
|
|
|
2017-08-23 13:06:19 +02:00
|
|
|
HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help"
|
|
|
|
|
2018-12-23 11:53:01 +01:00
|
|
|
${optionalString (cfg.defaultKeymap != null) ''
|
|
|
|
# Use ${cfg.defaultKeymap} keymap as the default.
|
|
|
|
${getAttr cfg.defaultKeymap bindkeyCommands}
|
|
|
|
''}
|
2018-12-16 15:23:08 +01:00
|
|
|
|
2018-12-13 15:25:09 +01:00
|
|
|
${localVarsStr}
|
|
|
|
|
2017-09-04 16:39:14 +02:00
|
|
|
${concatStrings (map (plugin: ''
|
2017-09-12 20:01:02 +02:00
|
|
|
path+="$HOME/${pluginsDir}/${plugin.name}"
|
|
|
|
fpath+="$HOME/${pluginsDir}/${plugin.name}"
|
2017-09-04 16:39:14 +02:00
|
|
|
'') cfg.plugins)}
|
|
|
|
|
|
|
|
${optionalString cfg.enableCompletion "autoload -U compinit && compinit"}
|
2017-09-12 20:01:02 +02:00
|
|
|
${optionalString cfg.enableAutosuggestions
|
2017-08-15 13:54:33 +02:00
|
|
|
"source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
|
|
|
|
}
|
|
|
|
|
2018-02-10 19:21:25 +01:00
|
|
|
# Environment variables
|
2018-07-29 18:15:50 +02:00
|
|
|
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
|
2018-02-10 19:21:25 +01:00
|
|
|
${envVarsStr}
|
|
|
|
|
2017-09-07 23:57:13 +02:00
|
|
|
${optionalString cfg.oh-my-zsh.enable ''
|
|
|
|
# oh-my-zsh configuration generated by NixOS
|
|
|
|
${optionalString (cfg.oh-my-zsh.plugins != [])
|
|
|
|
"plugins=(${concatStringsSep " " cfg.oh-my-zsh.plugins})"
|
|
|
|
}
|
|
|
|
${optionalString (cfg.oh-my-zsh.custom != "")
|
|
|
|
"ZSH_CUSTOM=\"${cfg.oh-my-zsh.custom}\""
|
|
|
|
}
|
|
|
|
${optionalString (cfg.oh-my-zsh.theme != "")
|
|
|
|
"ZSH_THEME=\"${cfg.oh-my-zsh.theme}\""
|
|
|
|
}
|
|
|
|
source $ZSH/oh-my-zsh.sh
|
|
|
|
''}
|
|
|
|
|
2017-09-04 16:39:14 +02:00
|
|
|
${concatStrings (map (plugin: ''
|
2018-04-16 22:12:53 +02:00
|
|
|
if [ -f "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" ]; then
|
|
|
|
source "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}"
|
|
|
|
fi
|
2017-09-04 16:39:14 +02:00
|
|
|
'') cfg.plugins)}
|
|
|
|
|
2018-01-12 17:23:58 +01:00
|
|
|
# History options should be set in .zshrc and after oh-my-zsh sourcing.
|
|
|
|
# See https://github.com/rycee/home-manager/issues/177.
|
2018-01-12 11:24:08 +01:00
|
|
|
HISTSIZE="${toString cfg.history.size}"
|
|
|
|
HISTFILE="$HOME/${cfg.history.path}"
|
2018-01-12 17:23:58 +01:00
|
|
|
SAVEHIST="${toString cfg.history.save}"
|
|
|
|
|
|
|
|
setopt HIST_FCNTL_LOCK
|
|
|
|
${if cfg.history.ignoreDups then "setopt" else "unsetopt"} HIST_IGNORE_DUPS
|
2018-06-06 22:32:46 +02:00
|
|
|
${if cfg.history.expireDuplicatesFirst then "setopt" else "unsetopt"} HIST_EXPIRE_DUPS_FIRST
|
2018-01-12 17:23:58 +01:00
|
|
|
${if cfg.history.share then "setopt" else "unsetopt"} SHARE_HISTORY
|
2018-06-06 22:32:46 +02:00
|
|
|
${if cfg.history.extended then "setopt" else "unsetopt"} EXTENDED_HISTORY
|
2018-01-12 11:24:08 +01:00
|
|
|
|
2017-08-15 13:54:33 +02:00
|
|
|
${cfg.initExtra}
|
2017-09-07 22:21:00 +02:00
|
|
|
|
2018-01-25 14:13:44 +01:00
|
|
|
# Aliases
|
2017-09-07 22:21:00 +02:00
|
|
|
${aliasesStr}
|
2017-08-15 13:54:33 +02:00
|
|
|
'';
|
2017-09-18 00:26:42 +02:00
|
|
|
}
|
2017-10-13 16:34:02 +02:00
|
|
|
|
2017-09-07 23:57:13 +02:00
|
|
|
(mkIf cfg.oh-my-zsh.enable {
|
|
|
|
# Oh-My-Zsh calls compinit during initialization,
|
|
|
|
# calling it twice causes sight start up slowdown
|
|
|
|
# as all $fpath entries will be traversed again.
|
|
|
|
programs.zsh.enableCompletion = mkForce false;
|
|
|
|
})
|
2017-10-13 16:34:02 +02:00
|
|
|
|
2017-09-04 16:39:14 +02:00
|
|
|
(mkIf (cfg.plugins != []) {
|
|
|
|
# Many plugins require compinit to be called
|
|
|
|
# but allow the user to opt out.
|
|
|
|
programs.zsh.enableCompletion = mkDefault true;
|
|
|
|
|
|
|
|
home.file = map (plugin: {
|
2017-09-12 20:01:02 +02:00
|
|
|
target = "${pluginsDir}/${plugin.name}";
|
2017-09-04 16:39:14 +02:00
|
|
|
source = plugin.src;
|
|
|
|
}) cfg.plugins;
|
|
|
|
})
|
2017-09-18 00:26:42 +02:00
|
|
|
]);
|
2017-08-15 13:54:33 +02:00
|
|
|
}
|