diff mbox series

[bug#51838,v4,29/45] gnu: Add node-sqlite3.

Message ID 20211213060107.129223-30-philip@philipmcgrath.com
State Accepted
Headers show
Series guix: node-build-system: Support compiling add-ons with node-gyp. | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Philip McGrath Dec. 13, 2021, 6 a.m. UTC
* gnu/packages/node-xyz.scm (node-sqlite3): New variable.
---
 gnu/packages/node-xyz.scm | 132 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
diff mbox series

Patch

diff --git a/gnu/packages/node-xyz.scm b/gnu/packages/node-xyz.scm
index c7bc660134..d1bb236186 100644
--- a/gnu/packages/node-xyz.scm
+++ b/gnu/packages/node-xyz.scm
@@ -655,3 +655,135 @@  (define-public node-addon-api
 @code{libuv} (included in a project via @code{#include <uv.h>}) are not
 ABI-stable across Node.js major versions.")
     (license license:expat)))
+
+(define-public node-sqlite3
+  (package
+    (name "node-sqlite3")
+    (version "5.0.2")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/mapbox/node-sqlite3")
+             (commit (string-append "v" version))))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32 "0sbbzzli282nxyfha10zx0k5m8hdp0sf3ipl59khjb7wm449j86h"))
+       (snippet
+        (with-imported-modules '((guix build utils))
+          #~(begin
+              (use-modules (guix build utils))
+              ;; unbundle sqlite
+              (for-each delete-file-recursively
+                        (find-files "deps"
+                                    (lambda (pth stat)
+                                      (gzip-file? pth)))))))))
+    (inputs
+     `(("node-addon-api" ,node-addon-api)
+       ("python" ,python)
+       ("sqlite" ,sqlite)))
+    (build-system node-build-system)
+    (arguments
+     `(#:tests?
+       #f ; FIXME: tests depend on node-mocha
+       #:modules
+       ((guix build node-build-system)
+        (guix build json)
+        (srfi srfi-1)
+        (ice-9 match)
+        (guix build utils))
+       #:absent-dependencies
+       `(;; Normally, this is "built" using @mapbox/node-pre-gyp,
+         ;; which publishes or downloads pre-built binaries
+         ;; or falls back to building from source.
+         ;; Here, we patch out all of that and just build directly.
+         ;; It would be better to patch a version of @mapbox/node-pre-gyp
+         ;; that always builds from source, as Debian does,
+         ;; but there are a number of dependencies that need
+         ;; to be packaged or removed.
+         "@mapbox/node-pre-gyp"
+         "node-pre-gyp" ;; deprecated name still used in some places
+         "aws-sdk"
+         "@mapbox/cloudfriend"
+         ;; Confusingly, this is only a dependency because of
+         ;; @mapbox/node-pre-gyp: with that removed,
+         ;; npm will use its own copy:
+         "node-gyp"
+         ;; These we'd like, we just don't have them yet:
+         "eslint"
+         "mocha")
+       #:phases
+       (modify-phases %standard-phases
+         (add-before 'configure 'npm-config-sqlite
+           ;; 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
+           ;; 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.
+           ;; The exact path is supposed to depend on things like the
+           ;; architecture and napi_build_version, so, to avoid having
+           ;; hard-code the details accurately, we do this after the addon
+           ;; has been built so we can just find where it ended up.
+           (lambda* (#:key outputs #:allow-other-keys)
+             (with-directory-excursion
+                 (string-append (assoc-ref outputs "out")
+                                "/lib/node_modules/sqlite3/lib")
+               (match (find-files "binding" "\\.node$")
+                 ((rel-path)
+                  (with-atomic-file-replacement "sqlite3-binding.js"
+                    (lambda (in out)
+                      (format out "var binding = require('./~a');\n" rel-path)
+                      (display "module.exports = exports = binding;\n" out))))))))
+         (add-after 'patch-dependencies 'avoid-node-pre-gyp
+           (lambda args
+             ;; We need to patch .npmignore before the 'repack phase
+             ;; so that the built addon is installed with in the package.
+             ;; (Upstream assumes node-pre-gyp will download a pre-built
+             ;; version when this package is installed.)
+             (substitute* ".npmignore"
+               (("lib/binding")
+                "#lib/binding # <- patched for Guix"))
+             ;; We need to remove the install script from "package.json",
+             ;; as it would try to use node-pre-gyp and would block the
+             ;; automatic building performed by `npm install`.
+             (with-atomic-file-replacement "package.json"
+               (lambda (in out)
+                 (let* ((js (read-json in))
+                        (alist (match js
+                                 (('@ . alist) alist)))
+                        (scripts-alist (match (assoc-ref alist "scripts")
+                                         (('@ . alist) alist)))
+                        (scripts-alist
+                         ;; install script would use node-pre-gyp
+                         (assoc-remove! scripts-alist "install"))
+                        (alist
+                         (assoc-set! alist "scripts" (cons '@ scripts-alist)))
+                        (binary-alist (match (assoc-ref alist "binary")
+                                        (('@ . alist) alist)))
+                        (js (cons '@ alist)))
+                   ;; When it builds from source, node-pre-gyp supplies
+                   ;; module_name and module_path based on the entries under
+                   ;; "binary" from "package.json", so this package's
+                   ;; "binding.gyp" doesn't define them. Thus, we also need
+                   ;; to supply them. The GYP_DEFINES environment variable
+                   ;; turns out to be the easiest way to make sure they are
+                   ;; propagated from npm to node-gyp to gyp.
+                   (setenv "GYP_DEFINES"
+                           (string-append
+                            "module_name="
+                            (assoc-ref binary-alist "module_name")
+                            " "
+                            "module_path="
+                            (assoc-ref binary-alist "module_path")))
+                   (write-json js
+                               out)))))))))
+    (home-page "https://github.com/mapbox/node-sqlite3")
+    (synopsis "Asynchronous, non-blocking SQLite3 bindings for Node.js")
+    (description
+     "The Node.js add-on @code{node-sqlite3} provides a set of a asynchronous,
+non-blocking bindings for SQLite3, written in modern C++ and tested for memory
+leaks.")
+     (license license:bsd-3)))