Skip to content

ZSH completion of nix shell <flake-pkg-ref> --command <cmd> should suggest _files #14625

@doronbehar

Description

@doronbehar

On ZSH, I noticed that querying tab completions for the following kind of commands:

nix shell <flake-pkg-ref> --command <cmd>

Gives no suggestions. IMO since we don't dynamically load the completion for <cmd>, the least we can do is to complete _files in this case - as it will most likely work for most commands. At the moment, nothing is being completed in this case, and if you want to give a file as an argument, it is pretty annoying to not have even a file completion.

I managed to trace how Nix' ZSH completions are generated, and found the completion.zsh script, and the NIX_GET_COMPLETIONS support done by all Nix commands. Indeed (example):

NIX_GET_COMPLETIONS=4 nix shell nixpkgs-current#texliveFull --command xelatex

Prints:

normal

And that's it - no file names are printed there.

I managed to workaround the issue with the following diff to the file:

22c22,27
<   compadd -J nix "${args[@]}" -a suggestions
---
>   if [[ "$tpe" == normal ]] && (( ${#suggestions} == 0 )); then
>     # In case there are no suggestions by Nix,
>     _files
>   else
>     compadd -J nix "${args[@]}" -a suggestions
>   fi

But I wonder. Is it the correct way to go? Maybe the CPP code that handles NIX_GET_COMPLETIONS should be changed? If so, should it be changed for many more cases? My solution is pretty generic, but perhaps too absolute?

I think that in this specific case of completing external commands, it should be possible to detect a --command is being used and to edit the ZSH completion script to fully complete xelatex - as _xelatex is a completion function already distributed by ZSH:

$ whence -v _tex
_tex is a shell function from /run/current-system/sw/share/zsh/5.9/functions/_tex
$ head -1 $(whence -v _tex | awk '{print $NF}')
#compdef tex latex slitex pdftex pdflatex jadetex pdfjadetex xetex=tex xelatex=latex latexmk

CCing people who were involved in the ZSH completion:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions