I use i3wm, and to map cap lock to escape, I run:
setxkbmap -option caps:swapescape
This works fine, but sometimes while hitting the F1 key, my pinky can accidentally hit the Escape key, which turns on CapsLock.
Gnome has a very nice way to do this, where Shift + Escape = CapsLock. Hitting Escape on its own will do nothing.
Not what you asked for, but you may be interested in getting a keyboard running QMK where you can define these kind of modifier overloading (and much much more) on the keyboard itself, so it’s “portable” and doesn’t require OS/DE settings