With matrix, which is also federated, this cannot be done. In any way. At all.
You could attempt editing the database directly, but this is a bad, superbad, idea.
This is because the domain of the original server was part of the usernames and chat room names, and all of that would be in the database, for the server, and other servers, to know what goes together with what.
If you change your domain, your server wont know who the users logging in with the new domain are. Other servers wont know who they are, or what chat rooms they should be in.
I imagine it’s much the same with the fediverse. Changing your domain would break everything, where the databases are concerned.
And even if you could edit your database to swap out all mentions of the old domain, you could never do so for all other servers out there with federation database entries for your old domain.