nix-community / home-manager

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

bug: hm installed zsh completions aren't added to fpath #2562

Open willruggiano opened 2 years ago

willruggiano commented 2 years ago

Is there an existing issue for this?

Issue description

I had to add this to my zsh configuration;

initExtraBeforeCompInit = ''
    fpath+=(/nix/var/nix/profiles/per-user/<me>/home-manager/home-path/share/zsh/site-functions)
'';

Maintainer CC

No response

System information

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 5.15.4, NixOS, 22.05 (Quokka)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.4pre20210802_47e96bb`
 - nixpkgs: `/nix/store/dk3j2sylwfssk2qvbwz74yf7k6j9zwf4-source`
nixos-discourse commented 2 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/zsh-completions-not-being-loaded/16925/2

berbiche commented 2 years ago

Hi,

I could not reproduce the issue on my system.

I have added environment.pathsToLink = [ "/share/zsh" ]; in my system configuration as documented here: https://nix-community.github.io/home-manager/options.html#opt-programs.zsh.enableCompletion

The logic that is responsible for adding the right paths to fpath is here: https://github.com/nix-community/home-manager/blob/4fa1ba72a39da509399cce43995e4c1e1960ce9c/modules/programs/zsh.nix#L459-L461

wojtodzio commented 2 years ago

I have a very similar issue on my system (macOS with nix-darwin). This logic, among the others, adds the following path to my fpath:

The issue is that even though I do have environment.pathsToLink = [ "/share/zsh" ]; in my system configuration, /nix/var/nix/profiles/default/share/zsh/site-functions/ does not include completions for my packages, and /Users/<me>/.nix-profile/share/zsh/site-functions does not even exist.

/nix/var/nix/profiles/per-user/<me>/home-manager/home-path/share/zsh/site-functions does indeed include those completions, and adding it manually to fpath solves the issue for me.

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"aarch64-darwin"`
 - host os: `Darwin 21.2.0, macOS 12.1`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.5.0`
 - channels(<me>): `"darwin, home-manager"`
 - channels(root): `"nixpkgs-22.05pre338661.3c52ea8c921"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`
berbiche commented 2 years ago

What does the variable NIX_PROFILES contain in your zsh shell?

iRevive commented 2 years ago

I have an identical issue as https://github.com/nix-community/home-manager/issues/2562#issuecomment-1008655333 described.

Adding user's profile (maksim in my case) to fpath made completions available:

initExtraBeforeCompInit = ''
  fpath+="/etc/profiles/per-user/maksim/share/zsh/site-functions"
'';

Nix info:

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-darwin"`
 - host os: `Darwin 20.6.0, macOS 10.16`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.4`
 - channels(root): `"nixpkgs-22.05pre334669.f366af7a1b3"`
 - channels(maksim): `"darwin, nixpkgs, home-manager"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`

Nix profiles:

$ echo $NIX_PROFILES
/nix/var/nix/profiles/default /Users/maksim/.nix-profile

I can assume, /etc/profiles/per-user/maksim should be present in $NIX_PROFILES?

willruggiano commented 2 years ago

Is ~/.nix-profile a home-manager thing? At least for me on the machine in my OP, ~/.nix-profile is a broken symlink pointing to /nix/var/nix/profiles/per-user/<me>/profile. I know home.profileDirectory is one of the hm configuration options; I haven't set it in my configuration.

wojtodzio commented 2 years ago

Same thing on my machine:

$ echo $NIX_PROFILES
/nix/var/nix/profiles/default /Users/<me>/.nix-profile
$ readlink ~/.nix-profile
/nix/var/nix/profiles/per-user/<me>/profile

Both /nix/var/nix/profiles/per-user/<me>/home-manager/home-path/share/zsh/site-functions/ and /etc/profiles/per-user/<me>/share/zsh/site-functions/ contain the missing completions

berbiche commented 2 years ago

@iRevive @willruggiano I have additional questions to figure out whether the issue only happens on non-NixOS systems and/or in standalone Home Manager configurations.

  1. Is your host OS NixOS?
  2. Are you using Home Manager as a NixOS/Darwin module?

edit: In the meantime, the proposed solution is the following:

{
  programs.zsh.initExtraBeforeCompInit = ''
    fpath+=("${config.home.profileDirectory}"/share/zsh/site-functions "${config.home.profileDirectory}"/share/zsh/$ZSH_VERSION/functions "${config.home.profileDirectory}"/share/zsh/vendor-completions)
  '';
}

(adapted from @iRevive's comment https://github.com/nix-community/home-manager/issues/2562#issuecomment-1009219039)

willruggiano commented 2 years ago

I have two hosts, one nixOS the other macOS, and this repros on both. I am using hm as a nixOS/darwin module. I have not tried to repro in a stand-alone hm configuration but I can if that would help.

lovesegfault commented 2 years ago

I can reproduce this on a debian system that I use home-manager in. The issue doesn't pop-up on my NixOS system.

I suspect this might be due to NIX_PROFILES not being fully set by the time that for loop gets eval'd?

stale[bot] commented 2 years ago

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

* If this is resolved, please consider closing it so that the maintainers know not to focus on this. * If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

* If you are also experiencing this issue, please add details of your situation to help with the debugging process. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

wyndon commented 2 years ago

The proposed solution here doesn't work for me. My problem is that system commands like systemctl and journalctl don't have any completions, even when setting environment.pathsToLink.

My fix is to simply enable the zsh module from NixOS. I wish there was something simpler.

kenranunderscore commented 2 years ago

I have the same problem as @wyndon, but for me it's very weird: I have two nearly identical NixOS systems (w.r.t. the zsh configuration and the nixpkgs pin they're identical, and both set environment.pathsToLink), but this issue occurs only on one of the two. I can't check right now, but the fpath values are quite different, most likely due to different values of NIX_PROFILES. I don't know how to debug further, as I don't know how this variable gets set.

@wyndon You mean you disabled the home-manager zsh module and enable the system one instead, or something else?

wyndon commented 2 years ago

@wyndon You mean you disabled the home-manager zsh module and enable the system one instead, or something else?

Both enabled.

kenranunderscore commented 2 years ago

Dang, I've tried this, and I've also tried fully disabling the home-manager module and only using the system programs.zsh.enable, where completion is by default enabled, too. Still no completion in zsh for "global" commands like systemctl, docker etc., while it works fine for bash and fish. So perhaps it's not really a home-manager problem, but rather something system-dependent that I don't quite get. My system configurations are also nearly identical.

stale[bot] commented 2 years ago

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

* If this is resolved, please consider closing it so that the maintainers know not to focus on this. * If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

* If you are also experiencing this issue, please add details of your situation to help with the debugging process. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

kulabun commented 2 years ago

I have the same issue on NixOS 22.05 with a standalone home-manager. The issue is specific to zsh, I see completion works fine in bash.

kulabun commented 2 years ago

So the environment variable NIX_PROFILES is expected for correct fpath setup to work. I had it set on my Ubuntu system and completion worked fine, but it was empty on my NixOS. When the variable is set, everything works fine.

NIX_PROFILES=$(nix profile list --extra-experimental-features nix-command | cut -f4 -d" ")
home-manager switch

I tried then to set environment variable in my nix config as

NIX_PROFILES=${config.home.profileDirectory}

The result variable is resolved to /home/$USER/.nix-profile which is non-existent path in my system. Based on nix documentation I see that /home/$USER/.nix-profile is supposed to be a symlink to /nix/var/nix/profiles/per-user/$USER/profile/. I can create the one, but how NIX_PROFILES and /home/$USER/.nix-profile are supposed to be set in the first place? I use NixOS and home-manager both installed as two different flake configurations.

kulabun commented 2 years ago

I tried to change configuration to nixos module. In this case, autocompletion for zsh doesn't work either. What I see changed is:

  1. /nix/var/nix/profiles/per-user/$USER/profile/ now has only one file manifest.nix, while it has etc, share, bin and other system folders before.
  2. nix profile list --extra-experimental-features nix-command returns empty result now.
  3. ${config.home.profileDirectory} is resolved to /etc/profiles/per-user/$USER, but there is no zsh directory in share. I see there bash-completions though, for bash everything works fine again.
    ❯ ls /etc/profiles/per-user/konstantin/share/ -lah
    Permissions Size User Date Modified    Name
    dr-xr-xr-x     - root 1969-12-31 16:00  accountsservice
    dr-xr-xr-x     - root 1969-12-31 16:00  appdata
    dr-xr-xr-x     - root 1969-12-31 16:00  applications
    dr-xr-xr-x     - root 1969-12-31 16:00  backgrounds
    dr-xr-xr-x     - root 1969-12-31 16:00  bash-completion
    dr-xr-xr-x     - root 1969-12-31 16:00  dbus-1
    dr-xr-xr-x     - root 1969-12-31 16:00  desktop-directories
    dr-xr-xr-x     - root 1969-12-31 16:00  doc
    dr-xr-xr-x     - root 1969-12-31 16:00  emacs
    dr-xr-xr-x     - root 1969-12-31 16:00  hunspell
    dr-xr-xr-x     - root 1969-12-31 16:00  icons
    dr-xr-xr-x     - root 1969-12-31 16:00  info
    dr-xr-xr-x     - root 1969-12-31 16:00  kservices5
    dr-xr-xr-x     - root 1969-12-31 16:00  kservicetypes5
    dr-xr-xr-x     - root 1969-12-31 16:00  kxmlgui5
    dr-xr-xr-x     - root 1969-12-31 16:00  man
    dr-xr-xr-x     - root 1969-12-31 16:00  metainfo
    dr-xr-xr-x     - root 1969-12-31 16:00  mime
    dr-xr-xr-x     - root 1969-12-31 16:00  nano
    dr-xr-xr-x     - root 1969-12-31 16:00  org
    dr-xr-xr-x     - root 1969-12-31 16:00  pixmaps
    dr-xr-xr-x     - root 1969-12-31 16:00  polkit-1
    dr-xr-xr-x     - root 1969-12-31 16:00  sounds
    dr-xr-xr-x     - root 1969-12-31 16:00  systemd
    dr-xr-xr-x     - root 1969-12-31 16:00  terminfo
    dr-xr-xr-x     - root 1969-12-31 16:00  themes
    dr-xr-xr-x     - root 1969-12-31 16:00  thumbnailers
    dr-xr-xr-x     - root 1969-12-31 16:00  vim-plugins
    dr-xr-xr-x     - root 1969-12-31 16:00  vulkan
    dr-xr-xr-x     - root 1969-12-31 16:00  X11

$fpath has plugins only

❯ echo $fpath                 
/nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/tmux /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/terraform /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/taskwarrior /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/ssh-agent /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/vi-mode /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/fzf /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/rust /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/gradle /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/plugins/sudo /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/functions /nix/store/r3zcs127zlwc5760lfiad1758h8z0xxr-oh-my-zsh-2022-04-24/share/oh-my-zsh/completions /home/konstantin/.cache/oh-my-zsh/completions /nix/store/z1lh13c5z4ipcl2w9g1r1xn1vpmv0rm8-zsh-5.8.1/share/zsh/5.8.1/functions /home/konstantin/.zsh/plugins/fzf-tab /home/konstantin/.zsh/plugins/fast-syntax-highlighting /home/konstantin/.zsh/plugins/you-should-use /nix/store/6cyg5ab7nnjxp3sawgx2vsl6iqqklwmx-zsh-fzf-tab-unstable-2022-04-15/share/fzf-tab/lib

In my configuration, I have programs.zsh.enabled = true for home-manager.

I see that zsh.nix expect to have NIX_PROFILES set. But even if I set NIX_PROFILES, the paths it adds just don't exist:

        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

Nix-info

❯ nix-info -m
 - system: `"x86_64-linux"`
 - host os: `Linux 5.15.67, NixOS, 22.05 (Quokka), 22.05.20220915.178fea1`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.8.1`
 - channels(konstantin): `"home-manager-22.05.tar.gz"`
 - channels(root): `"nixos-22.05"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

So when home-manager is used as a NixOS module, the problem is solved by enabling programs.zsh.enable = true for NixOS zsh module in addition to home-manager's one, and setting NIX_PROFILES=${config.home.profileDirectory} environment variable.

stale[bot] commented 1 year ago

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

* If this is resolved, please consider closing it so that the maintainers know not to focus on this. * If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

* If you are also experiencing this issue, please add details of your situation to help with the debugging process. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

srid commented 8 months ago

Of potential interest to nix-darwin users, this is how I got zsh autocompletions working on macOS:

https://github.com/srid/nixos-config/pull/50

image