1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
{ depot, pkgs, lib, config, ... }:

let
  cfg = config.profpatsch.softwaregardening;
  booster-bot = depot.users.Profpatsch.booster-bot;
  asciinema-server = depot.users.Profpatsch.asciinema-server;
  osgameclones-server = depot.users.Profpatsch.osgameclones-server;
  sfttime-site = depot.users.Profpatsch.sfttime.site;
  auxciv-site = depot.users.Profpatsch.auxciv.site;
  fedimonth-site = depot.users.Profpatsch.fedimonth.site;
  firefox-profiler = depot.users.Profpatsch.firefox-profiler;
  inventory = depot.users.Profpatsch.inventory;

  # Public test instance of the voice+vision inventory app.
  inventoryBaseUrl = "https://inventory-test.softwaregardening.org";

  # Month of the Fediverse GoToSocial instance (split-domain deployment).
  #
  #   account-domain: fedimonth.softwaregardening.org  (the handle + website)
  #   host:           social.fedimonth.softwaregardening.org  (where GTS runs)
  #
  # Handle becomes @fedimonth@fedimonth.softwaregardening.org.
  #
  # DANGER: host / account-domain and the account name are IRREVERSIBLE once
  # the instance has federated with anyone. Do not change them afterwards.
  fedimonthAccountDomain = "fedimonth.softwaregardening.org";
  fedimonthHost = "social.fedimonth.softwaregardening.org";

  asciinemaEnv = {
    DATABASE_PATH = "/var/lib/asciinema-server/asciinema.db";
    BASE_URL = "https://asciinema.softwaregardening.org";
    APP_TITLE = "asciinema.softwaregardening.org";
  };

  # Wrapper with env pre-set for admin use on the server
  asciinema-server-cmd = pkgs.writeShellScriptBin "asciinema-server" ''
    ${lib.concatStringsSep "\n" (lib.mapAttrsToList (k: v: "export ${k}=${lib.escapeShellArg v}") asciinemaEnv)}
    exec ${asciinema-server}/bin/asciinema-server "$@"
  '';

  placeholderPage = pkgs.writeTextDir "index.html" ''
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Software Gardening</title>
      <style>
        body {
          font-family: sans-serif;
          max-width: 60em;
          margin: 4em auto;
          padding: 0 2em;
          background: #fff;
          color: #111;
        }
        @media (prefers-color-scheme: dark) {
          body { background: #1a1a1a; color: #ddd; }
        }
      </style>
    </head>
    <body>
      <h1>Software Gardening</h1>
    </body>
    </html>
  '';

in {
  options.profpatsch.softwaregardening = {
    enable = lib.mkEnableOption "softwaregardening.org";

    boosterBotPort = lib.mkOption {
      type = lib.types.port;
      default = 8765;
      description = "Port for the booster-bot ActivityPub service";
    };

    boosterBotAdmins = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [];
      description = "List of ActivityPub actor URLs that are admins of the bot";
      example = [ "https://mastodon.xyz/users/Profpatsch" ];
    };

    asciinemaPort = lib.mkOption {
      type = lib.types.port;
      default = 8767;
      description = "Port for the asciinema-server service";
    };

    osgamesPort = lib.mkOption {
      type = lib.types.port;
      default = 8768;
      description = "Port for the osgameclones-server service";
    };

    profilerPort = lib.mkOption {
      type = lib.types.port;
      default = 8769;
      description = "Port for the firefox-profiler service";
    };

    fedimonthGtsPort = lib.mkOption {
      type = lib.types.port;
      default = 8770;
      description = "Local port for the Month of the Fediverse GoToSocial instance";
    };

    inventoryPort = lib.mkOption {
      type = lib.types.port;
      default = 8771;
      description = "Local port for the inventory voice+vision capture app";
    };
  };

  config = lib.mkIf cfg.enable {

    services.nginx.virtualHosts."asciinema.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.asciinemaPort}";
        proxyWebsockets = true;
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        '';
      };
    };

    systemd.services.asciinema-server = {
      description = "asciinema-server minimal asciinema-compatible server";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      environment = asciinemaEnv // {
        LISTEN_ADDR = "127.0.0.1:${toString cfg.asciinemaPort}";
      };
      serviceConfig = {
        ExecStart = "${asciinema-server}/bin/asciinema-server serve";
        StateDirectory = "asciinema-server";
        DynamicUser = true;
        Restart = "always";
        RestartSec = "5s";
      };
    };

    services.nginx.virtualHosts."games.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.osgamesPort}";
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        '';
      };
    };

    systemd.services.osgameclones-server = {
      description = "osgameclones-server — open source games browser";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = "${osgameclones-server}/bin/osgameclones-server -db /var/lib/ossgames/games.db -addr 127.0.0.1:${toString cfg.osgamesPort}";
        StateDirectory = "ossgames";
        DynamicUser = true;
        Restart = "always";
        RestartSec = "5s";
        # The db is manually placed at /var/lib/ossgames/games.db;
        # the service is read-only so DynamicUser is safe.
        ReadWritePaths = [ "/var/lib/ossgames" ];
      };
    };

    services.nginx.virtualHosts."sfttime.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/".root = sfttime-site;
    };

    services.nginx.virtualHosts."auxciv.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/".root = auxciv-site;
    };

    services.nginx.virtualHosts."firefox-profiler.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.profilerPort}";
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        '';
      };
    };

    systemd.services.firefox-profiler = {
      description = "firefox-profiler — local Firefox Profiler frontend server";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = "${firefox-profiler}/bin/firefox-profiler -port ${toString cfg.profilerPort}";
        DynamicUser = true;
        Restart = "always";
        RestartSec = "5s";
      };
    };

    # --- inventory: voice+vision inventory capture (public test instance) -----

    services.nginx.virtualHosts."inventory-test.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.inventoryPort}";
        # /ws/capture is a long-lived WebSocket; /capture/stats is SSE.
        proxyWebsockets = true;
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          # Keep the capture WebSocket + SSE stream alive (default is 60s) and
          # don't buffer the SSE stats stream.
          proxy_read_timeout 3600s;
          proxy_send_timeout 3600s;
          proxy_buffering off;
          # Full-resolution archival photo uploads.
          client_max_body_size 40M;
        '';
      };
    };

    systemd.services.inventory = {
      description = "inventory — voice+vision inventory capture app";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = pkgs.lib.escapeShellArgs [
          "${inventory}/bin/inventory"
          "serve"
          "--addr=127.0.0.1:${toString cfg.inventoryPort}"
          "--base-url=${inventoryBaseUrl}"
          "/var/lib/inventory/inventory.db"
        ];
        StateDirectory = "inventory";
        DynamicUser = true;
        Restart = "always";
        RestartSec = "5s";
        # The Gemini API key is provided as a systemd credential. The source file
        # lives in a dedicated, root-owned secrets dir OUTSIDE the app's own
        # StateDirectory — so the DynamicUser service has no direct filesystem
        # access to it and it isn't intermingled with (or swept into backups of)
        # the app's data. systemd reads it as root at unit start and copies it
        # into the private per-service credentials tmpfs (mode 0400, wiped on
        # stop); the app reads it via GEMINI_API_KEY_FILE, so the key never
        # enters the environment or the nix store.
        LoadCredential = [ "gemini-key:/var/lib/secrets/inventory/gemini-key" ];
        Environment = [ "GEMINI_API_KEY_FILE=%d/gemini-key" ];
      };
    };

    services.nginx.virtualHosts."softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/".root = placeholderPage;
    };

    services.nginx.virtualHosts."happy.softwaregardening.org" = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.boosterBotPort}";
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
        '';
      };
    };

    environment.systemPackages = [ asciinema-server-cmd inventory ];

    systemd.services.booster-bot = {
      description = "booster-bot ActivityPub link-booster bot";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = pkgs.lib.escapeShellArgs [
          "${booster-bot}/bin/booster-bot"
          "-config"
          (pkgs.writeText "booster-bot-config.json" (builtins.toJSON {
            domain = "happy.softwaregardening.org";
            port = toString cfg.boosterBotPort;
            data_dir = "/var/lib/booster-bot";
            admins = cfg.boosterBotAdmins;
          }))
        ];
        StateDirectory = "booster-bot";
        DynamicUser = true;
        Restart = "always";
        RestartSec = "5s";
      };
    };

    # --- Month of the Fediverse: GoToSocial (split-domain) -----------------

    # Host domain: GoToSocial itself runs here, behind nginx.
    services.nginx.virtualHosts.${fedimonthHost} = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        proxyPass = "http://127.0.0.1:${toString cfg.fedimonthGtsPort}";
        proxyWebsockets = true;
        extraConfig = ''
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
          # Allow media uploads.
          client_max_body_size 40M;
        '';
      };
    };

    # Account domain: serves the static landing site, and redirects the
    # webfinger/host-meta/nodeinfo well-known endpoints to the host domain
    # (per GoToSocial split-domain docs). Note: /api/* is deliberately NOT
    # redirected, as that breaks client login heuristics.
    services.nginx.virtualHosts.${fedimonthAccountDomain} = {
      forceSSL = true;
      enableACME = true;
      locations."/".root = fedimonth-site;
      locations."/.well-known/webfinger".return =
        "301 https://${fedimonthHost}$request_uri";
      locations."/.well-known/host-meta".return =
        "301 https://${fedimonthHost}$request_uri";
      locations."/.well-known/nodeinfo".return =
        "301 https://${fedimonthHost}$request_uri";
    };

    systemd.services.gotosocial-fedimonth = {
      description = "GoToSocial instance for Month of the Fediverse";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = pkgs.lib.escapeShellArgs [
          "${pkgs.gotosocial}/bin/gotosocial"
          "--config-path"
          (pkgs.writeText "gotosocial-fedimonth.yaml" (builtins.toJSON {
            host = fedimonthHost;
            account-domain = fedimonthAccountDomain;
            bind-address = "127.0.0.1";
            port = cfg.fedimonthGtsPort;
            protocol = "https";
            # Bundled web assets/templates ship in the nix package's share dir;
            # the default relative ./web path does not exist, so point at them.
            web-asset-base-dir = "${pkgs.gotosocial}/share/gotosocial/web/assets";
            web-template-base-dir = "${pkgs.gotosocial}/share/gotosocial/web/template";
            # TLS is terminated by nginx.
            letsencrypt-enabled = false;
            tls-certificate-chain = "";
            tls-certificate-key = "";
            trusted-proxies = [ "127.0.0.1/32" "::1/128" ];
            # SQLite stored under the systemd StateDirectory.
            db-type = "sqlite";
            db-address = "/var/lib/gotosocial-fedimonth/sqlite.db";
            storage-backend = "local";
            storage-local-base-path = "/var/lib/gotosocial-fedimonth/storage";
            # Single-user instance: no open registrations, invite-only.
            accounts-registration-open = false;
          }))
          "server"
          "start"
        ];
        StateDirectory = "gotosocial-fedimonth";
        # GoToSocial needs persistent state (sqlite + media), so use a stable
        # dedicated user rather than DynamicUser.
        DynamicUser = false;
        User = "gotosocial-fedimonth";
        Group = "gotosocial-fedimonth";
        Restart = "always";
        RestartSec = "5s";
      };
    };

    users.users.gotosocial-fedimonth = {
      isSystemUser = true;
      group = "gotosocial-fedimonth";
    };
    users.groups.gotosocial-fedimonth = { };

  };
}