How to get the number of defined layers

Yes, that’s the tradeoff. But this magic variable doesn’t require any editing; it’s always just the one line:

const uint8_t DefinedLayersCount PROGMEM = sizeof(keymaps) / sizeof(*keymaps);

(And I’m not totally sure why (or if) the PROGMEM declaration needs to be there).