Subject: `this-package' references reintroduce pre-transformation packages.

From: Ulf Herrman <[email-masked]>
Date: Thu, 12 Oct 2023 22:57:14 -0500

Suppose you have a package that is using a gexp in its argument list
and, like a good citizen of the gexp world, it uses this-package-input
to refer to its own input packages.  In fact, let's suppose that it's
the model citizen depicted in
https://guix.gnu.org/en/blog/2021/the-big-change/ under the
"G-expressions and self-referential records" heading:

(define hello
  (package
    (name "hello")
    ;; …
    (arguments
     (list #:configure-flags
           #~(list (string-append "--with-gawk="
                                  #$(this-package-input "gawk")))))
    (inputs `(("gawk" ,gawk)))))

If we define a variant like so:

(define hello-variant
  (package
    (inherit hello)
    (name "hello-variant")
    (inputs `(("gawk" ,gawk-4.0)))))

it will work just fine.  But if we define a variant like SO:

(define hello-variant
  (package
    (inherit hello)
    (name "hello-variant")
    (inputs `(("gawk" ,gawk-4.0)))
    (arguments
     (substitute-keyword-arguments (package-arguments hello)
       ((#:configure-flags flags #~'())
        #~(cons "--with-hospitality=ice-cream"
                #$flags))))))

it will NOT work just fine.  When (package-arguments hello) is
evaluated, it will execute the field definition for `hello' with
`this-package' bound to `hello', rather than `hello-variant'.
Consequently, `this-package-input' will return gawk rather than
gawk-4.0.  We need a way to access the "parent" package's fields while
keeping `this-package' bound to its current value.  The most general
form of this would look something like this:

(define (package-arguments-with-package p0 p)
  (match p0
    (($ <package> _ _ _ _ arguments-proc)
     (arguments-proc p))))

Then hello-variant could be changed to use
(package-arguments-with-package hello this-package)
instead of (package-arguments hello).  This may be needlessly general,
though; the problem could also be solved with an interface more along
the lines of

(parent-package-arguments hello)

which expands into the aforementioned package-arguments-with-package
call.

Another option would be to, yet again, extend the record syntax.  It's
always bugged me a bit to have to explicitly reference the original
record in more than one place when using derived fields, so this might
be generally useful as well:

(define hello-variant
  (package
    (inherit hello
     (arguments hello-arguments))
    (name "hello-variant")
    (inputs `(("gawk" ,gawk-4.0)))
    (arguments
     (substitute-keyword-arguments hello-arguments
       ((#:configure-flags flags #~'())
        #~(cons "--with-hospitality=ice-cream"
                #$flags))))))

This would create a macro named `hello-arguments' within the scope of
the (package ...) form which expands into something equivalent to a
`parent-package-arguments' call.  Adjust syntax to taste.

Thoughts?

- Ulf