Skip to content

Commit

Permalink
Refresh zcompdump cache in each home-manager activation (#857)
Browse files Browse the repository at this point in the history
* Extract zcompdump path with zsh only variable

* Remove ZCOMPDUMP_CACHE in each home-manager activation

Without this, no completion changes after packages changes, it makes much confusion

* Fix to handle cache

* As same as in zsh code
  • Loading branch information
kachick authored Oct 20, 2024
1 parent dfbd987 commit dd895e5
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions home-manager/zsh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
...
}:

let
ZCOMPDUMP_CACHE_DIR = "${config.xdg.cacheHome}/zsh";
ZCOMPDUMP_CACHE_PATH = "${ZCOMPDUMP_CACHE_DIR}/zcompdump-${pkgs.zsh.version}";
in
{
services.gpg-agent.enableZshIntegration = true;
programs.starship.enableZshIntegration = true;
Expand All @@ -15,6 +19,14 @@
# Avoid nested zellij in host and remote login as container
programs.zellij.enableZshIntegration = false;

home.activation.refreshZcompdumpCache = config.lib.dag.entryAnywhere ''
if [[ -v oldGenPath ]]; then
run ${lib.getBin pkgs.coreutils}/bin/mkdir -p '${ZCOMPDUMP_CACHE_DIR}'
run ${lib.getExe pkgs.zsh} --interactive -c 'compinit -d "${ZCOMPDUMP_CACHE_PATH}"'
run ${lib.getBin pkgs.coreutils}/bin/touch '${ZCOMPDUMP_CACHE_PATH}' # Ensure to update timestamp
fi
'';

# https://nixos.wiki/wiki/Zsh
# https://zsh.sourceforge.io/Doc/Release/Options.html
# https://github.com/nix-community/home-manager/blob/release-24.05/modules/programs/zsh.nix
Expand All @@ -28,6 +40,8 @@
dotDir = ".config/zsh";

localVariables = {
inherit ZCOMPDUMP_CACHE_DIR ZCOMPDUMP_CACHE_PATH;

# This is a minimum note for home-manager dead case such as https://github.com/kachick/dotfiles/issues/680#issuecomment-2353820508
PROMPT = ''
%~ %? %#
Expand Down Expand Up @@ -127,19 +141,19 @@
# speed - https://gist.github.com/ctechols/ca1035271ad134841284
# both - https://github.com/kachick/dotfiles/pull/155
_compinit_with_interval() {
local -r dump_dir="${config.xdg.cacheHome}/zsh"
local -r dump_path="$dump_dir/zcompdump-$ZSH_VERSION"
# seconds * minutes * hours
local -r threshold="$((60 * 60 * 3))"
if [ -e "$dump_path" ] && [ "$(_elapsed_seconds_for "$dump_path")" -le "$threshold" ]; then
if [ -e "$ZCOMPDUMP_CACHE_PATH" ] && [ "$(_elapsed_seconds_for "$ZCOMPDUMP_CACHE_PATH")" -le "$threshold" ]; then
# https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Use-of-compinit
# -C omit to check new functions
compinit -C -d "$dump_path"
compinit -C -d "$ZCOMPDUMP_CACHE_PATH"
else
${lib.getBin pkgs.coreutils}/bin/mkdir -p "$dump_dir"
compinit -d "$dump_path"
${lib.getBin pkgs.coreutils}/bin/touch "$dump_path" # Ensure to update timestamp
# For refreshing the cache
${lib.getBin pkgs.coreutils}/bin/mkdir -p "$ZCOMPDUMP_CACHE_DIR"
compinit -d "$ZCOMPDUMP_CACHE_PATH"
${lib.getBin pkgs.coreutils}/bin/touch "$ZCOMPDUMP_CACHE_PATH" # Ensure to update timestamp
fi
}
'';
Expand Down

0 comments on commit dd895e5

Please sign in to comment.