nixos-config/configurations/profile/home/shell/zsh.nix

203 lines
6.3 KiB
Nix
Raw Permalink Normal View History

2024-11-18 00:28:37 +01:00
{
config,
pkgs,
lib,
materusArg,
...
}:
2023-10-08 11:42:08 +02:00
let
2024-02-26 01:23:15 +01:00
2024-11-18 00:28:37 +01:00
relToDotDir =
file:
(lib.optionalString (config.programs.zsh.dotDir != null) (config.programs.zsh.dotDir + "/")) + file;
2024-03-11 16:19:31 +01:00
pluginsDir =
if config.programs.zsh.dotDir != null then
2024-11-18 00:28:37 +01:00
relToDotDir "plugins"
else
"${config.home.homeDirectory}/.zsh/plugins";
2024-02-26 01:23:15 +01:00
2023-10-08 11:42:08 +02:00
p10kcfg = "${zshcfg}/p10kcfg";
zshcfg = "${materusArg.cfg.path}" + "/extraFiles/config/zsh";
2023-10-08 11:42:08 +02:00
cfg = config.materus.profile.zsh;
2024-02-26 01:23:15 +01:00
makeEnv = name: val: ''${name}=''${${name}:-"${val}"}'';
makeIfVar = var: val: ret: ''
if [[ "''$${var}" = "${val}" ]]; then
${ret}
2024-03-30 00:01:34 +01:00
fi
2024-03-31 20:01:34 +02:00
'';
2024-02-26 01:23:15 +01:00
2024-03-11 16:19:31 +01:00
makePlugin = nameArg: fileArg: srcArg: rec {
2024-02-26 01:23:15 +01:00
name = nameArg;
src = srcArg;
path = pluginsDir + "/" + name;
file = fileArg;
fullPath = path + "/" + file;
};
extraPlugins = {
2024-11-18 00:28:37 +01:00
powerlevel10k = makePlugin "powerlevel10k" "powerlevel10k.zsh-theme" (
pkgs.fetchFromGitHub {
owner = "romkatv";
repo = "powerlevel10k";
rev = "v1.20.0";
sha256 = "sha256-ES5vJXHjAKw/VHjWs8Au/3R+/aotSbY7PWnWAMzCR8E=";
}
);
2024-03-31 00:28:17 +01:00
sudo = makePlugin "sudo" "sudo.plugin.zsh" "${pkgs.oh-my-zsh}/share/oh-my-zsh/plugins/sudo";
2024-11-18 00:28:37 +01:00
extract =
makePlugin "extract" "extract.plugin.zsh"
"${pkgs.oh-my-zsh}/share/oh-my-zsh/plugins/extract";
2024-02-26 01:23:15 +01:00
};
2023-10-08 11:42:08 +02:00
in
{
options.materus.profile.zsh.enable = materusArg.pkgs.lib.mkBoolOpt config.materus.profile.enableTerminalExtra "Enable materus zsh config";
2023-10-08 11:42:08 +02:00
options.materus.profile.zsh.prompt = lib.mkOption {
type = lib.types.enum [ "p10k" ];
2024-03-11 16:19:31 +01:00
example = "p10k";
default = "p10k";
2023-10-08 11:42:08 +02:00
};
options.materus.profile.zsh.endConfig = lib.mkOption {
default = "";
description = "Zsh config after all of config";
type = lib.types.lines;
};
2023-10-08 11:42:08 +02:00
config = lib.mkIf cfg.enable {
2024-11-18 00:28:37 +01:00
home.packages = [ pkgs.ripgrep ];
2024-03-11 16:19:31 +01:00
2024-03-31 20:01:34 +02:00
home.file = lib.mkMerge [
2024-11-18 00:28:37 +01:00
(builtins.foldl' (a: b: a // b) { } (
builtins.map (plugin: { ${plugin.path}.source = plugin.src; }) (builtins.attrValues extraPlugins)
))
{ "${relToDotDir ".zshrc"}".text = lib.mkAfter cfg.endConfig; }
2024-03-31 20:01:34 +02:00
];
2023-10-08 11:42:08 +02:00
programs.zsh = {
enable = true;
2024-06-17 13:09:58 +02:00
autosuggestion.enable = true;
syntaxHighlighting.enable = true;
2023-10-08 11:42:08 +02:00
enableVteIntegration = true;
historySubstringSearch.enable = true;
historySubstringSearch.searchUpKey = "$key[Up]";
historySubstringSearch.searchDownKey = "$key[Down]";
2023-10-08 11:42:08 +02:00
envExtra = ''
2024-03-11 16:19:31 +01:00
${makeEnv "__MATERUS_HM_ZSH" "1"}
${makeEnv "__MATERUS_HM_ZSH_PROMPT" cfg.prompt}
2024-03-30 00:01:34 +01:00
${makeEnv "__MATERUS_HM_ZSH_PRIVATE" "0"}
2023-10-08 11:42:08 +02:00
'';
2024-02-26 01:23:15 +01:00
initExtraFirst = ''
2024-11-18 00:28:37 +01:00
bindkey -e
if [ -n "$EAT_SHELL_INTEGRATION_DIR" ]; then
source "$EAT_SHELL_INTEGRATION_DIR/zsh";
fi
2024-11-18 00:28:37 +01:00
2024-02-26 01:23:15 +01:00
${makeIfVar "__MATERUS_HM_ZSH_PROMPT" "p10k" ''
2024-11-18 00:28:37 +01:00
if [[ -r "''${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-''${(%):-%n}.zsh" ]]; then
source "''${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-''${(%):-%n}.zsh"
fi
if [[ -f "${extraPlugins.powerlevel10k.fullPath}" ]]; then
source "${extraPlugins.powerlevel10k.fullPath}"
fi
''}
if zmodload zsh/terminfo && (( "$terminfo[colors]" >= "256" )); then
__MATERUS_HM_ZSH_256COLORS="''${__MATERUS_HM_ZSH_256COLORS:-1}"; else
__MATERUS_HM_ZSH_256COLORS="''${__MATERUS_HM_ZSH_256COLORS:-0}";
fi
if [[ -f "${extraPlugins.sudo.fullPath}" ]]; then
source "${extraPlugins.sudo.fullPath}"
fi
if [[ -f "${extraPlugins.extract.fullPath}" ]]; then
source "${extraPlugins.extract.fullPath}"
path+="${extraPlugins.extract.path}"
fpath+="${extraPlugins.extract.path}"
fi
2024-11-18 00:28:37 +01:00
'';
2023-10-08 11:42:08 +02:00
2024-11-18 00:28:37 +01:00
plugins = [ ];
2024-03-11 16:19:31 +01:00
2023-10-08 11:42:08 +02:00
history = {
extended = true;
save = 100000;
size = 100000;
2024-03-30 00:01:34 +01:00
share = true;
2023-10-08 11:42:08 +02:00
ignoreDups = true;
2024-04-01 21:15:32 +02:00
ignoreAllDups = true;
2023-10-08 11:42:08 +02:00
ignoreSpace = true;
};
completionInit = ''
2024-11-18 00:28:37 +01:00
ZSH_COMPDUMP="''${ZSH_COMPDUMP:-''${XDG_CACHE_HOME:-${config.home.homeDirectory}/.cache}/.zcompdump-''${HOST}-''${ZSH_VERSION}}"
autoload -U compinit && compinit -d $ZSH_COMPDUMP
'';
2024-11-18 00:28:37 +01:00
initExtra =
''
if [[ "$__ETC_ZSHRC_SOURCED" != "1" ]]; then
. ${zshcfg}/zinputrc
fi
source ${zshcfg}/zshcompletion.zsh
history-substring-search-up-prefixed(){
HISTORY_SUBSTRING_SEARCH_PREFIXED=1 history-substring-search-up
}
history-substring-search-down-prefixed(){
HISTORY_SUBSTRING_SEARCH_PREFIXED=1 history-substring-search-down
}
zle -N history-substring-search-up-prefixed
zle -N history-substring-search-down-prefixed
bindkey -r "^["
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;5A" history-substring-search-up-prefixed
bindkey "^[[1;5B" history-substring-search-down-prefixed
2024-11-27 13:46:39 +01:00
bindkey '^[[3~' delete-char
bindkey '^[[3;2~' delete-word
2024-11-18 00:28:37 +01:00
zsh-private() {
__MATERUS_HM_ZSH_PRIVATE=1 ${lib.getExe config.programs.zsh.package}
}
myip() {
${lib.getExe pkgs.wget} -qO- https://wtfismyip.com/text
}
speedtest() {
${lib.getExe pkgs.curl} -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | ${lib.getExe pkgs.python3}
}
${lib.optionalString config.materus.profile.editor.emacs.enable ''alias "e"="emacsclient -nw -c --alternate-editor= "''}
${lib.optionalString config.materus.profile.editor.emacs.enable ''alias "ee"="emacsclient -n -r --alternate-editor= "''}
''
+ makeIfVar "__MATERUS_HM_ZSH_PROMPT" "p10k" ''
if [[ "$__MATERUS_HM_ZSH_256COLORS" = "1" ]] ; then
[[ ! -f ${p10kcfg}/fullcolor.zsh ]] || source ${p10kcfg}/fullcolor.zsh
else
[[ ! -f ${p10kcfg}/compatibility.zsh ]] || source ${p10kcfg}/compatibility.zsh
fi
''
+ makeIfVar "__MATERUS_HM_ZSH_PRIVATE" "1" ''
unset HISTFILE
${lib.optionalString config.programs.zsh.history.share "unsetopt SHARE_HISTORY"}
alias -- 'zsh'="__MATERUS_HM_ZSH_PRIVATE=0 zsh "
''
2024-03-31 20:01:34 +02:00
2024-03-30 00:01:34 +01:00
;
2023-10-08 11:42:08 +02:00
};
2024-03-17 10:25:09 +01:00
programs.starship.enableZshIntegration = lib.mkDefault false;
2023-10-08 11:42:08 +02:00
};
}