Subject: Re: [bug#29951] [PATCH] WIP guix: Add wrap-script.

From: Ricardo Wurmus <[email-masked]>
Date: Thu, 02 Aug 2018 09:23:16 +0200

Hi Chris,

> Ricardo Wurmus <[email-masked]> writes:
>
>> This is the result in “/tmp/test-python”:
>>
>> #!/home/rekado/.guix-profile/bin/guile --no-auto-compile
>> #!#; Guix wrapper
>> #\-(begin (let ((current (getenv "PYTHONPATH"))) (setenv "PYTHONPATH" (if current (string-append "/foo/bar:whatever" ":" current) "/foo/bar:whatever"))) (let ((current (getenv "FOOBAR"))) (setenv "FOOBAR" (if current (string-append current ":" "/to/me") "/to/me"))))
>> #\-(apply execl "/gnu/store/iyy9w0hcxv4dg9q92d4g023vvz50r5bq-python-3.5.3/bin/python3.5" (car (command-line)) (command-line))
>> #!/gnu/store/iyy9w0hcxv4dg9q92d4g023vvz50r5bq-python-3.5.3/bin/python3.5
>> import sys
>> from lib2to3.main import main
>>
>> sys.exit(main("lib2to3.fixes"))
>
> I understand that the part beginning with #! and ending with !# are a
> block comment in scheme, so they will be ignored by Guile, and that
> "Guix wrapper" shows up after a ;, so it will also be considered a
> comment by Guile, but I don't understand why the lines following that
> need to begin with #\-.  I also don't understand why Guile doesn't
> complain about it.  Why do the lines begin with #\-?

#\- is Guile syntax for the character “-”.  To ensure that the target
language ignores these lines they must start with “#”.  In Guile,
however, we cannot just use “#” on its own.  The “#” in Guile is the
start of a reader macro.  We don’t really want a reader macro at the
beginning of each line, though – we just want to move on and get to the
expression.

So we use “#\-”, which evaluates to the character “-”.  It is valid in
Scheme to write code that evaluates to something and then ignore that
value.  Here’s an example:

   10
   23
   #\a
   #\b
   5
   (display "hello")

Guile would evaluate the numbers and characters one after the other, and
none of them would have any effect on the final expression that displays
“hello”.

In the wrapper we do the same kind of thing.  This line:

#\-(begin (let …) …)

is really just two Scheme values: a single arbitrary character and the
S-expression that we care about.  We only care about the side-effects of
evaluating the S-expression.

A language that uses “#” to mark comments, on the other hand, would
simply ignore the whole line.  The result is that we can use Guile to
set environment variables and then hand over execution to the target
language interpreter, which would run in the new environment.

The advantage of this approach is: we don’t have to rename wrapped
scripts any more (“.foo-real”), which makes for prettier usage messages
(“Usage: foo -v” instead of “Usage: .foo-real -v”) and avoids problems
when an application checks its own name to determine what actions it
should take.

--
Ricardo