NixOS / nix-mode

An Emacs major mode for editing Nix expressions.
GNU Lesser General Public License v2.1
293 stars 74 forks source link

Support completing `.#` in pcomplete/nix (ex. nix build .#myconfig`) #174

Open ParetoOptimalDev opened 1 year ago

ParetoOptimalDev commented 1 year ago

And other situations, here's copy paste of my org mode todo:

* TODO try adding a completion at point function for .# 

** that works with basically anything with nix and .#

*** nix build .#something

*** nix run .#something

*** nixos-rebuild switch --flake .#something

Right now eshell just falls back to completiing it into a directory for some reason.

ParetoOptimalDev commented 1 year ago

I tried naively adding .# to nix--pcomplete-flags even though it's not a flag and that didn't work:

 ("build"
         (nix--pcomplete-flags
          (append nix-toplevel-options '("--arg" "--argstr" "--dry-run"
                                         "-f" "--file" "-I" "--include"
                                         "--no-link" "-o" "--out-link" ".#"))))

For others toying around with this or other nix related (p)completion I found this to be a useful resource:

https://www.masteringemacs.org/article/pcomplete-context-sensitive-completion-emac

ParetoOptimalDev commented 1 year ago

I tried using edebug to understand pcomplete/nix and figured out that while nix build hits its code path, nix build . hits it, nix build .# does not.

.# seems to be special in emacs completion somehow and this special behavior will need to be overriden to get completion for it.

ParetoOptimalDev commented 1 year ago

.# is special because it's an eshell glob. I can progress a bit further following the control flow in edebug by removing # from eshell-glob-chars-list but things aren't quite working. I think it's because the command getting run and it's result is:

~ $ NIX_GET_COMPLETIONS=2 nix build .#
attrs

It wouldn't be correct, but I'd expect at this point to get attrs as a completion candidate.

ParetoOptimalDev commented 1 year ago

Oh, actually it's working! I just wasn't in a directory with a flake. So this works (if the code is correct):

(setq eshell-glob-chars-list '(93 91 42 63 126 40 41 124 94))

And then adding .# in:

 ("build"
         (nix--pcomplete-flags
          (append nix-toplevel-options '("--arg" "--argstr" "--dry-run"
                                         "-f" "--file" "-I" "--include"
                                         "--no-link" "-o" "--out-link" ".#"))))

Ideally though we could avoid removing .# as a glob and only treat it as a glob when it's not preceded by nix build. I don't have time to look into right now though.