nix-community / nix-zsh-completions

ZSH Completions for Nix
BSD 3-Clause "New" or "Revised" License
228 stars 31 forks source link

[WIP] Nix 3.0 (+flakes) support #34

Closed Ma27 closed 3 years ago

Ma27 commented 4 years ago

:warning: Caution: very early (and probably broken) draft to support Nix 2.4 completion (with and without flakes-support).

Todo:

Note: I recommend to review this commit-by-commit, unfortunately the diff grew a bit too much.

Refs #32

hedning commented 4 years ago

Found a solution that's backwards compatible:

    local arg_text=${option_spec##*-[a-z-]# }
    local opt_text=${option_spec%%$arg_text}
    local -a option_args=(${=arg_text//[^A-Z-[:space:]]/})
    local -a option_group=(${=opt_text//,/})

For normal arguments this works (though we should really preserve any ? and handle those):

    local -a args=(${=help[1]//(*FLAGS|[^A-Z ])/})

Now I'm getting a weird rendering issue, where the prompt blanks while waiting for completions. Not sure if that's reproducable or not though.

Ma27 commented 4 years ago

Found a solution that's backwards compatible:

Nice! I decided to focus on a POC for Nix 2.4 first before taking care of backwards compatibility, but this is something that really has to be fixed :+1: .

Now I'm getting a weird rendering issue

Same here. I'll try to fix this tomorrow. I also have a basic POC for flakes and registries as well, so with the next changes I'll push here, we'll hopefully have something usable to test and review :)

Ma27 commented 4 years ago

Have basic support for nix-flake references now. Please note that this may contain bugs and is not ready yet as I'm not aware (yet) of all semantics of the new nix 2.4 CLI API.

For dogfooding, I'll use my git branch for my local shell now to catch some regressions.

srhb commented 4 years ago

Been testing this out for a few hours now, and it looks very good so far. I'm using flakes and experimental CLI, so far without any issues, in my default shell. :+1:

Ma27 commented 4 years ago

nix flake-subcommands get completed now as well. Not the prettiest change though, we may want to find a better approach lateron :)

srhb commented 4 years ago

Tested as well, looks good, assuming that I should not expect flags to update all the way down right now (eg. nix flake update --<tab>

Ma27 commented 4 years ago

That's right, unfortunately I didn't find an obvious way to fix it right now, but I'll work on this soon :)

Ma27 commented 4 years ago

Just pushed a change that treats nested commands in a better way by getting rid of the gross $subcmd hack. This solves issues like missing options for nix flake foobar and now it's also possible to complete the actual arguments for e.g. nix flake info.

Please note that I'm not a seasoned ZSH hacker so I had to use grep and sed on a few occasions, suggestions for improvements are welcome though.

In order to keep this somehow understandable I added a few comments explaining what's happening now.

I'll dogfood the change now, however it took me a while to get this together, so I wouldn't be too surprised if this causes a few new regressions.

Ma27 commented 4 years ago

@srhb @hedning while this isn't backwards-compatible (yet) and there are still a few things missing, I'd say that this slowly reaches a usable state and can be reviewed :)

Ma27 commented 4 years ago

AFAICS the basic functionality is now implemented. Before discussing things like backwards-compat etc. I'd love to get some feedback regarding functionality and implementation :)

Ma27 commented 4 years ago

FYI it may be better to use NIX_GET_COMPLETIONS (see #32 for that). If that's actually the case we can close this. Too bad I didn't know about it before ^^

doronbehar commented 3 years ago

Today I noticed this is missing:

diff --git i/_nixos-rebuild w/_nixos-rebuild
index 3586f24..d30fa71 100644
--- i/_nixos-rebuild
+++ w/_nixos-rebuild
@@ -20,6 +20,7 @@ _arguments \
   '--upgrade[Fetch the latest version of NixOS from the NixOS channel]'\
   '--install-bootloader[(Re)install bootloader on the configured device]'\
   "--no-build-nix[Don't build Nix package manager]"\
+  '--update-input[Update the given flake input (usually "nixpkgs"):flake input:'
   '--fast[Equivalent to --no-build-nix --show-trace]'\
   '--rollback[Roll back to the previous configuration]'\
   '(--profile-name -p)'{--profile-name,-p}'[Profile to use to track current and previous system configurations]:Profile:_nix_profiles'\

Along with a few other options:

      --recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
        lockFlags+=("$i")
        ;;
      --update-input)
        j="$1"; shift 1
        lockFlags+=("$i" "$j")
        ;;
      --override-input)

I couldn't however find where to grab them from the local variables, although I do notice them in nix build --<tab>.

oxalica commented 3 years ago

It sometimes eats the whole line when pressing tab. The second line is what I typed, and the third line is what happened after pressing tab. Note that it also eat my leading .

image

Press another tab, then the command is back but the leading notation is still eaten.

image

I'm using oh-my-zsh with theme avit.

Ma27 commented 3 years ago

Yup, another issue is that nix flake update --update-input and similar commands won't receive proper completion.

I'll leave this as "good enough"-solution for now though until it's clear whether that's a suitable approach or the internal completion output of Nix is better (and Nix probably implements such a completion on its own).

Ma27 commented 3 years ago

Closing for now. This isn't compatible with current nixUnstable and I don't see a trivial reason to fix this. As suggested previously, I'll try NIX_GET_COMPLETIONS.

Ma27 commented 3 years ago

For those who are following this thread: