Run commands from toolbox / distrobox only if missing

I was bored switching to/from ArchLinux/Fedora with toolbox/distrobox

Exemple: I need an ArchLinux package, I enter distrobox, I work on the command line and then I want to launch an application like org.gnome.geary for exemple: flatpak command not found.

Normal, I’m in the toolbox container, not in Silverblue.

After thinking to the issue, I remembered command-not-found package, how does it work ?

I now have this in my Bash configuration:

~/.bashrc.d/distrobox

command_not_found_handle() {
    echo $@|grep 'pacman' >/dev/null 2>&1
    if (( $? == 0 ))
    then
        distrobox-enter -n arch -e sudo $@
    else
        distrobox-enter -n arch -e $@
    fi
    return $?
}

Example for toolbox:

~/.bashrc.d/toolbox

command_not_found_handle() {
    toolbox run $@
    return $?
}

Only thing missing is completion.