From 26c7f0b0170ea334f2687c0f554258dd06a69c49 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 2 Sep 2025 19:56:27 +0300 Subject: [PATCH] feat(dx/desktop): improve rebuilding experience on NixOS --- apps/desktop/scripts/rebuild.mts | 16 +++------------- apps/desktop/scripts/start.mts | 15 ++++----------- scripts/utils.mts | 12 +++++++++++- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/apps/desktop/scripts/rebuild.mts b/apps/desktop/scripts/rebuild.mts index 25b341ada..635d4690f 100644 --- a/apps/desktop/scripts/rebuild.mts +++ b/apps/desktop/scripts/rebuild.mts @@ -1,8 +1,8 @@ import { join } from "path"; -import { cpSync, existsSync, mkdirSync, readFileSync, rmSync } from "fs"; +import { cpSync, existsSync, mkdirSync, rmSync } from "fs"; import { execSync } from "child_process"; import { rebuild } from "@electron/rebuild" -import { isNixOS, resetPath } from "../../../scripts/utils.mjs"; +import { getElectronPath, isNixOS } from "../../../scripts/utils.mjs"; import packageJson from "../package.json" with { type: "json" }; const desktopProjectRoot = join(import.meta.dirname, ".."); @@ -33,24 +33,14 @@ function rebuildNativeDependencies() { buildPath: desktopProjectRoot, electronVersion }); - - if (isNixOS()) { - console.log("Patching ELF..."); - - return execSync(`nix-shell -p auto-patchelf gcc.cc.lib --run "auto-patchelf --paths node_modules/better-sqlite3/build/Release/better_sqlite3.node --libs ${libStdPath}"`, { - cwd: desktopProjectRoot, - stdio: "inherit" - }); - } } function determineElectronVersion() { if (isNixOS()) { console.log("Detected NixOS, reading Electron version from PATH"); - resetPath(); try { - return execSync("electron --version", { }).toString("utf-8"); + return execSync(`${getElectronPath()} --version`, { }).toString("utf-8"); } catch (e) { console.error("Got error while trying to read the Electron version from shell. Make sure that an Electron version is in the PATH (e.g. `nix-shell -p electron`)"); process.exit(1); diff --git a/apps/desktop/scripts/start.mts b/apps/desktop/scripts/start.mts index a2151ba2d..e283a7023 100644 --- a/apps/desktop/scripts/start.mts +++ b/apps/desktop/scripts/start.mts @@ -1,18 +1,11 @@ -import { execSync, spawnSync } from "child_process"; -import { isNixOS, resetPath } from "../../../scripts/utils.mjs"; +import { execSync } from "child_process"; +import { getElectronPath, isNixOS } from "../../../scripts/utils.mjs"; import { join } from "path"; const projectRoot = join(import.meta.dirname, ".."); +const LD_LIBRARY_PATH = isNixOS() && execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8") + "/lib"; -let LD_LIBRARY_PATH = undefined; -let electronPath = "electron"; -if (isNixOS()) { - resetPath(); - LD_LIBRARY_PATH = execSync("nix eval --raw nixpkgs#gcc.cc.lib").toString("utf-8") + "/lib"; - electronPath = execSync("nix eval --raw nixpkgs#electron_37").toString("utf-8") + "/bin/electron"; -} - -execSync(`${electronPath} ./src/main.ts`, { +execSync(`${getElectronPath()} ./src/main.ts`, { stdio: "inherit", cwd: projectRoot, env: { diff --git a/scripts/utils.mts b/scripts/utils.mts index d8cefe4c5..ea63f3760 100644 --- a/scripts/utils.mts +++ b/scripts/utils.mts @@ -1,3 +1,4 @@ +import { execSync } from "child_process"; import { readFileSync } from "fs"; import { platform } from "os"; @@ -7,7 +8,7 @@ export function isNixOS() { return osReleaseFile.includes("ID=nixos"); } -export function resetPath() { +function resetPath() { // On Unix-like systems, PATH is usually inherited from login shell // but npm prepends node_modules/.bin. Let's remove it: const origPath = process.env.PATH || ""; @@ -18,3 +19,12 @@ export function resetPath() { .filter(p => !p.includes("node_modules/.bin")) .join(":"); } + +export function getElectronPath() { + if (isNixOS()) { + resetPath(); + return execSync("nix eval --raw nixpkgs#electron_37").toString("utf-8") + "/bin/electron"; + } else { + return "electron"; + } +} \ No newline at end of file