Shell.nix load completions

For some reasons nixpkgs/default.nix at 53f793d51fb233456941481ea22b761bb4960312 · NixOS/nixpkgs · GitHub is doing nothing in my nix shell, i also was trying to put into the shellHook:

  • . <(skaffold completions $(basename $SHELL))
  • . <(skaffold completions zsh)

I also tried to put them in direnv’s .envrc.

All to no avail. When I execute those exact commands in the final shell, it works.

  • Why is this? What am I not seeing?
  • How can I make it work, so that nix-shell does get with completions?
1 Like

for bash, doing something like:

source <(kubectl completion bash)


I’m not sure about zsh.

It would be nice if nix-shell could auto-detect shell completion from inputs though…

for mkShell you can pass a Shellhook

mkShell {
  shellHook = ''
    . <(skaffold completions $(basename $SHELL))

The shellHook should execute each time you do nix-shell

1 Like

Thanks for giving this a try, Jon! :+1:

  • Running manually works
  • Running from shellHook doesn’t
  • which might be related to why the built-in linked postInstall hook misses out as well.

EDIT: I think the shell hook is not actually executed in the target shell. That might explain it.

installShellCompletion hooks will install it for nixos’s environment.systemPackages and similar paradigms to check for paths to link to, they do not automatically get executed with mkShell


How did @burke solve this for runix?

Not sure.

runix isn’t a package, nor a command found in nixos:

[17:39:14] jon@jon-desktop /home/jon/projects/nixpkgs (master)
$ command-not-found runix
runix: command not found
[17:39:19] jon@jon-desktop /home/jon/projects/nixpkgs (master)
$ nix-build -A runix
error: attribute 'runix' in selection path 'runix' not found

Haha, I like your reflexes!

It has been accidentally published just enough for google to catch it up - funny! - private repository at the time of writing.


But is presented here:

There are also a couple of posts in this forum.

looking at the video, there’s probably something which will bring in a shellhook which will add the shell completion to the current shell session.

But I can’t be certain because there’s no code :slight_smile:

It looks like there is a solution under way:

you should be able to pass the dependencies through inputsFrom and have them execute their shellhook. However, this has other implications, as I think it will propagate all of their dependencies into your shell as well.

That might work as well, I think, though I’m gonna rely on zimbatm’s witchery. :wink:

1 Like

@blaggacao if you are using direnv shellhooks that source scripts won’t be present in ZSH.
That’s because direnv just exports the environment and saves it to a file & reloads it when you enter.

It doesn’t end up re-executing the shellHook commands.

1 Like

Oh thank you that also explains other observations I had. Thx!

instead of direnv @burke also uses but honestly it’s for their more exotic usecase since they pre-generate the lisp code.

I find direnv much simpler for smaller projects.
Also the nix-community improvements are amazing

There’s now two PRs related to this:
Adding /share folders to XDG_DATA_DIRS:
Adding separate sourceInputDerivationHook for mkShell: