diff mbox series

[bug#51838,v3,04/43] gnu: node: Add an npmrc file to set nodedir.

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

Commit Message

Philip McGrath Dec. 8, 2021, 8:27 p.m. UTC
* gnu/packages/node.scm (node, node-lts)[arguments]: Add a phase
'install-npmrc to create a "built-in" npmrc file that configures
"nodedir" to point to the output store path.
(libnode)[arguments]: Delete the 'install-npmrc phase.
---
 gnu/packages/node.scm | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Comments

Pierre Langlois Dec. 12, 2021, 3:19 p.m. UTC | #1
Philip McGrath <philip@philipmcgrath.com> writes:

> * gnu/packages/node.scm (node, node-lts)[arguments]: Add a phase
> 'install-npmrc to create a "built-in" npmrc file that configures
> "nodedir" to point to the output store path.
> (libnode)[arguments]: Delete the 'install-npmrc phase.
> ---
>  gnu/packages/node.scm | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/gnu/packages/node.scm b/gnu/packages/node.scm
> index a57a74fb81..0f67fe79c2 100644
> --- a/gnu/packages/node.scm
> +++ b/gnu/packages/node.scm
> @@ -250,7 +250,21 @@ (define-public node
>                  (find-files (string-append prefix "/lib/node_modules")
>                              (lambda (file stat)
>                                (executable-file? file))
> -                            #:stat lstat))))))))
> +                            #:stat lstat)))))
> +         (add-after 'install 'install-npmrc
> +           (lambda* (#:key outputs #:allow-other-keys)
> +             (let* ((out (assoc-ref outputs "out")))
> +               (with-output-to-file
> +                   ;; Use the config file "primarily for distribution
> +                   ;; maintainers" rather than "{prefix}/etc/npmrc",
> +                   ;; especially because node-build-system uses --prefix
> +                   ;; to install things to their store paths:
> +                   (string-append out "/lib/node_modules/npm/npmrc")
> +                 (lambda ()
> +                   ;; Tell npm (mostly node-gyp) where to find our
> +                   ;; installed headers so it doesn't try to
> +                   ;; download them from the internet:
> +                   (format #t "nodedir=~a\n" out)))))))))

When I run `node-gyp configure' in my tree-sitter packages, it's not
able to find the node dir and tries to download headers. So I need to
set `npm_config_nodedir' manually, like so:

--8<---------------cut here---------------start------------->8---
(add-after 'configure 'configure-gyp
  (lambda* (#:key inputs #:allow-other-keys)
    (let ((node (assoc-ref inputs "node")))
      (setenv "npm_config_nodedir" node)
      (invoke
        (string-append
          node
          "/lib/node_modules/npm/bin/node-gyp-bin/node-gyp")
        "configure"))))
--8<---------------cut here---------------end--------------->8---

I'm wondering if the npmrc approach here is working properly? I see in
another patch you've used the `npx' command to run `node-gyp rebuild',
should I be using that?

Thanks,
Pierre
Philip McGrath Dec. 12, 2021, 8:19 p.m. UTC | #2
Hi!

On 12/12/21 10:19, Pierre Langlois wrote:
> When I run `node-gyp configure' in my tree-sitter packages, it's not
> able to find the node dir and tries to download headers. So I need to
> set `npm_config_nodedir' manually, like so:

> I'm wondering if the npmrc approach here is working properly? I see in
> another patch you've used the `npx' command to run `node-gyp rebuild',
> should I be using that?

The npmrc configuration will only work of node-gyp is run through npm 
(or npx): https://github.com/nodejs/node-gyp#npm-configuration

In general, my current understanding is that you shouldn't have to run 
`node-gyp configure` explicitly. First, `node-gyp rebuild` includes a 
`node-gyp configure` step, but, more broadly, Guix's configure and build 
phases should usually do any needed addon building automatically. Aside 
from packages that deleted the configue phase, the main exception I've 
found is packages that try to use some strategy for downloading 
pre-built binaries: in some cases I've patched them enough to make 
automatic compilation work, but maybe sometimes just doing things 
manually is less work. If you do need to run `node-gyp` explicitly, the 
`npx` approach is the best I've found.

-Philip
Philip McGrath Dec. 13, 2021, 6 a.m. UTC | #3
Hi!

Here is v4 of the patch series: I've also put it up
at <https://gitlab.com/philip1/guix-patches/-/tags/guix-issue-51838-v4>.

  -Philip

Philip McGrath (45):
  gnu: node: Avoid duplicating build phases.
  gnu: node: Update to 10.24.1 for bootstrapping.
  gnu: node: Patch shebangs in node_modules.
  gnu: node: Add an npmrc file to set nodedir.
  guix: node-build-system: Add delete-lockfiles phase.
  guix: node-build-system: Refactor patch-dependencies phase.
  guix: node-build-system: Add #:absent-dependencies argument.
  gnu: node-semver-bootstrap: Use #:absent-dependencies.
  gnu: node-ms-bootstrap: Use #:absent-dependencies.
  gnu: node-binary-search-bootstrap: Use #:absent-dependencies.
  gnu: node-debug-bootstrap: Use #:absent-dependencies.
  gnu: node-llparse-builder-bootstrap: Use #:absent-dependencies.
  gnu: node-llparse-frontend-bootstrap: Use #:absent-dependencies.
  gnu: node-llparse-bootstrap: Use #:absent-dependencies.
  gnu: node-semver: Use #:absent-dependencies.
  gnu: node-wrappy: Use #:absent-dependencies.
  gnu: node-once: Use #:absent-dependencies.
  gnu: node-irc-colors: Use #:absent-dependencies.
  gnu: node-irc: Use #:absent-dependencies.
  guix: node-build-system: Add implicit libuv input.
  guix: node-build-system: Add avoid-node-gyp-rebuild phase.
  gnu: Add node-inherits.
  gnu: Add node-safe-buffer.
  gnu: Add node-string-decoder.
  gnu: Add node-readable-stream.
  gnu: Add node-nan.
  gnu: Add node-openzwave-shared.
  gnu: Add node-addon-api.
  gnu: Add node-sqlite3.
  gnu: Add node-file-uri-to-path.
  gnu: Add node-bindings.
  gnu: Add node-segfault-handler.
  gnu: Add node-ms.
  gnu: Add node-debug.
  gnu: Add node-serialport-binding-abstract.
  gnu: Add node-serialport-parser-delimiter.
  gnu: Add node-serialport-parser-readling.
  gnu: Add node-serialport-bindings.
  gnu: Add node-serialport-parser-regex.
  gnu: Add node-serialport-parser-ready.
  gnu: Add node-serialport-parser-inter-byte-timeout.
  gnu: Add node-serialport-parser-cctalk.
  gnu: Add node-serialport-parser-byte-length.
  gnu: Add node-serialport-stream.
  gnu: Add node-serialport.

 gnu/packages/node-xyz.scm        | 932 ++++++++++++++++++++++++++++++-
 gnu/packages/node.scm            | 222 ++++----
 gnu/packages/zwave.scm           |  69 +++
 guix/build-system/node.scm       |  28 +-
 guix/build/node-build-system.scm | 129 ++++-
 5 files changed, 1225 insertions(+), 155 deletions(-)
diff mbox series

Patch

diff --git a/gnu/packages/node.scm b/gnu/packages/node.scm
index a57a74fb81..0f67fe79c2 100644
--- a/gnu/packages/node.scm
+++ b/gnu/packages/node.scm
@@ -250,7 +250,21 @@  (define-public node
                 (find-files (string-append prefix "/lib/node_modules")
                             (lambda (file stat)
                               (executable-file? file))
-                            #:stat lstat))))))))
+                            #:stat lstat)))))
+         (add-after 'install 'install-npmrc
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((out (assoc-ref outputs "out")))
+               (with-output-to-file
+                   ;; Use the config file "primarily for distribution
+                   ;; maintainers" rather than "{prefix}/etc/npmrc",
+                   ;; especially because node-build-system uses --prefix
+                   ;; to install things to their store paths:
+                   (string-append out "/lib/node_modules/npm/npmrc")
+                 (lambda ()
+                   ;; Tell npm (mostly node-gyp) where to find our
+                   ;; installed headers so it doesn't try to
+                   ;; download them from the internet:
+                   (format #t "nodedir=~a\n" out)))))))))
     (native-inputs
      `(;; Runtime dependencies for binaries used as a bootstrap.
        ("c-ares" ,c-ares)
@@ -818,4 +832,5 @@  (define-public libnode
         `(cons* "--shared" "--without-npm" ,flags))
        ((#:phases phases '%standard-phases)
         `(modify-phases ,phases
+           (delete 'install-npmrc)
            (delete 'patch-nested-shebangs)))))))