feat(dx/desktop): improve rebuilding experience on NixOS

This commit is contained in:
Elian Doran 2025-09-02 19:56:27 +03:00
parent d058dbe9af
commit 26c7f0b017
No known key found for this signature in database
3 changed files with 18 additions and 25 deletions

View File

@ -1,8 +1,8 @@
import { join } from "path"; 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 { execSync } from "child_process";
import { rebuild } from "@electron/rebuild" 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" }; import packageJson from "../package.json" with { type: "json" };
const desktopProjectRoot = join(import.meta.dirname, ".."); const desktopProjectRoot = join(import.meta.dirname, "..");
@ -33,24 +33,14 @@ function rebuildNativeDependencies() {
buildPath: desktopProjectRoot, buildPath: desktopProjectRoot,
electronVersion 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() { function determineElectronVersion() {
if (isNixOS()) { if (isNixOS()) {
console.log("Detected NixOS, reading Electron version from PATH"); console.log("Detected NixOS, reading Electron version from PATH");
resetPath();
try { try {
return execSync("electron --version", { }).toString("utf-8"); return execSync(`${getElectronPath()} --version`, { }).toString("utf-8");
} catch (e) { } 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`)"); 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); process.exit(1);

View File

@ -1,18 +1,11 @@
import { execSync, spawnSync } from "child_process"; import { execSync } from "child_process";
import { isNixOS, resetPath } from "../../../scripts/utils.mjs"; import { getElectronPath, isNixOS } from "../../../scripts/utils.mjs";
import { join } from "path"; import { join } from "path";
const projectRoot = join(import.meta.dirname, ".."); 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; execSync(`${getElectronPath()} ./src/main.ts`, {
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`, {
stdio: "inherit", stdio: "inherit",
cwd: projectRoot, cwd: projectRoot,
env: { env: {

View File

@ -1,3 +1,4 @@
import { execSync } from "child_process";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { platform } from "os"; import { platform } from "os";
@ -7,7 +8,7 @@ export function isNixOS() {
return osReleaseFile.includes("ID=nixos"); return osReleaseFile.includes("ID=nixos");
} }
export function resetPath() { function resetPath() {
// On Unix-like systems, PATH is usually inherited from login shell // On Unix-like systems, PATH is usually inherited from login shell
// but npm prepends node_modules/.bin. Let's remove it: // but npm prepends node_modules/.bin. Let's remove it:
const origPath = process.env.PATH || ""; const origPath = process.env.PATH || "";
@ -18,3 +19,12 @@ export function resetPath() {
.filter(p => !p.includes("node_modules/.bin")) .filter(p => !p.includes("node_modules/.bin"))
.join(":"); .join(":");
} }
export function getElectronPath() {
if (isNixOS()) {
resetPath();
return execSync("nix eval --raw nixpkgs#electron_37").toString("utf-8") + "/bin/electron";
} else {
return "electron";
}
}