guix_mirror_bot pushed a commit to branch master
in repository guix.

commit 139365874a10204c49a3f78a2dd129da73acfc5a
Author: Jelle Licht <[email protected]>
AuthorDate: Thu Oct 15 13:07:37 2026 +0200

    build/node: Build native addons after install.
    
    * guix/build/node-build-system.scm (build-addon): New procedure.
    (%standard-phases): Add 'build-addon after 'install.
    * guix/build/tree-sitter-build-system.scm (%standard-phases): Delete
    'build-addon.
    * gnu/packages/node-xyz.scm (node-sqlite3)[arguments]<#:phases>: Set
    npm_package_config_node_gyp_sqlite in 'npm-config-sqlite.  Run
    'patch-binding-path after 'build-addon.
---
 gnu/packages/node-xyz.scm               |  5 +++--
 guix/build/node-build-system.scm        | 29 ++++++++++++++++++++++++++++-
 guix/build/tree-sitter-build-system.scm |  1 +
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/gnu/packages/node-xyz.scm b/gnu/packages/node-xyz.scm
index adf7689c15d..c81a97d8c0d 100644
--- a/gnu/packages/node-xyz.scm
+++ b/gnu/packages/node-xyz.scm
@@ -4053,8 +4053,9 @@ connection.")))
            ;; We need this step even if we do replace @mapbox/node-pre-gyp
            ;; because the package expects to build its bundled sqlite
            (lambda* (#:key inputs #:allow-other-keys)
-             (setenv "npm_config_sqlite" (assoc-ref inputs "sqlite"))))
-         (add-after 'install 'patch-binding-path
+             (setenv "npm_package_config_node_gyp_sqlite"
+                     (assoc-ref inputs "sqlite"))))
+         (add-after 'build-addon 'patch-binding-path
            ;; We replace a file that dynamic searches for the addon using
            ;; node-pre-gyp (which we don't have) with a version that
            ;; simply uses the path to the addon we built directly.
diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 3767ae68a39..67fc05d8e05 100644
--- a/guix/build/node-build-system.scm
+++ b/guix/build/node-build-system.scm
@@ -245,6 +245,32 @@ dependencies."
             "--install-links"
             "install" "../package.tgz")))
 
+(define* (build-addon #:key inputs outputs #:allow-other-keys)
+  "Build the native addon of the installed package, as 'npm install' would
+when running scripts."
+  (let* ((node (assoc-ref inputs "node"))
+         (pkg-dir (string-append (assoc-ref outputs "out") "/lib/node_modules/"
+                                 (module-name ".")))
+         (pkg-meta (call-with-input-file
+                       (string-append pkg-dir "/package.json")
+                     json->scm)))
+    (define (delete-except dir keep?)
+      (for-each (lambda (file)
+                  (unless (keep? file)
+                    (delete-file-recursively (string-append dir "/" file))))
+                (scandir dir (negate (cut member <> '("." ".."))))))
+    (when (and (file-exists? (string-append pkg-dir "/binding.gyp"))
+               (not (equal? (assoc "gypfile" pkg-meta) '("gypfile" . #f))))
+      (with-directory-excursion pkg-dir
+        (invoke (string-append node "/bin/node")
+                (string-append node "/lib/node_modules/npm/node_modules"
+                               "/node-gyp/bin/node-gyp.js")
+                "rebuild"
+                (string-append "--nodedir=" node))
+        ;; Keep only the addons.
+        (delete-except "build" (cut string=? <> "Release"))
+        (delete-except "build/Release" (cut string-suffix? ".node" <>))))))
+
 (define* (avoid-node-gyp-rebuild #:key outputs #:allow-other-keys)
   "Adjust the installed 'package.json' to remove an 'install' script that
 would try to run 'node-gyp rebuild'."
@@ -302,7 +328,8 @@ would try to run 'node-gyp rebuild'."
     (replace 'check check)
     (add-before 'install 'repack repack)
     (replace 'install install)
-    (add-after 'install 'avoid-node-gyp-rebuild avoid-node-gyp-rebuild)))
+    (add-after 'install 'build-addon build-addon)
+    (add-after 'build-addon 'avoid-node-gyp-rebuild avoid-node-gyp-rebuild)))
 
 (define* (node-build #:key inputs (phases %standard-phases)
                      #:allow-other-keys #:rest args)
diff --git a/guix/build/tree-sitter-build-system.scm 
b/guix/build/tree-sitter-build-system.scm
index 81ff741f96c..f3e27e48053 100644
--- a/guix/build/tree-sitter-build-system.scm
+++ b/guix/build/tree-sitter-build-system.scm
@@ -146,6 +146,7 @@ and replace development dependencies with tree-sitter 
grammar node modules."
     (replace 'build build)
     (replace 'check check)
     (replace 'install install)
+    (delete 'build-addon)
     (add-after 'install 'install-js install-js)))
 
 (define* (tree-sitter-build #:key inputs (phases %standard-phases)

Reply via email to