]> git.ekhem.eu.org Git - guix.git/commitdiff
[home] Simplify and clarify npm-install-build-system
authorJakub Czajka <jakub@ekhem.eu.org>
Thu, 9 Jul 2026 16:21:29 +0000 (18:21 +0200)
committerJakub Czajka <jakub@ekhem.eu.org>
Thu, 9 Jul 2026 16:21:29 +0000 (18:21 +0200)
Rename functions for clarity, drop coreutils dependency, merge
helpers into the build system as an inline lambda.  Reduce four
top-level definitions to two.

Co-Authored-By: Claude <noreply@anthropic.com>
conf/home/npm.scm

index b1227dfcf490928d8b038f297e4fde72c1f462b1..f6e3027696c574dc4cf307b5db9dda534c9320ff 100644 (file)
   #:use-module (guix build-system trivial)
   #:use-module (guix utils)
   #:use-module (gnu packages bash)
-  #:use-module (gnu packages base)
   #:use-module (gnu packages node)
   #:export (npm-install-build-system))
 
-(define (npm-prebuilt-drv bin-name npm-package version hash)
-  "Return a computed-file that npm-installs NPM-PACKAGE at VERSION."
-  (computed-file (string-append bin-name "-built-" version)
+(define (npm-install-directory name npm-package version hash)
+  "Return a computed-file containing node_modules/ for NPM-PACKAGE
+at VERSION.  This is a fixed-output derivation: npm install runs
+with network access, pinned by HASH."
+  (computed-file (string-append name "-built-" version)
                  #~(begin
                      (use-modules (guix build utils))
                      (setenv "HOME" "/tmp")
@@ -40,7 +41,6 @@
                             (bash #$(file-append bash-minimal "/bin/bash"))
                             (build-dir "/tmp/npm-build")
                             (bin-dir "/tmp/bin"))
-                       ;; Phase 1: npm install (fixed-output).
                        (mkdir-p build-dir)
                        (with-output-to-file (string-append build-dir
                                                            "/package.json")
                            (format #t "{\"dependencies\": {\"~a\": \"~a\"}}~%"
                                    #$npm-package
                                    #$version)))
-                       ;; sh symlink for lifecycle scripts.
                        (mkdir-p bin-dir)
                        (symlink bash
                                 (string-append bin-dir "/sh"))
-                       ;; PATH must include node, bash, npm itself,
-                       ;; and the sh symlink so lifecycle scripts
-                       ;; that run before npm's bin/ is populated
-                       ;; can find a shell.
                        (setenv "PATH"
-                               (string-append (dirname (dirname npm-cli))
-                                              "/bin:"
-                                              (dirname node)
+                               (string-append (dirname node)
                                               ":"
                                               (dirname bash)
                                               ":"
                              #:leaked-env-vars ("http_proxy" "https_proxy")
                              #:modules ((guix build utils)))))
 
-(define (npm-build-gexp bin-name entry hash npm-package version)
-  "Return a builder gexp for an npm-install-build-system package."
-  (let ((built (npm-prebuilt-drv bin-name npm-package version hash))
-        (mkdir (file-append coreutils "/bin/mkdir"))
-        (chmod (file-append coreutils "/bin/chmod")))
-    #~(begin
-        ;; Phase 2: generate shell wrapper script.
-        (let* ((out (getenv "out"))
-               (bindir (string-append out "/bin"))
-               (script (string-append bindir "/"
-                                      #$bin-name)))
-          (system (string-append #$mkdir " -p " bindir))
-          (with-output-to-file script
-            (lambda ()
-              (format #t "#!~a~%"
-                      #$(file-append bash-minimal "/bin/bash"))
-              (format #t "exec ~a ~a \"$@\"~%"
-                      #$(file-append node-lts "/bin/node")
-                      (string-append #$built "/node_modules/"
-                                     #$entry))))
-          (system (string-append #$chmod " +x " script))))))
-
-(define* (lower-npm-install name
-                            #:key npm-package
-                            version
-                            entry
-                            hash
-                            (bin-name name)
-                            (wrapper-name name)
-                            source
-                            target
-                            system
-                            inputs
-                            native-inputs
-                            outputs
-                            #:allow-other-keys #:rest arguments)
-  "Lower an npm-install-build-system package to a bag."
-  (let ((builder (npm-build-gexp wrapper-name entry hash npm-package version)))
-    ((build-system-lower trivial-build-system)
-     name
-     #:source source
-     #:target target
-     #:system system
-     #:inputs inputs
-     #:native-inputs `(("coreutils" ,coreutils)
-                       ,@native-inputs)
-     #:outputs outputs
-     #:builder builder)))
-
 (define npm-install-build-system
   (build-system (name 'npm-install)
                 (description
                  "npm install in a fixed-output derivation, wrap with a Node.js
 shell script")
-                (lower lower-npm-install)))
+                (lower (lambda* (name #:key npm-package
+                                      version
+                                      entry
+                                      hash
+                                      (wrapper-name name)
+                                      source
+                                      target
+                                      system
+                                      inputs
+                                      native-inputs
+                                      outputs
+                                      #:allow-other-keys)
+                         "Lower an npm-install-build-system package to a bag."
+                         (let ((built (npm-install-directory wrapper-name
+                                                             npm-package
+                                                             version hash)))
+                           ((build-system-lower trivial-build-system)
+                            name
+                            #:source source
+                            #:target target
+                            #:system system
+                            #:inputs inputs
+                            #:native-inputs native-inputs
+                            #:outputs outputs
+                            #:modules '((guix build utils))
+                            #:builder #~(begin
+                                          (use-modules (guix build utils))
+                                          (let ((script (string-append #$output
+                                                         "/bin/"
+                                                         #$wrapper-name)))
+                                            (mkdir-p (dirname script))
+                                            (with-output-to-file script
+                                              (lambda ()
+                                                (format #t
+                                                 "#!~a~%exec ~a ~a \"$@\"~%"
+                                                 #$(file-append bash-minimal
+                                                                "/bin/bash")
+                                                 #$(file-append node-lts
+                                                                "/bin/node")
+                                                 (string-append #$built
+                                                  "/node_modules/"
+                                                  #$entry))))
+                                            (chmod script #o755)))))))))