nix-index icon indicating copy to clipboard operation
nix-index copied to clipboard

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

Open figsoda opened this issue 2 years ago • 8 comments

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:

  • [x] actually replace the command-not-found.sh, including fish support
  • [ ] complete has_flakes to read user config, also consider keeping the previous manifest.json check
  • [ ] better error handling, currently some errors are silently ignored
  • [ ] make the code a bit more compact, the Command::news feel a bit verbose

figsoda avatar Jun 17 '23 01:06 figsoda

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

emilazy avatar Jun 17 '23 01:06 emilazy

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

figsoda avatar Jun 17 '23 02:06 figsoda

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.

emilazy avatar Jun 17 '23 02:06 emilazy

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?

figsoda avatar Jun 17 '23 02:06 figsoda

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

emilazy avatar Jun 17 '23 09:06 emilazy

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.

bennofs avatar Jun 17 '23 10:06 bennofs

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.

teutat3s avatar Jun 12 '24 15:06 teutat3s