diff mbox series

[bug#65229,2/2] ui: 'load*' accepts /dev/fd/N files pointing to a pipe.

Message ID a00d4d602028777a6beaf823336a7b901480fcf4.1691767568.git.ludo@gnu.org
State New
Headers show
Series Add '-q' for 'pull' and 'time-machine'; support loading from pipes | expand

Commit Message

Ludovic Courtès Aug. 11, 2023, 3:34 p.m. UTC
From: Ludovic Courtès <ludovic.courtes@inria.fr>

This allows users to write Bash commands like:

  guix time-machine -C <(echo %default-channels) -- ...

or:

  guix build -m <(echo '(specifications->manifest (list "guile"))')

Previously, on GNU/Linux, they would fail with:

  error: failed to load '/dev/fd/63': No such file or directory

* guix/ui.scm (try-canonicalize-path): New procedure.
(load*): Use it.
* tests/guix-build.sh: Test 'guix build -m' with a /dev/fd/N file.
---
 guix/ui.scm         | 16 +++++++++++++++-
 tests/guix-build.sh |  9 ++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

Comments

Simon Tournier Aug. 16, 2023, 1:32 p.m. UTC | #1
Hi Ludo,

Cool!

On Fri, 11 Aug 2023 at 17:34, Ludovic Courtès <ludo@gnu.org> wrote:

> This allows users to write Bash commands like:
>
>   guix time-machine -C <(echo %default-channels) -- ...
>
> or:
>
>   guix build -m <(echo '(specifications->manifest (list "guile"))')

I propose to document this.  WDYT about this?

--8<---------------cut here---------------start------------->8---
1 file changed, 6 insertions(+)
doc/guix.texi | 6 ++++++

modified   doc/guix.texi
@@ -5103,6 +5103,12 @@ Invoking guix time-machine
 @item -q
 Inhibit loading of the user and system channel files,
 @file{~/.config/guix/channels.scm} and @file{/etc/guix/channels.scm}.
+
+This option is equivalent to the command run on Bash shell:
+
+@example
+guix time-machine -C <(echo %default-channels) -- build hello
+@end example
 @end table
 
 As for @command{guix pull}, in the absence of any options,
--8<---------------cut here---------------end--------------->8---


Cheers,
simon
diff mbox series

Patch

diff --git a/guix/ui.scm b/guix/ui.scm
index 47a118364a..6f2d4fe245 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -200,6 +200,20 @@  (define-syntax-rule (without-compiler-optimizations exp)
   (parameterize (((@ (system base compile) default-optimization-level) 1))
     exp))
 
+(define (try-canonicalize-path file)
+  "Like 'canonicalize-path', but return FILE as-is if 'canonicalize-path'
+throws.
+
+This is necessary for corner cases where 'canonicalize-path' fails.  One
+example is on Linux when a /dev/fd/N file denotes a pipe, represented as a
+symlink to a non-existent file like 'pipe:[1234]', as in this example:
+
+  sh -c 'stat $(readlink -f /dev/fd/1)' | cat"
+  (catch 'system-error
+    (lambda ()
+      (canonicalize-path file))
+    (const file)))
+
 (define* (load* file user-module
                 #:key (on-error 'nothing-special))
   "Load the user provided Scheme source code FILE."
@@ -230,7 +244,7 @@  (define* (load* file user-module
                ;; 'primitive-load', so that FILE is compiled, which then allows
                ;; us to provide better error reporting with source line numbers.
                (without-compiler-optimizations
-                (load (canonicalize-path file))))
+                (load (try-canonicalize-path file))))
              (const #f))))))
     (lambda _
       ;; XXX: Errors are reported from the pre-unwind handler below, but
diff --git a/tests/guix-build.sh b/tests/guix-build.sh
index 317c58ac42..4eab0e38b6 100644
--- a/tests/guix-build.sh
+++ b/tests/guix-build.sh
@@ -1,5 +1,5 @@ 
 # GNU Guix --- Functional package management for GNU
-# Copyright © 2012-2014, 2016-2022 Ludovic Courtès <ludo@gnu.org>
+# Copyright © 2012-2014, 2016-2023 Ludovic Courtès <ludo@gnu.org>
 # Copyright © 2020 Marius Bakke <mbakke@fastmail.com>
 # Copyright © 2021 Chris Marusich <cmmarusich@gmail.com>
 #
@@ -397,6 +397,13 @@  guix build -d -m "$module_dir/manifest.scm" \
 
 rm "$module_dir"/*.scm
 
+if [ -n "$BASH_VERSION" ]
+then
+    # Check whether we can load from a /dev/fd/N denoting a pipe, using this
+    # handy Bash-specific construct.
+    guix build -m <(echo '(specifications->manifest (list "guile"))') -n
+fi
+
 # Using 'GUIX_BUILD_OPTIONS'.
 GUIX_BUILD_OPTIONS="--dry-run --no-grafts"
 export GUIX_BUILD_OPTIONS