nix-community / nix-index

Quickly locate nix packages with specific files [maintainers=@bennofs @figsoda @raitobezarius]
Other
785 stars 50 forks source link

refactor: rewrite command-not-found.sh in rust #227

Open figsoda opened 1 year ago

figsoda commented 1 year ago

closes #222 closes #126 should also fix #210 once this is finished

the implementation is adapted from locate and command-not-found.sh

draft because there are a few things I need to do:

emilazy commented 1 year ago

Wonderful!

The fish mechanism is simple but there are some nuances to consider that break the existing script (translated with babelfish); see https://github.com/fish-shell/fish-shell/issues/9806, https://github.com/fish-shell/fish-shell/issues/7902, https://github.com/fish-shell/fish-shell/pull/9517. (tl;dr it's run inside a substitution so you can't check tty through stdout, which might also interfere with the auto-run feature in terms of what stdout they get and how the exit status is handled. I wonder if anyone actually uses auto-run though? comma seems like a less error-prone way of getting essentially the same result...)

figsoda commented 1 year ago

I just found a typo in the auto run error messages that's existed for a few years now

https://github.com/nix-community/nix-index/blob/73d96c85bc1bfe7c0cbd6269ea42119f65073cf9/command-not-found.sh#L56 https://github.com/nix-community/nix-index/blob/73d96c85bc1bfe7c0cbd6269ea42119f65073cf9/command-not-found.sh#L69

that's probably a sign that people are not really using this feature

emilazy commented 1 year ago

Yeah, I would be happy to see it go. I think the main adaptation you'll need to make for fish is conditioning the pipe check on stdin or stderr, which unfortunately won't be as reliable (e.g. bad-command | ...). Not sure if there's a better check that could be done on the fish side and passed in via environment or something. Maybe it's fine to just check stderr and output there though? Seems okay for a piped command to print to stderr and then fail.

figsoda commented 1 year ago

tbh I think it makes sense for this to run even if it is being piped into another command, maybe we can just remove the check altogether? @bennofs what do you think?

emilazy commented 1 year ago

Yeah I dunno exactly what the rationale is if no output is going to stdout anyway. I think checking stderr would be good because you're printing output there that could be misinterpreted if you're piping it into another command with 2>&1 or similar.

(fwiw I think NIX_AUTO_INSTALL is even worse than NIX_AUTO_RUN – permanent imperative changes to system configuration in response to what could be a mere typo...)

bennofs commented 1 year ago

I agree that NIX_AUTO_RUN / NIX_AUTO_INSTALL are probably fringe usecases that we can drop from upstream (people who want this behavior can use comma / use their own command-not-found handler).

We should include a message if NIX_AUTO_RUN / NIX_AUTO_INSTALL is set that explains the alternatives (comma, custom command not found) so users are not surprised that the feature is suddenly no longer available.

teutat3s commented 1 month ago

Just leaving a note here that I'm building and running nix-index with these changes on my systems and it behaves fine for me. EDIT: Using bash on NixOS 24.05.