build(nix): add flake.nix for self-hosted NixOS deployment (frontend + api systemd service + nginx)
This commit is contained in:
1 parent
5b4286f934
commit
8308878ec5
1 file changed
+200
@@ -0,0 +1,200 @@
|
||||
{
|
||||
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 from package-lock.json, not knowable ahead of time on a
|
||||
# machine without `nix` installed. Run `nix build .#frontend` once:
|
||||
# it fails with "hash mismatch ... got: sha256-XXXX=" - paste that
|
||||
# value here and rebuild.
|
||||
npmDepsHash = pkgs.lib.fakeHash;
|
||||
|
||||
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 node_modules $out/node_modules
|
||||
cp package.json $out/package.json
|
||||
runHook postInstall
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
packages = {
|
||||
default = commons;
|
||||
frontend = commons;
|
||||
};
|
||||
|
||||
devShells.default = pkgs.mkShell {
|
||||
packages = [
|
||||
nodejs
|
||||
pkgs.nodePackages.npm
|
||||
];
|
||||
};
|
||||
}
|
||||
)
|
||||
// {
|
||||
# `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";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user