From 145a3e70c30a415279b723dec70897ba759e0c88 Mon Sep 17 00:00:00 2001 From: Abel Luck Date: Fri, 6 Mar 2026 11:40:57 +0100 Subject: [PATCH] Use BOT_STATE_DIR for matrix state storage --- config.json.sample | 3 +-- nix/modules/nixos/services/matrix-ops-bot.nix | 5 ++++- nix/tests/matrix-ops-bot.nix | 1 - ops_bot/matrix.py | 12 +++++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config.json.sample b/config.json.sample index 1bec7a5..9bbd500 100644 --- a/config.json.sample +++ b/config.json.sample @@ -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 } -} \ No newline at end of file +} diff --git a/nix/modules/nixos/services/matrix-ops-bot.nix b/nix/modules/nixos/services/matrix-ops-bot.nix index 5380202..eb77ae1 100644 --- a/nix/modules/nixos/services/matrix-ops-bot.nix +++ b/nix/modules/nixos/services/matrix-ops-bot.nix @@ -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}"; diff --git a/nix/tests/matrix-ops-bot.nix b/nix/tests/matrix-ops-bot.nix index 72b50ca..11c0bac 100644 --- a/nix/tests/matrix-ops-bot.nix +++ b/nix/tests/matrix-ops-bot.nix @@ -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; }; }; diff --git a/ops_bot/matrix.py b/ops_bot/matrix.py index a2a5e39..da1a56c 100644 --- a/ops_bot/matrix.py +++ b/ops_bot/matrix.py @@ -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, )