@ben What about:
auth_username_format = %{user | regex("@localhost$", "") | lower}
(see https://doc.dovecot.org/latest/core/settings/variables.html#list-of-filters)
Edit: the at-localhost should not be a mention but literal, no idea how to quote the at-symbol in Mastodon. :-(
I also suggest filing a bug about missing support for pipes in conditionals!