Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 11 commits into from
Closed

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Jul 22, 2020

⚠️ 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.

  • Optional arguments (declared as PATH?, see e.g. nix add-to-store --help for instance)
  • New Flake CLI
    • Local flakes (nix build .#packages)
    • nix flake subcommands
    • nix flake cmd cli options
    • Support for flake registries (and flake#attr-expressions)
    • Package-completion nix shell nixpkgs#nixStabe (currently only nix shell nixpkgs#packages.x86_64-linux.nixStable gets comleted).
    • Completion for flake URLs
  • options/--experimental-features/--log-format
  • backwards compat
  • prompt_nix_shell_setup for nix develop/shell/run
  • probably more

Refs #32

…ters

This requires some fixes in the help-text parser. From now on, `nix
<tab>` and `nix build <tab>` (though it still uses NIX_PATH rather than
the flake registry) are usable again.
@hedning
Copy link
Collaborator

hedning commented Jul 22, 2020

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
Copy link
Member Author

Ma27 commented Jul 23, 2020

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 👍 .

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
Copy link
Member Author

Ma27 commented Jul 25, 2020

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.

@Ma27 Ma27 mentioned this pull request Jul 26, 2020
@srhb
Copy link

srhb commented Jul 26, 2020

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. 👍

For instance `nix build` allows `<flake>#<name>` where `name` is an
attribute in `self.{packages,legacyPackages}.{arch}`. This means that
commands like

  nix shell nixpkgs#nixStable

get properly completed now.
@Ma27 Ma27 changed the title [WIP] Nix 2.4 (+flakes) support [WIP] Nix 3.0 (+flakes) support Aug 5, 2020
Needed for commands like `nix flake check` where the behavior for `nix
flake <tab>` must be the same as for `nix <tab>`.

This will become relevant in the future as it's planned to namespace
even more-commands[1].

For simplicity I didn't test full recursion, we probably have to fix
this to support e.g. `nix foo bar baz` in the future.

[1] NixOS/nix#3858
@Ma27
Copy link
Member Author

Ma27 commented Aug 5, 2020

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

@srhb
Copy link

srhb commented Aug 5, 2020

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
Copy link
Member Author

Ma27 commented Aug 5, 2020

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

…tions as well

This is a more generic solution which would allow arbitrary nesting of
commands (e.g. `nix foo bar baz`). It is primarily needed now to make
sure that the sub-subcommands get properly interpreted.

This fixes bugs like:

* Missing command-specific options for sub-subcommands like `nix flake
  update --impure`.
* Completion of the arguments for sub-subcommands works now.
* `nix flake --debug <tab>` provides the proper commands.

I tried to document the new behavior with comments to make sure that the
code in here doesn't become even harder to read as it is now.

Pleaes note that I'm not a seasoned ZSH hacker, so I had to use `sed`
and `grep` on a few occasions, improvements are very welcome.
@Ma27
Copy link
Member Author

Ma27 commented Aug 25, 2020

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.

With this patch, a flake reference like

  nix build .#hello

gets completed.
@Ma27
Copy link
Member Author

Ma27 commented Aug 26, 2020

@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 Ma27 marked this pull request as ready for review September 15, 2020 14:12
@Ma27
Copy link
Member Author

Ma27 commented Sep 15, 2020

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
Copy link
Member Author

Ma27 commented Oct 8, 2020

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
Copy link

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
Copy link

oxalica commented Nov 6, 2020

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
Copy link
Member Author

Ma27 commented Feb 20, 2021

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
Copy link
Member Author

Ma27 commented Mar 31, 2021

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 Ma27 closed this Mar 31, 2021
@Ma27 Ma27 deleted the flakes branch March 31, 2021 17:31
@Ma27
Copy link
Member Author

Ma27 commented Apr 5, 2021

For those who are following this thread:

  • I think it makes sense to focus on https://github.com/NixOS/nix/blob/master/misc/zsh/completion.zsh for nix from now on. Using NIX_GET_COMPLETIONS my save a lot of time.
  • I observed the following issues and will try to work on those. Feel free to help out :D
    • Stuff like nix build -f . doesn't seem to complete packages properly
    • Attr-path completion doesn't seem to work (nix build .#.<Tab>)
    • Not sure if I misunderstand the implementation, but NIX_GET_COMPLETIONS=4 nix build -f . should be used to provide stuff for the fourth argument, but throws an assertion error.

edmundmiller added a commit to edmundmiller/dotfiles that referenced this pull request Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants