Files
patriothacks/flake.nix
T
OmO Orchestrator a1cefa75d1 fix(flake): explicitly put openssl on PATH for nix run
'nix run' threw 'spawnSync openssl ENOENT' on the real NixOS target even
though openssl is installed there system-wide - nix run's environment
doesn't carry ambient host PATH entries like /usr/bin, only what the
flake explicitly provides. run-local.mjs shells out to openssl to
generate its HTTPS cert, so pkgs.openssl now gets explicitly prefixed
onto PATH in the apps.default wrapper script, guaranteeing it's
reachable regardless of host state - the actual Nix-idiomatic fix,
not a hope that a system binary happens to be on PATH.

Verified via the real flake: nix build succeeded, and nix run fetched
openssl-3.5.9-bin from cache.nixos.org specifically because of this
reference (proof it is a real, explicit dependency now), then served
HTTPS successfully with a real generated cert.
2026-11-20 23:48:10 -05:00

223 lines
9.0 KiB
Nix

{
description = "THE COMMONS - local-first civic super-app. Static frontend (Vite/React) plus an optional self-hosted backend (blob store + signed-event relay) for the NixOS target server.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{ self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
# `node:sqlite`'s DatabaseSync is stable since Node 22.5 (see
# api/_lib/blobStore.ts's own doc comment), and api/_lib/devServer.ts
# is executed directly as a .ts file relying on Node's built-in
# TypeScript type-stripping, which needs no flag from Node 23.6+/24.
# If this nixpkgs channel does not yet have `nodejs_24`, fall back to
# `pkgs.nodejs_22` and add "--experimental-strip-types" as the first
# argument in the systemd service's ExecStart below.
nodejs = pkgs.nodejs_24;
commons = pkgs.buildNpmPackage {
pname = "the-commons-hollow";
version = "0.0.0";
src = ./.;
inherit nodejs;
# ffmpeg-static's postinstall downloads a prebuilt ffmpeg binary over
# the network; the Nix build sandbox has none. It is a devDependency
# used only by the offline e2e video-rendering pipeline
# (e2e/openmotion/*) - never by `npm run build` or the api/ server -
# so skipping ALL install scripts is safe. (Confirmed via
# package-lock.json: only ffmpeg-static and the macOS-only, Linux-
# irrelevant fsevents declare an install script; typescript@7 does
# not ship or fetch a native binary.)
npmFlags = [ "--ignore-scripts" ];
# Computed via `nix build .#frontend` against package-lock.json as
# committed at the time this flake was written. If package-lock.json
# changes, this hash goes stale - re-run `nix build .#frontend`, it
# fails with "got: sha256-XXXX=", paste that value in.
npmDepsHash = "sha256-RQAKbVaJMRVANpXB9n5XX3YT7nePjJOEIPFET6trjf8=";
npmBuildScript = "build";
# Keep dist/ (served by nginx), api/ + node_modules/ (run directly
# by the systemd service below - api/*.ts needs no separate build
# step, and @noble/ed25519 + @noble/curves are its only runtime
# deps, already in node_modules from the npm install above).
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r dist $out/dist
cp -r api $out/api
cp -r scripts $out/scripts
cp -r node_modules $out/node_modules
cp package.json $out/package.json
runHook postInstall
'';
};
in
{
packages = {
default = commons;
frontend = commons;
};
devShells.default = pkgs.mkShell {
# `nodejs_24` already bundles npm - no separate `nodePackages.npm`
# (that attribute set was removed from recent nixpkgs; most of what
# it held now lives at the top level, per the nixpkgs manual).
packages = [ nodejs ];
};
# `nix run .` - ad-hoc local run, no system config, no persistent
# service: one process serving both the static frontend and the
# api/* handlers on one auto-picked free port (tries 3001, falls
# back to any free port if taken). Ctrl+C or `kill <pid>` stops it;
# only the SQLite files under ./.data survive the process exiting.
apps.default = {
type = "app";
# `nix run`'s environment carries only what the flake explicitly
# puts on PATH - NOT ambient system binaries like a host's
# /usr/bin/openssl (confirmed: this threw `spawnSync openssl
# ENOENT` on the real NixOS target even though openssl is
# installed system-wide there - nix run's PATH simply never saw
# it). run-local.mjs shells out to openssl to generate its
# self-signed HTTPS cert, so it must be explicitly prefixed onto
# PATH here, not assumed to already be reachable.
program = "${pkgs.writeShellScript "commons-run" ''
export PATH="${pkgs.openssl}/bin:$PATH"
exec ${nodejs}/bin/node ${commons}/scripts/run-local.mjs
''}";
};
}
)
// {
# `nixos-rebuild --flake .#<host>` on the target server imports this
# module, e.g.:
#
# imports = [ inputs.the-commons-hollow.nixosModules.default ];
# services.commons = {
# enable = true;
# domain = "commons.example.com";
# };
nixosModules.default =
{ config, lib, pkgs, ... }:
let
cfg = config.services.commons;
stateDirName = lib.removePrefix "/var/lib/" cfg.dataDir;
in
{
options.services.commons = {
enable = lib.mkEnableOption "THE COMMONS (local-first civic super-app)";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.system}.default;
description = "The built THE COMMONS package (frontend dist/ + api/ + node_modules).";
};
domain = lib.mkOption {
type = lib.types.str;
example = "commons.example.com";
description = "Public domain name nginx serves the app on.";
};
apiPort = lib.mkOption {
type = lib.types.port;
default = 3001;
description = ''
Port the blob store + signed-event relay API listens on
(api/_lib/devServer.ts - the same handlers Vercel's serverless
runtime would call, run here as one long-running process).
'';
};
enableNginx = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether this module configures an nginx virtual host serving
dist/ and reverse-proxying /api/*. Set false to front it with
your own reverse proxy instead - the API still listens on
127.0.0.1:apiPort either way.
'';
};
dataDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/commons";
description = ''
Persistent directory for the blob store and event-relay
SQLite files (BLOB_DB_PATH / EVENT_DB_PATH). Must be a path
under /var/lib - its basename is used as the systemd
StateDirectory so the dynamically-assigned service user
still gets a persistent, correctly-owned directory across
restarts and reboots.
'';
};
};
config = lib.mkIf cfg.enable {
systemd.services.commons-api = {
description = "THE COMMONS - blob store + signed-event relay API";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
PORT = toString cfg.apiPort;
BLOB_DB_PATH = "${cfg.dataDir}/blobs.sqlite3";
EVENT_DB_PATH = "${cfg.dataDir}/events.sqlite3";
};
serviceConfig = {
# `pkgs` here is this NixOS module's own pkgs argument (the
# target server's system pkgs), a different scope from the
# `nodejs`/`pkgs` let-bound inside the per-system
# eachDefaultSystem output above - this module is evaluated
# standalone when imported into a NixOS configuration, not
# inside that flake-utils closure.
ExecStart = "${pkgs.nodejs_24}/bin/node ${cfg.package}/api/_lib/devServer.ts";
WorkingDirectory = cfg.package;
# No static user to manage: systemd allocates one at
# activation and owns StateDirectory for it, which is exactly
# the "a SQLite file that must survive restarts, nothing
# else" shape this service needs.
DynamicUser = true;
StateDirectory = stateDirName;
StateDirectoryMode = "0750";
Restart = "always";
RestartSec = 5;
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
};
};
services.nginx = lib.mkIf cfg.enableNginx {
enable = true;
virtualHosts.${cfg.domain} = {
locations."/api/" = {
proxyPass = "http://127.0.0.1:${toString cfg.apiPort}/api/";
};
locations."/" = {
root = "${cfg.package}/dist";
tryFiles = "$uri /index.html";
};
};
};
};
};
};
}