Use BOT_STATE_DIR for matrix state storage

This commit is contained in:
Abel Luck 2026-03-06 11:40:57 +01:00
parent b4fd06ef51
commit 145a3e70c3
4 changed files with 12 additions and 9 deletions

View file

@ -14,7 +14,6 @@
"user_id": "@my-bot-name:matrix.org",
"password": "hunter2",
"device_name": "bot.mydomain.com",
"store_path": "dev.data/",
"verify_ssl": true
}
}

View file

@ -78,6 +78,10 @@ in
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
script = ''
export BOT_STATE_DIR="''${STATE_DIRECTORY}"
exec ${cfg.package}/bin/matrix-ops-bot
'';
environment = {
BOT_CONFIG_FILE = "%d/config.json";
BOT_LISTEN_HOST = cfg.listenAddress;
@ -94,7 +98,6 @@ in
// cfg.extraEnvironment;
serviceConfig = {
Type = "simple";
ExecStart = "${cfg.package}/bin/matrix-ops-bot";
DynamicUser = true;
StateDirectory = cfg.stateDirectory;
WorkingDirectory = "/var/lib/${cfg.stateDirectory}";

View file

@ -16,7 +16,6 @@ self: {
user_id = "@ops-bot:example.invalid";
password = "not-a-real-password";
device_name = "nixos-test";
store_path = "/var/lib/matrix-ops-bot/matrix";
verify_ssl = false;
};
};

View file

@ -49,7 +49,7 @@ class MatrixClientSettings(BaseSettings):
user_id: str
password: str
device_name: str
store_path: str
store_path: Optional[str] = None
verify_ssl: Optional[bool] = True
class Config:
@ -60,7 +60,10 @@ class MatrixClientSettings(BaseSettings):
class MatrixClient:
def __init__(self, settings: MatrixClientSettings, join_rooms: List[str]):
self.settings = settings
self.store_path = pathlib.Path(settings.store_path)
bot_state_dir = os.environ.get("BOT_STATE_DIR")
if bot_state_dir is None:
raise ValueError("BOT_STATE_DIR env var is required")
self.store_path = pathlib.Path(bot_state_dir).joinpath("matrix")
self.join_rooms = join_rooms
self.credential_store = LocalCredentialStore(
self.store_path.joinpath("credentials.json")
@ -76,8 +79,7 @@ class MatrixClient:
self.greeting_sent = False
if self.store_path and not os.path.isdir(self.store_path):
os.mkdir(self.store_path)
self.store_path.mkdir(parents=True, exist_ok=True)
async def start(self) -> None:
await self.login()
@ -108,7 +110,7 @@ class MatrixClient:
self.client = AsyncClient(
homeserver=self.settings.homeserver,
user=self.settings.user_id,
store_path=str(self.settings.store_path),
store_path=str(self.store_path),
config=self.client_config,
ssl=self.settings.verify_ssl,
)