Subject: this-package-input for multiple outputs package

From: Simon Tournier <[email-masked]>
Date: Wed, 24 Jul 2024 02:41:39 +0200

Hi,

For the good or the bad, we have package with multiple outputs.  For
instance, see ’git’ with ’git:send-email’ and others.

However, it’s not always easy to work with them using the “new style”.

For a concrete example: <https://issues.guix.gnu.org/66704#4>.

A minimal example.

--8<---------------cut here---------------start------------->8---
$ cat /tmp/pkgs/pkgs.scm
(define-module (pkgs)
  #:use-module (guix packages)
  #:use-module (gnu packages base)
  #:use-module (gnu packages version-control))

(define-public salut
  (package
    (inherit hello)
    (name "bye")
    (inputs
     (list git
           `(,git "send-email")))))
--8<---------------cut here---------------end--------------->8---

Then, it’s possible to add phases, e.g.,

--8<---------------cut here---------------start------------->8---
                (wrap-program (string-append #$output "/bin/hello")
                  `("STUFF" ":" prefix
                    (,(string-append #$(this-package-input "git") "/bin")))
--8<---------------cut here---------------end--------------->8---

All fine!

However, from my understanding, it does not seem possible to access
using this “new style” way to other outputs than “out”.  Because, the
inputs reads,

--8<---------------cut here---------------start------------->8---
$ guix repl -q -L /tmp/pkgs
GNU Guile 3.0.9
Copyright (C) 1995-2023 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guix-user)> ,use(guix packages)
scheme@(guix-user)> ,use(pkgs)
scheme@(guix-user)> ,pp (package-inputs salut)
$1 = (("git"
  #<package [email-masked] gnu/packages/version-control.scm:243 73a06a652580>)
 ("git"
  #<package [email-masked] gnu/packages/version-control.scm:243 73a06a652580>
  "send-email"))
--8<---------------cut here---------------end--------------->8---

Other said, both outputs have the same label – here “git”.

Then, the procedure ’this-package-input’ – under the hood via
lookup-package-input and then lookup-input from (guix packages) – calls:

    (assoc-ref (package-inputs some-package) some-string)

Therefore, since “some-string” is the same here, bang!

A first proposal for fixing the issue.

It’s easy to tweak how ’sanitize-inputs’ works.  For instance using this
patch:

--8<---------------cut here---------------start------------->8---
1 file changed, 1 insertion(+), 1 deletion(-)
guix/packages.scm | 2 +-

modified   guix/packages.scm
@@ -667,7 +667,7 @@ (define (add-input-label input)
     ((? package? package)
      (list (package-name package) package))
     (((? package? package) output)                ;XXX: ugly?
-     (list (package-name package) package output))
+     (list (string-append (package-name package) ":" output) package output))
     ((? gexp-input?)       ;XXX: misplaced because 'native?' field is ignored?
      (let ((obj    (gexp-input-thing input))
            (output (gexp-input-output input)))
--8<---------------cut here---------------end--------------->8---

Now we get different labels,

--8<---------------cut here---------------start------------->8---
scheme@(guix-user)> ,pp (package-inputs salut)
$1 = (("git"
  #<package [email-masked] gnu/packages/version-control.scm:243 72f31b35de70>)
 ("git:send-email"
  #<package [email-masked] gnu/packages/version-control.scm:243 72f31b35de70>
  "send-email"))
--8<---------------cut here---------------end--------------->8---

So far, so good!  Please note that some packages then need some
adjustment; e.g., see below.

Next, let add to the package definition this snippet for more wrapping,

--8<---------------cut here---------------start------------->8---
                    (,(string-append #$(this-package-input "git:send-email") "/bin")))
--8<---------------cut here---------------end--------------->8---

Because of the procedure ’lookup-input’ from (guix packages), the
package is correctly identified but the output is still discarded.
Hence this modification:

--8<---------------cut here---------------start------------->8---
1 file changed, 1 insertion(+)
guix/packages.scm | 1 +

modified   guix/packages.scm
@@ -1213,6 +1213,7 @@ (define (lookup-input inputs name)
   ;; check package names.
   (match (assoc-ref inputs name)
     ((obj) obj)
+    ((obj (? string? out)) (cons obj out))
     ((obj _) obj)
     (#f #f)))
 --8<---------------cut here---------------end--------------->8---

But then it is still incorrect.  For instance, we check using
’package-arguments’ and the interesting part reads:

--8<---------------cut here---------------start------------->8---
(unquote (string-append #<gexp-input (#<package [email-masked] gnu/packages/version-control.scm:243 7b7573de1b00> . "send-email"):out> "/bin"))
--8<---------------cut here---------------end--------------->8---

Ok, this is incorrect and it will error when trying to build.  Well, the
G-exp compiler needs to be updated in agreement with ’lookup-input’ as
above.  What I would expect is something like:

    #<gexp-input #<package [email-masked] gnu/packages/version-control.scm:243 7b7573de1b00>:send-email>

All in all, I am a bit lost inside the module (guix gexp). :-)  To be continued…

Cheers,
simon

Some adjustments here or there:

--8<---------------cut here---------------start------------->8---
1 file changed, 1 insertion(+), 1 deletion(-)
gnu/packages/backup.scm | 2 +-

modified   gnu/packages/backup.scm
@@ -318,7 +318,7 @@ (define-public libarchive
                      (libxml2 (assoc-ref inputs "libxml2"))
                      (xz      (assoc-ref inputs "xz"))
                      (zlib    (assoc-ref inputs "zlib"))
-                     (zstd    (assoc-ref inputs "zstd"))
+                     (zstd    (assoc-ref inputs "zstd:lib"))
                      (bzip2   (assoc-ref inputs "bzip2")))
                 ;; Embed absolute references to these inputs to avoid propagation.
                 (substitute* (list (string-append lib "/pkgconfig/libarchive.pc")
--8<---------------cut here---------------end--------------->8---

And thus improving the situation for packages with multiple outputs and
“new style” probably means a world rebuild.