zerobyte/app/server/db/db.ts
James Brink 5cb092f130 feat: add pure NixOS flake with bun2nix
Adds a complete Nix flake for NixOS integration:

Package:
- Pure reproducible builds using bun2nix (no network during build)
- Bundles restic, rclone, shoutrrr, and other runtime dependencies
- Supports x86_64-linux, aarch64-linux, x86_64-darwin, aarch64-darwin
- Version read from package.json to avoid drift

NixOS Module:
- Systemd service with security hardening
- FUSE support for remote mounts (NFS, SMB, WebDAV)
- Configurable options: port, dataDir, user/group, timezone, etc.
- protectHome option for backing up home directories
- CAP_DAC_READ_SEARCH capability when protectHome=false
- PORT env var properly passed to server

Development:
- devShell with bun, node, biome, typescript, and runtime tools
- bun2nix CLI for regenerating bun.nix after dependency changes

Also includes:
- Overlay exposing zerobyte and shoutrrr packages
- NixOS VM integration tests
- Experimental darwin module (requires TCC permissions for home dirs)
- Configurable MIGRATIONS_PATH and PORT for Nix store compatibility

Note: package.json includes additional peer dependencies (@standard-community/*,
@standard-schema/spec, openapi-types, quansync, react-is) required by bun2nix
for pure builds - bun requires all transitive peer deps to be satisfied.
2025-12-27 00:01:46 -07:00

34 lines
1.1 KiB
TypeScript

import "dotenv/config";
import { Database } from "bun:sqlite";
import path from "node:path";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import { DATABASE_URL } from "../core/constants";
import * as schema from "./schema";
import fs from "node:fs/promises";
import { config } from "../core/config";
await fs.mkdir(path.dirname(DATABASE_URL), { recursive: true });
const sqlite = new Database(DATABASE_URL);
export const db = drizzle({ client: sqlite, schema });
export const runDbMigrations = () => {
let migrationsFolder: string;
// Migration path priority:
// 1. MIGRATIONS_PATH env var (Nix, custom deployments)
// 2. /app/assets/migrations (Docker production)
// 3. /app/app/drizzle (Docker development)
if (config.migrationsPath) {
migrationsFolder = config.migrationsPath;
} else if (config.__prod__) {
migrationsFolder = path.join("/app", "assets", "migrations");
} else {
migrationsFolder = path.join("/app", "app", "drizzle");
}
migrate(db, { migrationsFolder });
sqlite.run("PRAGMA foreign_keys = ON;");
};