Overriding command_not_found_handler

Hi there :wave:

Since I started using zoxide, I became lazier. Now I want to redirect every “not found” command to it for faster action.

Currently if I type an unknown command (e.g. “sl”), it shows me:

The program 'sl' is not in your PATH. It is provided by several packages.
You can make it available in an ephemeral shell by typing one of the following:
  nix-shell -p python38Packages.softlayer
  nix-shell -p python39Packages.softlayer
  nix-shell -p sl

Which comes from here apparently. I’d like to override this part to first send the query to zoxide, then continue to the default handler if there is no matching directory found.
Yet I am still a potato on overriding values in nix, could you share an example override I can define on my config?

Thanks :eyes:

1 Like

If you want to overwrite command_not_found_handler it’s quite simple. I have this in my NixOS config file:

programs.command-not-found.enable = false;
programs.zsh.interactiveShellInit = ''
        # This function is called whenever a command is not found.
      command_not_found_handler() {
        local p=${pkgs.comma}/bin/comma
        if [ -x $p ]; then
          # Run the helper program.
          $p "$@"
          # Indicate than there was an error so ZSH falls back to its default handler
          echo "$1: command not found" >&2
          return 127

You could replace ${pkgs.comma}/bin/comma with ${pkgs.zoxide}/bin/zoxide} or whatever it is.

However, this will fall back to the default command not found handler, not the nix command-not-found command not found handler.

I can’t think of a way to fall back to that without just recreating all the logic in the Nix file. The reason for this is that when two modules have values for programs.zsh.interactiveShellInit, they will be concatenated, but the order they will appear in is totally random, so which one will override which is unspecified.

1 Like

Thanks for the hint. After digging a bit I realized I can override a function on ZSH, so tried to do it on my config. Sadly it was way more complicated than I thought.
Cloning the module seems easier. I gave up on the way though.