Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(desktop): make start-prod launch Electron correctly on NixOS
Elian Dorancbb1d53f253e
diff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex 63900becbd..e68398d2f8 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -14,8 +14,8 @@"dev": "cross-env TRILIUM_PORT=37742 TRILIUM_DATA_DIR=data TRILIUM_ELECTRON_DATA_DIR=data-electron-37742 tsx ../../scripts/electron-start.mts src/main.ts","start-no-dir": "cross-env TRILIUM_PORT=37743 tsx ../../scripts/electron-start.mts src/main.ts","build": "tsx scripts/build.ts",- "start-prod": "pnpm build && cross-env TRILIUM_DATA_DIR=data TRILIUM_ELECTRON_DATA_DIR=data-electron-37841 TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 electron dist",- "start-prod-no-dir": "pnpm build && cross-env TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 electron dist",+ "start-prod": "pnpm build && cross-env TRILIUM_DATA_DIR=data TRILIUM_ELECTRON_DATA_DIR=data-electron-37841 TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 tsx ../../scripts/electron-run-prod.mts dist",+ "start-prod-no-dir": "pnpm build && cross-env TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 tsx ../../scripts/electron-run-prod.mts dist","electron-forge:make": "pnpm build && electron-forge make dist","electron-forge:make-flatpak": "pnpm build && DEBUG=* electron-forge make dist --targets=@electron-forge/maker-flatpak","electron-forge:package": "pnpm build && electron-forge package dist",diff --git a/scripts/electron-run-prod.mts b/scripts/electron-run-prod.mtsnew file mode 100644index 0000000000..056ecfa089--- /dev/null+++ b/scripts/electron-run-prod.mts@@ -0,0 +1,33 @@+import { execSync } from "child_process";++import { getElectronPath, getNixLdLibraryPath, isNixOS } from "./utils.mjs";++// Launches the built Electron app (apps/desktop/dist) for `start-prod`.+//+// This mirrors the NixOS handling in electron-start.mts so production launches+// work on NixOS too, where the npm prebuilt Electron binary can't find FHS+// system libraries (libcups.so.2, libgtk-3, libnss3, ...). On every other+// platform this is a no-op wrapper that simply runs `electron <args>`, so+// behaviour is unchanged there.+//+// Unlike the dev launcher, this expects a fully-built bundle: no preload+// compilation, no tsx runtime, and the caller (package.json) supplies the+// TRILIUM_* / ELECTRON_IS_DEV env vars via cross-env.++const args = process.argv.slice(2);+const electronPath = getElectronPath();++const env = { ...process.env };+let extraArgs = "";++if (isNixOS()) {+ env.LD_LIBRARY_PATH = getNixLdLibraryPath();+ // The Nix Electron's chrome-sandbox helper isn't setuid-root, so the+ // sandbox fails to start; disable it (matches the dev launcher).+ extraArgs = " --no-sandbox";+}++execSync(`${electronPath} ${args.join(" ")}${extraArgs}`, {+ stdio: "inherit",+ env+});diff --git a/scripts/electron-start.mts b/scripts/electron-start.mtsindex 19aa4d9da9..26258e0810 100644--- a/scripts/electron-start.mts+++ b/scripts/electron-start.mts@@ -2,7 +2,7 @@ import { execSync } from "child_process";import { buildSync } from "esbuild";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";-import { getElectronPath, isNixOS } from "./utils.mjs";+import { getElectronPath, getNixLdLibraryPath, isNixOS } from "./utils.mjs";// Compile the preload script to CJS so Electron's sandboxed renderer can load it.// Always build from apps/desktop/ regardless of CWD — window.ts resolves the@@ -17,7 +17,7 @@ buildSync({external: ["electron"]});-const LD_LIBRARY_PATH = isNixOS() && execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8") + "/lib";+const LD_LIBRARY_PATH = isNixOS() && getNixLdLibraryPath();const args = process.argv.slice(2);execSync(`${getElectronPath()} ${args.join(" ")} --no-sandbox`, {diff --git a/scripts/utils.mts b/scripts/utils.mtsindex 02883db106..d0a3eda830 100644--- a/scripts/utils.mts+++ b/scripts/utils.mts@@ -50,4 +50,13 @@ export function getElectronPath() {} else {return "electron";}+}++/**+ * On NixOS the Nix-provided Electron is dynamically linked against libstdc+++ * from gcc.cc.lib. Resolve that store path so callers can add it to+ * LD_LIBRARY_PATH before launching Electron.+ */+export function getNixLdLibraryPath() {+ return execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8").trimEnd() + "/lib";}\ No newline at end of file