|
| 1 | +{ |
| 2 | + config, |
| 3 | + pkgs, |
| 4 | + lib, |
| 5 | + ... |
| 6 | +}: |
| 7 | +let |
| 8 | + cfg = config.services.checkmate-server; |
| 9 | + |
| 10 | + inherit (lib) |
| 11 | + mkEnableOption |
| 12 | + mkPackageOption |
| 13 | + mkIf |
| 14 | + mkOption |
| 15 | + types |
| 16 | + isPath |
| 17 | + ; |
| 18 | + |
| 19 | + assertStringPath = |
| 20 | + optionName: value: |
| 21 | + if isPath value then |
| 22 | + throw '' |
| 23 | + services.checkmate-server.${optionName}: |
| 24 | + ${toString value} |
| 25 | + is a Nix path, but should be a string, since Nix |
| 26 | + paths are copied into the world-readable Nix store. |
| 27 | + '' |
| 28 | + else |
| 29 | + value; |
| 30 | +in |
| 31 | +{ |
| 32 | + options = { |
| 33 | + services.checkmate-server = { |
| 34 | + enable = mkEnableOption "the Checkmate monitoring server"; |
| 35 | + |
| 36 | + package = mkPackageOption pkgs "checkmate-server" { }; |
| 37 | + |
| 38 | + vhostName = mkOption { |
| 39 | + type = types.str; |
| 40 | + default = "checkmate-server"; |
| 41 | + description = "Name of the nginx vhost."; |
| 42 | + }; |
| 43 | + |
| 44 | + enableLocalDB = mkEnableOption "a local MongoDB instance"; |
| 45 | + |
| 46 | + settings = { |
| 47 | + clientHost = mkOption { |
| 48 | + type = types.str; |
| 49 | + default = "http://127.0.0.1"; |
| 50 | + description = "Frontend Host URI."; |
| 51 | + }; |
| 52 | + |
| 53 | + origin = mkOption { |
| 54 | + type = types.str; |
| 55 | + default = "localhost"; |
| 56 | + description = '' |
| 57 | + Origin where requests to server originate from, for CORS purposes. |
| 58 | + ''; |
| 59 | + }; |
| 60 | + |
| 61 | + port = mkOption { |
| 62 | + type = types.port; |
| 63 | + default = 52345; |
| 64 | + description = "Port the Checkmate backend should listen on."; |
| 65 | + }; |
| 66 | + |
| 67 | + logLevel = mkOption { |
| 68 | + type = types.enum [ |
| 69 | + "debug" |
| 70 | + "info" |
| 71 | + "warn" |
| 72 | + "error" |
| 73 | + ]; |
| 74 | + default = "info"; |
| 75 | + description = "Debug level, can be one of: debug, info, warn, error."; |
| 76 | + }; |
| 77 | + |
| 78 | + tokenTTL = mkOption { |
| 79 | + type = types.str; |
| 80 | + default = "1h"; |
| 81 | + description = '' |
| 82 | + Time for token to live in vercel/ms format, see: https://github.com/vercel/ms. |
| 83 | + ''; |
| 84 | + }; |
| 85 | + |
| 86 | + JWTSecretFile = mkOption { |
| 87 | + type = types.path; |
| 88 | + apply = assertStringPath "settings.JWTSecretFile"; |
| 89 | + description = '' |
| 90 | + Path to a file that contains the secret to sign web requests using JSON Web Tokens. |
| 91 | + ''; |
| 92 | + }; |
| 93 | + }; |
| 94 | + |
| 95 | + mongodbUri = mkOption { |
| 96 | + type = types.str; |
| 97 | + default = "mongodb://127.0.0.1:27017/uptime_db"; |
| 98 | + description = '' |
| 99 | + MongoDB connection string. |
| 100 | + See http://docs.mongodb.org/manual/reference/connection-string/ for details. |
| 101 | + ''; |
| 102 | + }; |
| 103 | + }; |
| 104 | + }; |
| 105 | + |
| 106 | + config = mkIf cfg.enable { |
| 107 | + |
| 108 | + services.mongodb = mkIf cfg.enableLocalDB { |
| 109 | + enable = true; |
| 110 | + }; |
| 111 | + |
| 112 | + systemd.services.checkmate-backend = { |
| 113 | + description = "Checkmate backend daemon"; |
| 114 | + wantedBy = [ "multi-user.target" ]; |
| 115 | + after = [ "network.target" ] ++ lib.optionals cfg.enableLocalDB [ "mongodb.service" ]; |
| 116 | + startLimitIntervalSec = 60; |
| 117 | + startLimitBurst = 3; |
| 118 | + environment = { |
| 119 | + CLIENT_HOST = cfg.settings.clientHost; |
| 120 | + LOG_LEVEL = cfg.settings.logLevel; |
| 121 | + DB_CONNECTION_STRING = cfg.mongodbUri; |
| 122 | + ORIGIN = cfg.settings.origin; |
| 123 | + TOKEN_TTL = cfg.settings.tokenTTL; |
| 124 | + PORT = toString cfg.settings.port; |
| 125 | + }; |
| 126 | + serviceConfig = { |
| 127 | + LoadCredential = [ "JWT_SECRET:${cfg.settings.JWTSecretFile}" ]; |
| 128 | + PrivateDevices = true; |
| 129 | + LimitCORE = 0; |
| 130 | + KillSignal = "SIGINT"; |
| 131 | + TimeoutStopSec = "30s"; |
| 132 | + Restart = "on-failure"; |
| 133 | + DynamicUser = true; |
| 134 | + }; |
| 135 | + script = '' |
| 136 | + set -eou pipefail |
| 137 | + shopt -s inherit_errexit |
| 138 | +
|
| 139 | + JWT_SECRET="$(<"$CREDENTIALS_DIRECTORY/JWT_SECRET")" \ |
| 140 | + ${cfg.package}/startserver ${cfg.package}/backend/index.js |
| 141 | + ''; |
| 142 | + }; |
| 143 | + |
| 144 | + services.nginx.virtualHosts.${cfg.vhostName} = { |
| 145 | + locations."/" = { |
| 146 | + root = "${cfg.package}/public"; |
| 147 | + index = "index.html index.htm"; |
| 148 | + tryFiles = "$uri $uri/ /index.html"; |
| 149 | + }; |
| 150 | + locations."/api/" = { |
| 151 | + proxyPass = "http://127.0.0.1:${toString cfg.settings.port}/api/"; |
| 152 | + proxyWebsockets = true; |
| 153 | + }; |
| 154 | + locations."/api-docs/" = { |
| 155 | + proxyPass = "http://127.0.0.1:${toString cfg.settings.port}/api-docs/"; |
| 156 | + proxyWebsockets = true; |
| 157 | + }; |
| 158 | + }; |
| 159 | + |
| 160 | + }; |
| 161 | +} |
0 commit comments