After following Andrei's suggestion of adding a sleep command I could finally see the error. It turns out I needed to add the required module(regexp.mod) to the subdirectory "x86_64-efi".
I'm sharing my final config in case anyone needs something similar in the future:
set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000. ...(password hash)
insmod regexp
search.fs_uuid 1b3fdfe0-99cb-4aba-9acd-695b84be63cc root hd0,gpt3
for os_path in ($root)/os/*; do
regexp --set=os_name '^\([^)]*\)/os/(.*)$' $os_path
menuentry "boot $os_name" --unrestricted {
configfile $os_path/boot/grub/grub.cfg
}
done
submenu "boot iso file ->" --users "" {
insmod loopback
for iso in ($root)/iso/*.iso; do
loopback tmp $iso
regexp --set=iso_file '^\([^)]*\)/iso/(.*)$' $iso
if [ -f (tmp)/boot/grub/loopback.cfg ]; then
menuentry "boot $iso_file" $iso /iso/$iso_file {
iso_path=$3
export iso_path
loopback loop $2
root=(loop)
configfile /boot/grub/loopback.cfg
loopback --delete loop
}
elif [ -f (tmp)/boot/grub/grub.cfg ]; then
menuentry "boot $iso_file" $iso {
loopback loop $2
root=(loop)
configfile /boot/grub/grub.cfg
loopback --delete loop
}
fi
loopback --delete tmp
done
}
It is a bit bigger than I expected but working great nevertheless. This configuration targets a btrfs partition with two directories: "os" and "iso". "os" contains one subvolume per installed os, and "iso" a bunch of iso files that can be booted to. The "elif" is there for small isos that just contain a linux image and a initrd.(such as ubuntu minimal isos)
Note that I have added a small extra protection to booting the isos, to avoid the case where someone with temporary physical access to my computer can boot into an iso and mess with my installed distros.(Still possible for a thief to take my hd, but he would have to take it off my laptop since my bios is password protected and locked to boot my hard drive)
Anyway, thank you all for the help!