From ef8a00133928f97207d60c161699c466b9ae8540 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 17 Nov 2024 00:52:09 +0000 Subject: [PATCH] home-manager/zsh: refactor --- home-manager/cli/zsh/default.nix | 356 ++++++++++++++++--------------- 1 file changed, 179 insertions(+), 177 deletions(-) diff --git a/home-manager/cli/zsh/default.nix b/home-manager/cli/zsh/default.nix index 7389c499..5e91adb3 100644 --- a/home-manager/cli/zsh/default.nix +++ b/home-manager/cli/zsh/default.nix @@ -50,207 +50,209 @@ in }; config = lib.mkIf cfg.enable { - home.packages = - with pkgs; - [ - get-ip - realise-symlink - ] - ++ lib.optionals (!stdenv.isDarwin) [ (run-bg-alias "open" (lib.getExe' xdg-utils "xdg-open")) ]; - - programs.zsh = { - enable = true; - autocd = true; - defaultKeymap = "viins"; + home = { + packages = + with pkgs; + [ + get-ip + realise-symlink + ] + ++ lib.optionals (!stdenv.isDarwin) [ + (run-bg-alias "open" (lib.getExe' xdg-utils "xdg-open")) + ]; - # taken care by zim-completion - completionInit = ""; + file = + let + compileZshConfig = + filename: + pkgs.runCommand filename + { + name = "${filename}-zwc"; + nativeBuildInputs = [ pkgs.zsh ]; + } + '' + cp "${config.home.file.${filename}.source}" "${filename}" + zsh -c 'zcompile "${filename}"' + cp "${filename}.zwc" "$out" + ''; + in + { + ".zprofile.zwc".source = compileZshConfig ".zprofile"; + ".zshenv.zwc".source = compileZshConfig ".zshenv"; + ".zshrc.zwc".source = compileZshConfig ".zshrc"; + }; - autosuggestion.enable = true; + sessionPath = [ "$HOME/.local/bin" ]; + }; - history = { - ignoreDups = true; - ignoreSpace = true; - expireDuplicatesFirst = true; - share = true; + programs = { + dircolors.enable = true; + fzf = { + enable = true; + fileWidgetOptions = [ "--preview 'head {}'" ]; + historyWidgetOptions = [ "--sort" ]; + enableZshIntegration = false; }; - - historySubstringSearch = { + zoxide = { enable = true; - searchUpKey = [ "$terminfo[kcuu1]" ]; - searchDownKey = [ "$terminfo[kcud1]" ]; + enableZshIntegration = false; }; + zsh = { + enable = true; + autocd = true; + defaultKeymap = "viins"; - profileExtra = lib.concatStringsSep "\n" ( - lib.filter (x: x != "") [ - (lib.optionalString config.home-manager.crostini.enable # bash - '' - # Force truecolor support in Crostini - export COLORTERM=truecolor - # https://github.com/nix-community/home-manager/issues/3711 - export LC_CTYPE=C.UTF-8 - '' - ) - (lib.optionalString pkgs.stdenv.isDarwin # bash - '' - # Source nix-daemon profile since macOS updates can remove it from /etc/zshrc - # https://github.com/NixOS/nix/issues/3616 - if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then - source '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' - fi - # Set the soft ulimit to something sensible - # https://developer.apple.com/forums/thread/735798 - ulimit -Sn 524288 - '' - ) - # bash - '' - # Source .profile - [[ -e ~/.profile ]] && emulate sh -c '. ~/.profile' - '' - ] - ); - - initExtraBeforeCompInit = # bash - '' - # zimfw config - zstyle ':zim:input' double-dot-expand no - - # try to correct the spelling of commands - setopt correct - # disable C-S/C-Q - setopt noflowcontrol - # disable "no matches found" check - unsetopt nomatch + # taken care by zim-completion + completionInit = ""; - # allow ad-hoc scripts to be add to PATH locally - export PATH="$HOME/.local/bin:$PATH" + autosuggestion.enable = true; - # source contents from ~/.zshrc.d/*.zsh - for file in "$HOME/.zshrc.d/"*.zsh; do - [[ -f "$file" ]] && source "$file" - done - ''; + history = { + ignoreDups = true; + ignoreSpace = true; + expireDuplicatesFirst = true; + share = true; + }; - initExtra = - # manually creating integrations since this is faster than calling - # the program during startup (e.g. `zoxide init zsh`) - # bash - '' - # pure - fpath+=(${pkgs.pure-prompt}/share/zsh/site-functions) - source ${pkgs.pure-prompt}/share/zsh/site-functions/prompt_pure_setup + historySubstringSearch = { + enable = true; + searchUpKey = [ "$terminfo[kcuu1]" ]; + searchDownKey = [ "$terminfo[kcud1]" ]; + }; - # any-nix-shell - source ${ - pkgs.runCommand "any-nix-shell-zsh" { buildInputs = [ pkgs.any-nix-shell ]; } '' - any-nix-shell zsh > $out + profileExtra = lib.concatStringsSep "\n" ( + lib.filter (x: x != "") [ + (lib.optionalString config.home-manager.crostini.enable # bash + '' + # Force truecolor support in Crostini + export COLORTERM=truecolor + # https://github.com/nix-community/home-manager/issues/3711 + export LC_CTYPE=C.UTF-8 + '' + ) + (lib.optionalString pkgs.stdenv.isDarwin # bash + '' + # Source nix-daemon profile since macOS updates can remove it from /etc/zshrc + # https://github.com/NixOS/nix/issues/3616 + if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then + source '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' + fi + # Set the soft ulimit to something sensible + # https://developer.apple.com/forums/thread/735798 + ulimit -Sn 524288 + '' + ) + # bash '' - } + # Source .profile + [[ -e ~/.profile ]] && emulate sh -c '. ~/.profile' + '' + ] + ); - # fzf - source ${config.programs.fzf.package}/share/fzf/completion.zsh - source ${config.programs.fzf.package}/share/fzf/key-bindings.zsh + initExtraBeforeCompInit = # bash + '' + # zimfw config + zstyle ':zim:input' double-dot-expand no - # zoxide - source ${ - pkgs.runCommand "zoxide-init-zsh" { buildInputs = [ config.programs.zoxide.package ]; } '' - zoxide init zsh > $out - '' - } + # try to correct the spelling of commands + setopt correct + # disable C-S/C-Q + setopt noflowcontrol + # disable "no matches found" check + unsetopt nomatch - # zsh-fast-syntax-highlighting - source ${pkgs.zsh-fast-syntax-highlighting}/share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh + # source contents from ~/.zshrc.d/*.zsh + for file in "$HOME/.zshrc.d/"*.zsh; do + [[ -f "$file" ]] && source "$file" + done + ''; - # disable clock - unset RPROMPT + initExtra = + # manually creating integrations since this is faster than calling + # the program during startup (e.g. `zoxide init zsh`) + # bash + '' + # pure + fpath+=(${pkgs.pure-prompt}/share/zsh/site-functions) + source ${pkgs.pure-prompt}/share/zsh/site-functions/prompt_pure_setup - # edit the current command line in $EDITOR - bindkey -M vicmd v edit-command-line + # any-nix-shell + source ${ + pkgs.runCommand "any-nix-shell-zsh" { buildInputs = [ pkgs.any-nix-shell ]; } '' + any-nix-shell zsh > $out + '' + } - # zsh-history-substring-search - # historySubstringSearch.{searchUpKey,searchDownKey} does not work with - # vicmd, this is why we have this here - bindkey -M vicmd 'k' history-substring-search-up - bindkey -M vicmd 'j' history-substring-search-down - ''; + # fzf + source ${config.programs.fzf.package}/share/fzf/completion.zsh + source ${config.programs.fzf.package}/share/fzf/key-bindings.zsh - plugins = - let - zshCompilePlugin = - name: src: - pkgs.runCommand name - { - name = "${name}-zwc"; - nativeBuildInputs = [ pkgs.zsh ]; - } + # zoxide + source ${ + pkgs.runCommand "zoxide-init-zsh" { buildInputs = [ config.programs.zoxide.package ]; } '' + zoxide init zsh > $out '' - mkdir $out - cp -rT ${src} $out - cd $out - find -name '*.zsh' -execdir zsh -c 'zcompile {}' \; - ''; - zshPlugin = name: { - inherit name; - src = zshCompilePlugin name (builtins.getAttr name flake.inputs); - }; - zimPlugin = name: zshPlugin name // { file = "init.zsh"; }; - in - lib.flatten [ - (zimPlugin "zim-input") - (zimPlugin "zim-utility") - (zshPlugin "zsh-autopair") - (zshPlugin "zsh-completions") - (zimPlugin "zim-completion") # needs to be the last one - ]; + } - sessionVariables = { - # Enable scroll support - LESS = "--RAW-CONTROL-CHARS"; - # Reduce time to wait for multi-key sequences - KEYTIMEOUT = 1; - # Set right prompt to show time - RPROMPT = "%F{8}%*"; - }; + # zsh-fast-syntax-highlighting + source ${pkgs.zsh-fast-syntax-highlighting}/share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh - shellAliases = { - # https://unix.stackexchange.com/questions/335648/why-does-the-reset-command-include-a-delay - reset = "${lib.getExe' pkgs.ncurses "tput"} reset"; - }; - }; + # disable clock + unset RPROMPT - home.file = - let - compileZshConfig = - filename: - pkgs.runCommand filename - { - name = "${filename}-zwc"; - nativeBuildInputs = [ pkgs.zsh ]; - } - '' - cp "${config.home.file.${filename}.source}" "${filename}" - zsh -c 'zcompile "${filename}"' - cp "${filename}.zwc" "$out" - ''; - in - { - ".zprofile.zwc".source = compileZshConfig ".zprofile"; - ".zshenv.zwc".source = compileZshConfig ".zshenv"; - ".zshrc.zwc".source = compileZshConfig ".zshrc"; - }; + # edit the current command line in $EDITOR + bindkey -M vicmd v edit-command-line - programs = { - dircolors.enable = true; - fzf = { - enable = true; - fileWidgetOptions = [ "--preview 'head {}'" ]; - historyWidgetOptions = [ "--sort" ]; - enableZshIntegration = false; - }; - zoxide = { - enable = true; - enableZshIntegration = false; + # zsh-history-substring-search + # historySubstringSearch.{searchUpKey,searchDownKey} does not work with + # vicmd, this is why we have this here + bindkey -M vicmd 'k' history-substring-search-up + bindkey -M vicmd 'j' history-substring-search-down + ''; + + plugins = + let + zshCompilePlugin = + name: src: + pkgs.runCommand name + { + name = "${name}-zwc"; + nativeBuildInputs = [ pkgs.zsh ]; + } + '' + mkdir $out + cp -rT ${src} $out + cd $out + find -name '*.zsh' -execdir zsh -c 'zcompile {}' \; + ''; + zshPlugin = name: { + inherit name; + src = zshCompilePlugin name (builtins.getAttr name flake.inputs); + }; + zimPlugin = name: zshPlugin name // { file = "init.zsh"; }; + in + lib.flatten [ + (zimPlugin "zim-input") + (zimPlugin "zim-utility") + (zshPlugin "zsh-autopair") + (zshPlugin "zsh-completions") + (zimPlugin "zim-completion") # needs to be the last one + ]; + + sessionVariables = { + # Enable scroll support + LESS = "--RAW-CONTROL-CHARS"; + # Reduce time to wait for multi-key sequences + KEYTIMEOUT = 1; + # Set right prompt to show time + RPROMPT = "%F{8}%*"; + }; + + shellAliases = { + # https://unix.stackexchange.com/questions/335648/why-does-the-reset-command-include-a-delay + reset = "${lib.getExe' pkgs.ncurses "tput"} reset"; + }; }; }; };