This should be rather complicated feature, but this can remove extra step of searching package during installation/building
$ nix build nixpkgs.fire|<TAB><TAB>
nixpkgs.firefox
nixpkgs.firefox-esr
nixpkgs.firefox-bin
$ nix build nixpkgs.fire|
That is, evaluate expression and complete attributes in a way similar to how nix repl works
This at least works with nix-build -A for me already.
https://github.com/spwhitt/nix-zsh-completions exists and is very good, but that's for zsh, not bash.
@globin how? I have programs.bash.enableCompletion = true; in configuration, and I don't get that
I try like this:
$ nix-build '<nixpkgs>' -A fire<TAB><TAB>
As far as I now there's no real completion written for bash (though I could be wrong, as I use ZSH), while the completion support for zsh is fairly complete (supports nix now too :)), I'm guessing @globin uses zsh?
But if anyone is interested in writing completion functions for bash (which I have no experience of, or personal interest in apart from making nix awesome) I'd be happy to help explain how it's done in nix-zsh-completions (a lot of the code should be reusable).
For instance getting the attribute names is done like this:
nix-instantiate --eval - \
<<NIX_FILE | tr '[]"' ' '
let
top_gen = $defexpr;
# --file arguments can be a lambda producing a record too
top = if builtins.typeOf top_gen == "lambda" then top_gen {} else top_gen ;
in
builtins.attrNames $attr_path
NIX_FILE
Where $defexpr is a something like { nixpkgs = import <nixpgs> {}; nixos = import <nixos> {}; } (taken from the ~/.nix-defexpr directory), or import /path/to/file where the file is taken from the command line.
$attr_path is the attribute path written so far up to the last . prefixed with top., eg. top.nixpkgs. tr just strips quoting and the array brackets which builtins.attrNames returns.
I took a crack at making a bash completion proof of concept: nix-bash-completions (as we really should have completion for bash). Getting nix-repl like completion on attribute paths seems to be pretty doable.
Sourcing the _nix file from the link above in a bash shell should make nix-build -A/--attr complete nixpkgs paths (hard coded, so if eg. nixos is your only channel nix-build won't actually find the completed path when run). It also doesn't take files supplied on the command line into account.
It will also add main command completion to nix and attribute completion to nix run|build|eval using your actual $NIX_PATH, so that's a bit more robust.
There's no option logic etc. but could be used a starting point for fully functional bash completion functions.
So I went and made a full implementation. nix-bash-completions should now support most commands including nix, and have almost full feature parity with nix-zsh-completions (apart from some stuff not being possible in bash).
I'll fix it up to a releasable state over the next days and put it in nixpkgs at least, hopefully including NixOS support (through programs.bash.enableCompletion) out of the box.
Would be awesome to have someone test it a bit and leave any possible issues on the github page.
@hedning awesome! I'll leave https://github.com/NixOS/nix/issues/37 open for bash completions out of box with Nix itself, but package nix-bash-completions solves my issue.
Most helpful comment
So I went and made a full implementation. nix-bash-completions should now support most commands including
nix, and have almost full feature parity with nix-zsh-completions (apart from some stuff not being possible in bash).I'll fix it up to a releasable state over the next days and put it in nixpkgs at least, hopefully including NixOS support (through
programs.bash.enableCompletion) out of the box.Would be awesome to have someone test it a bit and leave any possible issues on the github page.