I've tried to do what one of the KeePassXC maintainers says in issue #6907. I've checked Use a custom browser configuration location:, Browser type Firefox and added the line ~/.librewolf/native-messaging-hosts in Config Location. Then I've restarted the browser, but the extension still says Cannot connect to KeePassXC. Check that browser integration is enabled in KeePassXC settings.

Edit: it has created a directory in /home/ajr/~/.librewolf/native-messaging-hosts. So it probably doesn't want the ~/ at the start of the location.