Do you happen to know how to do that on a linux machine running X? I'm afraid to dive back into the xkb rule hell-hole to figure it out myself.
handles part of it and
xcape -e 'Caps_Lock=Escape'
does the rest. xcape is in the ubuntu repos, for fedora there's a copr (dawid/xcape).