|
| 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