You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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_pathNIX_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.
Reacted by Spiros Boosalis
Reacted by Bjørn Forsman, Orivej Desh and Ole Jørgen Brønner
Activity
globin commentedon Nov 2, 2017
This at least works with
nix-build -Afor me already.michaelpj commentedon Nov 2, 2017
https://github.com/spwhitt/nix-zsh-completions exists and is very good, but that's for zsh, not bash.
danbst commentedon Nov 2, 2017
@globin how? I have
programs.bash.enableCompletion = true;in configuration, and I don't get thatI try like this:
hedning commentedon Nov 6, 2017
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
nixnow 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_FILEWhere
$defexpris a something like{ nixpkgs = import <nixpgs> {}; nixos = import <nixos> {}; }(taken from the~/.nix-defexprdirectory), orimport /path/to/filewhere the file is taken from the command line.$attr_pathis the attribute path written so far up to the last.prefixed withtop., eg.top.nixpkgs.trjust strips quoting and the array brackets whichbuiltins.attrNamesreturns.hedning commentedon Nov 7, 2017
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
_nixfile from the link above in a bash shell should makenix-build -A/--attrcompletenixpkgspaths (hard coded, so if eg.nixosis your only channelnix-buildwon'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
nixand attribute completion tonix run|build|evalusing 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.
hedning commentedon Nov 10, 2017
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.
danbst commentedon Nov 11, 2017
@hedning awesome! I'll leave #37 open for bash completions out of box with Nix itself, but package
nix-bash-completionssolves my issue.