Skip to content

Commit 3231cdb

Browse files
committed
nixos(hetzner-matrix): enable local provisioning service and route
1 parent cea0ac5 commit 3231cdb

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

configs/nixos/hosts/hetzner-matrix/default.nix

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let
4040
database_path = "/var/lib/tuwunel"
4141
address = ["127.0.0.1", "::1"]
4242
port = 8008
43+
# Token-gated registration; token is loaded from an agenix-managed secret file.
4344
allow_registration = true
4445
registration_token_file = "${config.age.secrets.registration-token.path}"
4546
allow_federation = true
@@ -81,6 +82,7 @@ in
8182
imports = [
8283
../../optional/zfs-auto-snapshot.nix
8384
./networking.nix
85+
./local_mindroom_provisioning_service.nix
8486
];
8587

8688
# ── Tuwunel Matrix homeserver ───────────────────────────────────────
@@ -100,6 +102,12 @@ in
100102
group = "tuwunel";
101103
mode = "0400";
102104
};
105+
registration-token-provisioning = {
106+
file = ./secrets/registration-token.age;
107+
owner = "mindroom-local-provisioning";
108+
group = "mindroom-local-provisioning";
109+
mode = "0400";
110+
};
103111
sso-google-secret = {
104112
file = ./secrets/sso-google-secret.age;
105113
owner = "tuwunel";
@@ -172,6 +180,7 @@ in
172180
virtualHosts."${siteDomain}" = {
173181
extraConfig = ''
174182
reverse_proxy /_matrix/* localhost:8008
183+
reverse_proxy /v1/local-mindroom/* localhost:8776
175184
176185
handle /.well-known/matrix/server {
177186
header Content-Type application/json
@@ -198,13 +207,24 @@ in
198207
# Cinny web client (SPA)
199208
virtualHosts."${cinnyDomain}" = {
200209
extraConfig = ''
201-
root * ${cinnyDist}/dist
210+
root * /var/www/cinny/dist
202211
try_files {path} /index.html
203212
file_server
204213
'';
205214
};
206215
};
207216

217+
services.mindroom-local-provisioning = {
218+
enable = true;
219+
repoPath = "/srv/mindroom";
220+
matrixHomeserver = "https://${siteDomain}";
221+
matrixServerName = siteDomain;
222+
matrixRegistrationTokenFile = config.age.secrets.registration-token-provisioning.path;
223+
listenHost = "127.0.0.1";
224+
listenPort = 8776;
225+
corsOrigins = [ "https://${cinnyDomain}" ];
226+
};
227+
208228
# ── General server config ──────────────────────────────────────────
209229

210230
# Packages needed for release pin updates and operational debugging.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Source: https://github.com/mindroom-ai/mindroom/blob/main/scripts/local_mindroom_provisioning_service.nix
2+
{ config, lib, pkgs, ... }:
3+
4+
let
5+
cfg = config.services.mindroom-local-provisioning;
6+
in
7+
{
8+
options.services.mindroom-local-provisioning = {
9+
enable = lib.mkEnableOption "MindRoom local provisioning service";
10+
11+
repoPath = lib.mkOption {
12+
type = lib.types.str;
13+
default = "/srv/mindroom";
14+
description = "Absolute path to a checkout of this repository.";
15+
};
16+
17+
scriptPath = lib.mkOption {
18+
type = lib.types.str;
19+
default = "scripts/local_mindroom_provisioning_service.py";
20+
description = "Path to the provisioning script relative to repoPath.";
21+
};
22+
23+
matrixHomeserver = lib.mkOption {
24+
type = lib.types.str;
25+
default = "https://mindroom.chat";
26+
description = "Matrix homeserver used for /account/whoami token verification.";
27+
};
28+
29+
matrixServerName = lib.mkOption {
30+
type = lib.types.nullOr lib.types.str;
31+
default = null;
32+
description = "Optional Matrix server_name override when it differs from matrixHomeserver host.";
33+
};
34+
35+
matrixRegistrationTokenFile = lib.mkOption {
36+
type = lib.types.str;
37+
description = "File containing the Matrix registration token.";
38+
};
39+
40+
listenHost = lib.mkOption {
41+
type = lib.types.str;
42+
default = "127.0.0.1";
43+
description = "Bind address for the local provisioning HTTP server.";
44+
};
45+
46+
listenPort = lib.mkOption {
47+
type = lib.types.port;
48+
default = 8776;
49+
description = "Bind port for the local provisioning HTTP server.";
50+
};
51+
52+
corsOrigins = lib.mkOption {
53+
type = lib.types.listOf lib.types.str;
54+
default = [ "https://chat.mindroom.chat" ];
55+
description = "CORS origins allowed to call provisioning endpoints from browser UI.";
56+
};
57+
58+
statePath = lib.mkOption {
59+
type = lib.types.str;
60+
default = "/var/lib/mindroom-local-provisioning/state.json";
61+
description = "State file path for pair sessions/connections.";
62+
};
63+
64+
caddyHost = lib.mkOption {
65+
type = lib.types.nullOr lib.types.str;
66+
default = null;
67+
description = "Optional host name to publish through Caddy (for example provisioning.mindroom.chat).";
68+
};
69+
};
70+
71+
config = lib.mkIf cfg.enable {
72+
users.users.mindroom-local-provisioning = {
73+
isSystemUser = true;
74+
group = "mindroom-local-provisioning";
75+
home = "/var/lib/mindroom-local-provisioning";
76+
};
77+
users.groups.mindroom-local-provisioning = { };
78+
79+
systemd.tmpfiles.rules = [
80+
"d /var/lib/mindroom-local-provisioning 0750 mindroom-local-provisioning mindroom-local-provisioning -"
81+
];
82+
83+
systemd.services.mindroom-local-provisioning = {
84+
description = "MindRoom Local Provisioning Service";
85+
after = [ "network-online.target" ];
86+
wants = [ "network-online.target" ];
87+
wantedBy = [ "multi-user.target" ];
88+
89+
environment = {
90+
MATRIX_HOMESERVER = cfg.matrixHomeserver;
91+
MATRIX_REGISTRATION_TOKEN_FILE = cfg.matrixRegistrationTokenFile;
92+
MINDROOM_PROVISIONING_HOST = cfg.listenHost;
93+
MINDROOM_PROVISIONING_PORT = toString cfg.listenPort;
94+
MINDROOM_PROVISIONING_STATE_PATH = cfg.statePath;
95+
MINDROOM_PROVISIONING_CORS_ORIGINS = lib.concatStringsSep "," cfg.corsOrigins;
96+
} // lib.optionalAttrs (cfg.matrixServerName != null) {
97+
MATRIX_SERVER_NAME = cfg.matrixServerName;
98+
};
99+
100+
serviceConfig = {
101+
Type = "simple";
102+
User = "mindroom-local-provisioning";
103+
Group = "mindroom-local-provisioning";
104+
WorkingDirectory = cfg.repoPath;
105+
ExecStart = "${pkgs.uv}/bin/uv run --script ${cfg.repoPath}/${cfg.scriptPath}";
106+
Restart = "on-failure";
107+
RestartSec = "5s";
108+
NoNewPrivileges = true;
109+
PrivateTmp = true;
110+
ProtectHome = true;
111+
ProtectSystem = "strict";
112+
ReadWritePaths = [ "/var/lib/mindroom-local-provisioning" ];
113+
};
114+
};
115+
116+
services.caddy.virtualHosts = lib.mkIf (cfg.caddyHost != null) {
117+
"${cfg.caddyHost}" = {
118+
extraConfig = ''
119+
reverse_proxy localhost:${toString cfg.listenPort}
120+
'';
121+
};
122+
};
123+
};
124+
}

0 commit comments

Comments
 (0)