Refactor Nix build configuration for mempool

This commit is contained in:
Sovran Systems
2026-08-10 18:07:46 -05:00
committed by GitHub
parent d733187ec5
commit 820d850d00
+16 -40
View File
@@ -60,7 +60,6 @@ let
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
# napi doesn't accept an absolute path as dest dir, so we can't directly write to $out
napi build --platform --release --strip out napi build --platform --release --strip out
runHook postBuild runHook postBuild
''; '';
@@ -79,20 +78,26 @@ in rec {
mempool-backend = buildNpmPackage { mempool-backend = buildNpmPackage {
pname = "mempool-backend"; pname = "mempool-backend";
inherit version src; inherit version src;
npmRoot = "backend";
# postPatch runs in the npmDeps fetcher too. Copy the lock file to # postPatch runs in the npmDeps fetcher too. Copy the lock file to
# the repo root so prefetch-npm-deps can find it, then apply patches # the repo root so prefetch-npm-deps can find it, then apply patches.
# in the main build.
# Also create a dummy rust-gbt stub so `npm ci --offline` can resolve # Also create a dummy rust-gbt stub so `npm ci --offline` can resolve
# the `file:./rust-gbt` dependency before the real native module is # the `file:./rust-gbt` dependency before the real native module is
# synced in buildPhase. # synced in buildPhase.
postPatch = '' postPatch = ''
cp backend/package-lock.json . cp backend/package-lock.json ./package-lock.json
cp backend/package.json ./package.json
patch -p1 < ${./0001-allow-disabling-mining-pool-fetching.patch} patch -p1 < ${./0001-allow-disabling-mining-pool-fetching.patch}
mkdir -p backend/rust-gbt mkdir -p backend/rust-gbt
cat > backend/rust-gbt/package.json <<'EOF' cat > backend/rust-gbt/package.json <<'EOF'
{"name":"rust-gbt","version":"0.0.1","main":"index.js"} {"name":"rust-gbt","version":"0.0.1","main":"index.js"}
EOF EOF
# also need stub at repo root for the fetcher's npm ci at root
mkdir -p rust-gbt
cat > rust-gbt/package.json <<'EOF'
{"name":"rust-gbt","version":"0.0.1","main":"index.js"}
EOF
''; '';
npmDepsHash = "sha256-tuMrdc9vw5CWzaL1xRxZnskgGwElWv8qz4LSNvSUdXI="; npmDepsHash = "sha256-tuMrdc9vw5CWzaL1xRxZnskgGwElWv8qz4LSNvSUdXI=";
@@ -100,40 +105,29 @@ in rec {
makeCacheWritable = true; makeCacheWritable = true;
npmFlags = [ "--legacy-peer-deps" ]; npmFlags = [ "--legacy-peer-deps" ];
nativeBuildInputs = [ nativeBuildInputs = [ makeWrapper rsync ];
makeWrapper
rsync
];
dontNpmBuild = true; dontNpmBuild = true;
dontNpmInstall = true; dontNpmInstall = true;
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
cd backend cd backend
patchShebangs node_modules patchShebangs node_modules
${sync} ${mempool-rust-gbt}/ rust-gbt ${sync} ${mempool-rust-gbt}/ rust-gbt
npm run package npm run package
runHook postBuild runHook postBuild
''; '';
installPhase = '' installPhase = ''
mkdir -p $out/lib/mempool-backend mkdir -p $out/lib/mempool-backend
${sync} package/ $out/lib/mempool-backend ${sync} package/ $out/lib/mempool-backend
makeWrapper ${nodejs-slim_22}/bin/node $out/bin/mempool-backend \ makeWrapper ${nodejs-slim_22}/bin/node $out/bin/mempool-backend \
--add-flags $out/lib/mempool-backend/index.js --add-flags $out/lib/mempool-backend/index.js
runHook postInstall runHook postInstall
''; '';
passthru = { passthru = { nodejs = nodejs_22; nodejsRuntime = nodejs-slim_22; };
nodejs = nodejs_22;
nodejsRuntime = nodejs-slim_22;
};
meta = with lib; { meta = with lib; {
description = "Bitcoin blockchain and mempool explorer (backend)"; description = "Bitcoin blockchain and mempool explorer (backend)";
@@ -145,61 +139,43 @@ in rec {
mempool-frontend = mkFrontend {}; mempool-frontend = mkFrontend {};
# Argument `config` (type: attrset) defines the mempool frontend config.
# If `{}`, the default config is used.
mkFrontend = config: buildNpmPackage { mkFrontend = config: buildNpmPackage {
pname = "mempool-frontend"; pname = "mempool-frontend";
inherit version src; inherit version src;
npmRoot = "frontend";
# postPatch runs in the npmDeps fetcher too. Copy the lock file to
# the repo root so prefetch-npm-deps can find it.
postPatch = '' postPatch = ''
cp frontend/package-lock.json . cp frontend/package-lock.json ./package-lock.json
cp frontend/package.json ./package.json
''; '';
npmDepsHash = "sha256-/UwK0X9knsqTSAmnh2+jk35SK/J7DjBUhsR7e6OOn8Y="; npmDepsHash = "sha256-/UwK0X9knsqTSAmnh2+jk35SK/J7DjBUhsR7e6OOn8Y=";
npmDepsFetcherVersion = 2; npmDepsFetcherVersion = 2;
nativeBuildInputs = [ nativeBuildInputs = [ makeWrapper rsync ];
makeWrapper
rsync
];
dontNpmBuild = true; dontNpmBuild = true;
dontNpmInstall = true; dontNpmInstall = true;
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
cd frontend cd frontend
patchShebangs node_modules patchShebangs node_modules
# sync-assets.js is called during `npm run build` and downloads assets from the
# internet. Disable this script and instead add the assets manually after building.
: > sync-assets.js : > sync-assets.js
${lib.optionalString (config != {}) '' ${lib.optionalString (config != {}) ''
ln -s ${builtins.toFile "mempool-frontend-config" (builtins.toJSON config)} mempool-frontend-config.json ln -s ${builtins.toFile "mempool-frontend-config" (builtins.toJSON config)} mempool-frontend-config.json
''} ''}
npm run build npm run build
# Add assets that would otherwise be downloaded by sync-assets.js
${sync} ${frontendAssets}/ dist/mempool/browser/resources ${sync} ${frontendAssets}/ dist/mempool/browser/resources
runHook postBuild runHook postBuild
''; '';
installPhase = '' installPhase = ''
${sync} dist/mempool/browser/ $out ${sync} dist/mempool/browser/ $out
runHook postInstall runHook postInstall
''; '';
passthru = { passthru = { withConfig = mkFrontend; assets = frontendAssets; };
withConfig = mkFrontend;
assets = frontendAssets;
};
meta = with lib; { meta = with lib; {
description = "Bitcoin blockchain and mempool explorer (frontend)"; description = "Bitcoin blockchain and mempool explorer (frontend)";