nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
7.11k stars 1.83k forks source link

nix auto completion with zsh does not seem to work #2005

Closed rollschild closed 3 years ago

rollschild commented 3 years ago

Issue description

Hi community,

I recently switched to home-manager and moved my zsh and oh-my-zsh configs to .config/nixpkgs/home.nix, like so;

  programs.zsh = {
    enable = true;
    enableAutosuggestions = true;
    enableCompletion = true;
    history.extended = true;
    initExtraBeforeCompInit = "
      source ${pkgs.zsh-powerlevel10k}/share/zsh-powerlevel10k/powerlevel10k.zsh-theme
      source ~/.p10k.zsh
    ";
    initExtra = ''
      export ZSH=${pkgs.oh-my-zsh}/share/oh-my-zsh/
      POWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD=true
      DISABLE_UPDATE_PROMPT=true
      export UPDATE_ZSH_DAYS=1
      ENABLE_CORRECTION="true"
      COMPLETION_WAITING_DOTS="true"
      HIST_STAMPS="mm/dd/yyyy"

      if [ -f ~/.aliases ]; then
        source ~/.aliases
      fi

      export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden --follow --glob "!.git/*"'
      eval "$(direnv hook zsh)"

      [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh

      source $ZSH/oh-my-zsh.sh
    '';
    localVariables = {
      EDITOR = "nvim";
    };
    plugins = [
      {
        name = "powerlevel10k";
        src = pkgs.zsh-powerlevel10k;
      }
      {
        name = "nix-zsh-completions";
        src = pkgs.nix-zsh-completions;
      }
      {
        name = "zsh-completions";
        src = pkgs.zsh-completions;
      }
    ];
    oh-my-zsh = {
      enable = true;
      plugins = [ 
        "git" 
        "python" 
        "man" 
        "ripgrep" 
        "rust" 
        "node" 
        "golang"
      ];
    };
  };

However, after doing this, the nix auto completion does not work anymore, meaning, if I type home-manager swi it won't auto complete to home-manager switch anymore. When I installed system-level, this used to work, like this in /etc/nixos/cofiguration.nix:

# zsh and oh-my-zsh
  programs.zsh.enable = true;
  programs.zsh.autosuggestions.enable = true;
  programs.zsh.interactiveShellInit = ''
    export ZSH=${pkgs.oh-my-zsh}/share/oh-my-zsh/
    DISABLE_UPDATE_PROMPT=true
    export UPDATE_ZSH_DAYS=1
    ENABLE_CORRECTION="true"
    COMPLETION_WAITING_DOTS="true"
    HIST_STAMPS="mm/dd/yyyy"

    if [ -f ~/.aliases ]; then
      source ~/.aliases
    fi

    export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden --follow --glob "!.git/*"'
    eval "$(direnv hook zsh)"

    [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh

    source $ZSH/oh-my-zsh.sh

'';
  programs.zsh.ohMyZsh = {
    enable = true;
    plugins = [ "git" "python" "man" "ripgrep" "rust" "node" "golang" ];
  };
  programs.zsh.ohMyZsh.customPkgs = [
    pkgs.nix-zsh-completions
    # and even more...
  ];

Could somebody please help me on this? Thanks!

Technical details

 - system: `"x86_64-linux"`
 - host os: `Linux 5.10.35, NixOS, 21.05pre289039.d1601a40c48 (Okapi)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.3.10`
 - channels(<user>): `"home-manager"`
 - channels(root): `"nixos-21.05pre289039.d1601a40c48, nixpkgs-21.05pre287374.1c16013bd6e"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`
ambroisie commented 3 years ago

I think that's because you need to set the file attribute for plugins that you add from nixpkgs, as they put their file in $out/share/zsh/site-functions, whereas if you clone the repository, you get the file at the root of the source directory.

ambroisie commented 3 years ago

Here's an example for fast-syntax-highlighting:

{
  name = "fast-syntax-highlighting";
  file = "share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh";
  src = pkgs.zsh-fast-syntax-highlighting;
}
berbiche commented 3 years ago

@rollschild if the proposed solution works for you, I recommend closing the ticket :+1: