Skip to content

bash completion for nixpkgs attributes #1658

Closed

Description

@danbst

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

Activity

  1. globin commented on Nov 2, 2017

    @globin
    Member

    This at least works with nix-build -A for me already.

  2. michaelpj commented on Nov 2, 2017

    @michaelpj
  3. danbst commented on Nov 2, 2017

    @danbst
    ContributorAuthor

    @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>
    
  4. hedning commented on Nov 6, 2017

    @hedning

    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.

  5. hedning commented on Nov 7, 2017

    @hedning

    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.

  6. hedning commented on Nov 10, 2017

    @hedning

    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.

  7. danbst commented on Nov 11, 2017

    @danbst
    ContributorAuthor

    @hedning awesome! I'll leave #37 open for bash completions out of box with Nix itself, but package nix-bash-completions solves my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions