Automatic (shell) completions for devShell (zsh or in general)

When I get dropped into a devShell, which is based on the devShell I define in a flake.nix and direnv “activates” it for me, certain tools have working (auto-)completions and certain tools do not.

I was not able to figure out on a surface-level, how I would/should fix this. Since I work a lot with Rust projects I added this for cargo / (oxalica/crane) related tooling.

        devShells.default = craneLib.devShell (
           {
            shellHook = ''
              export FPATH="${toolchain}/share/zsh/site-functions''${FPATH:+:$FPATH}"
            '';
           }

Since this differs between shells, I was wondering whether anyone partially solved this issue for any shell or found a generic approach or whether I missed some cool feature already in nix/nixOS that solves this issue?

Would something be like this “canonical”:

          shellHook = ''
            export FPATH="${lib.makeSearchPath "share/zsh/site-functions" packages}''${FPATH:+:$FPATH}"
          '';

where packages is the packages I give to the devShell?

2 Likes

I’ve tried solving this for myself a few times, but never had enough time or persistence to actually do it. Will try your solution, thx.

1 Like

For both nix develop and nix-shell, XDG_DATA_DIRS should be set up by default. (I guess zsh doesn’t read some completions from there?)

This should just work for zsh and Bash, but for Fish, it requires fish: update fish_complete_path when XDG_DATA_DIRS changes by roberth · Pull Request #1540 · direnv/direnv · GitHub.

1 Like