From patchwork Thu May 14 09:18:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Konrad Hinsen X-Patchwork-Id: 22067 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id A8FE627BBE3; Thu, 14 May 2020 10:19:09 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM,MAILING_LIST_MULTI,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id B331127BBE1 for ; Thu, 14 May 2020 10:19:08 +0100 (BST) Received: from localhost ([::1]:44652 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jZA1E-0006Dj-9R for patchwork@mira.cbaines.net; Thu, 14 May 2020 05:19:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jZA19-0006Db-3e for guix-patches@gnu.org; Thu, 14 May 2020 05:19:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:48697) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jZA18-0003cf-Qo for guix-patches@gnu.org; Thu, 14 May 2020 05:19:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jZA18-0007tC-Kz for guix-patches@gnu.org; Thu, 14 May 2020 05:19:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#41253] [PATCH] guix: add script execution to "guix repl" Resent-From: Konrad Hinsen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 14 May 2020 09:19:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 41253 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 41253@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.158944793130298 (code B ref -1); Thu, 14 May 2020 09:19:02 +0000 Received: (at submit) by debbugs.gnu.org; 14 May 2020 09:18:51 +0000 Received: from localhost ([127.0.0.1]:60243 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jZA0x-0007sc-08 for submit@debbugs.gnu.org; Thu, 14 May 2020 05:18:51 -0400 Received: from lists.gnu.org ([209.51.188.17]:43720) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jZA0v-0007sV-Uk for submit@debbugs.gnu.org; Thu, 14 May 2020 05:18:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58146) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jZA0v-0006C2-OR for guix-patches@gnu.org; Thu, 14 May 2020 05:18:49 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:41081) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jZA0u-0003Y3-HL for guix-patches@gnu.org; Thu, 14 May 2020 05:18:49 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 19C435C0152; Thu, 14 May 2020 05:18:46 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 14 May 2020 05:18:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.net; h= from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=fm3; bh=7bN7BFmcPgx7J4JntQjkZv5iBB uMhySYS7Cws3cE32A=; b=pDJa3lLmoSPwf+G0joeidShvZUHUGBKobWDcnny1j/ q6Ycz8ZD6R4dJkFxHgtzCKK5aRjJJdQiaZW3Ag4k7KZpEp+BZ4FkYkkHHF/cS917 RSu40nN7AIdjzqlOZ4C9aKtf9U03QEsJ5pcM5ni5F2OxtFuqvMYRLRCtS5CV8oNx 3a293NbneYnU9qL7pHY0L/4ytvf9c1TSmzntgSzc986nqWfsjuXClgCWk+tXNm/X Vl324Un0+Wgd7JBPPmrolNS8Mg1ktOspZDMIgjvpn9pecI5T/KMkl+Sh2VOIUlCQ erIRXFg1yvzZbm4SOPOb/otfNA4YL7DXtw+MYFBMkq5Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=7bN7BF mcPgx7J4JntQjkZv5iBBuMhySYS7Cws3cE32A=; b=GUI+M3v/7xzQhZay2i0RBG GqiNdGZ7VMYGcS8eeOaga37BgSmDwuX440JfHCQY9fz20OfK8yCb9DYLT8KDWjCm VwR1iAkPzVz9BHA2bh1Zvhq4Rg4zjfMLZn9ivnQPNQSytpY2poJkGYqTg6n2CV2Y GXjK1AEZysL62Ye5wsthOYNOG824sCVDXLWUEmcdXKMIVRLEyJFngMT5iNzHk4Ip +3BRNIjWKb3XsbJC2sWLxD3vi0cqq4k3Mnl58kYCkEnQun4j6cwDb2MtFZWhYgFA cL551NqYRVmCwBryVcyeRDW+02rZYyGF2emYKZVlI9TlKmFogbqRM134DKvFvo0A == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrleeigdduvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkfggtgfgsehtqhertddttd ejnecuhfhrohhmpefmohhnrhgrugcujfhinhhsvghnuceokhhonhhrrggurdhhihhnshgv nhesfhgrshhtmhgrihhlrdhnvghtqeenucggtffrrghtthgvrhhnpefghfeuffehhfeuge fhvdeugfekfeegudeltdehveehkeekfefftdehgeejjedufeenucfkphepledvrdduieel rddujeefrddvvdehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomhepkhhonhhrrggurdhhihhnshgvnhesfhgrshhtmhgrihhlrdhnvght X-ME-Proxy: Received: from khs-macbook.home (lfbn-idf2-1-1364-225.w92-169.abo.wanadoo.fr [92.169.173.225]) by mail.messagingengine.com (Postfix) with ESMTPA id 6E8AD3060BB5; Thu, 14 May 2020 05:18:45 -0400 (EDT) From: Konrad Hinsen Date: Thu, 14 May 2020 11:18:43 +0200 Message-ID: MIME-Version: 1.0 Received-SPF: pass client-ip=66.111.4.25; envelope-from=konrad.hinsen@fastmail.net; helo=out1-smtp.messagingengine.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/14 05:18:46 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/repl.scm: Add filename options for script execution. * doc/guix.texi: Document script execution by "guix repl" --- doc/guix.texi | 38 +++++++++++++++----- guix/scripts/repl.scm | 80 ++++++++++++++++++++++++++----------------- 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index d6fbd85fde..68f1a8bba3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -238,7 +238,7 @@ Programming Interface * Derivations:: Low-level interface to package derivations. * The Store Monad:: Purely functional interface to the store. * G-Expressions:: Manipulating build expressions. -* Invoking guix repl:: Fiddling with Guix interactively. +* Invoking guix repl:: Programming Guix in Guile Defining Packages @@ -5415,7 +5415,7 @@ package definitions. * Derivations:: Low-level interface to package derivations. * The Store Monad:: Purely functional interface to the store. * G-Expressions:: Manipulating build expressions. -* Invoking guix repl:: Fiddling with Guix interactively. +* Invoking guix repl:: Programming Guix in Guile @end menu @node Package Modules @@ -8134,12 +8134,31 @@ has an associated gexp compiler, such as a @code{}. @node Invoking guix repl @section Invoking @command{guix repl} -@cindex REPL, read-eval-print loop -The @command{guix repl} command spawns a Guile @dfn{read-eval-print loop} -(REPL) for interactive programming (@pxref{Using Guile Interactively,,, guile, -GNU Guile Reference Manual}). Compared to just launching the @command{guile} +@cindex REPL, read-eval-print loop, script +The @command{guix repl} command makes it easier to program Guix in Guile +by launching a Guile @dfn{read-eval-print loop} (REPL) for interactive +programming (@pxref{Using Guile Interactively,,, guile, +GNU Guile Reference Manual}), or by running Guile scripts +(@pxref{Running Guile Scripts,,, guile, +GNU Guile Reference Manual}). +Compared to just launching the @command{guile} command, @command{guix repl} guarantees that all the Guix modules and all its -dependencies are available in the search path. You can use it this way: +dependencies are available in the search path. + +The general syntax is: + +@example +guix repl @var{options} @var{files} +@end example + +When at least one @var{files} argument is provided, @var{files} are +executed as Guile scripts in the given order: + +@example +$ guix repl my-script.scm +@end example + +Otherwise a Guile REPL is started: @example $ guix repl @@ -8188,11 +8207,12 @@ Add @var{directory} to the front of the package module search path (@pxref{Package Modules}). This allows users to define their own packages and make them visible to -the command-line tool. +the scripts or REPL. @item -q Inhibit loading of the @file{~/.guile} file. By default, that -configuration file is loaded when spawning a @code{guile} REPL. +configuration file is loaded when executing scripts or spawning +a @code{guile} REPL. @end table @c ********************************************************************* diff --git a/guix/scripts/repl.scm b/guix/scripts/repl.scm index ff1f208894..f4cb744bbd 100644 --- a/guix/scripts/repl.scm +++ b/guix/scripts/repl.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès ;;; Copyright © 2020 Simon Tournier +;;; Copyright © 2020 Konrad Hinsen ;;; ;;; This file is part of GNU Guix. ;;; @@ -32,10 +33,12 @@ ;;; Commentary: ;;; -;;; This command provides a Guile REPL +;;; This command provides a Guile script runner and REPL in an environment +;;; that contains all the modules comprising Guix. (define %default-options - `((type . guile))) + `((scripts . ()) + (type . guile))) (define %options (list (option '(#\h "help") #f #f @@ -63,8 +66,9 @@ (define (show-help) - (display (G_ "Usage: guix repl [OPTIONS...] -Start a Guile REPL in the Guix execution environment.\n")) + (display (G_ "Usage: guix repl [OPTIONS...] [FILES...] +In the Guix execution environment, run FILES as Guile scripts, +or start a Guile REPL if no FILES are given,\n")) (display (G_ " -t, --type=TYPE start a REPL of the given TYPE")) (display (G_ " @@ -135,12 +139,13 @@ call THUNK." (define (guix-repl . args) (define opts - ;; Return the list of package names. (args-fold* args %options (lambda (opt name arg result) (leave (G_ "~A: unrecognized option~%") name)) (lambda (arg result) - (leave (G_ "~A: extraneous argument~%") arg)) + (alist-cons 'scripts + (cons arg (cdr (assq 'scripts result))) + result)) %default-options)) (define user-config @@ -148,29 +153,42 @@ call THUNK." (lambda (home) (string-append home "/.guile")))) + (define (set-user-module) + (set-current-module user-module) + (when (and (not (assoc-ref opts 'ignore-dot-guile?)) + user-config + (file-exists? user-config)) + (load user-config))) + (with-error-handling - (let ((type (assoc-ref opts 'type))) - (call-with-connection (assoc-ref opts 'listen) - (lambda () - (case type - ((guile) - (save-module-excursion - (lambda () - (set-current-module user-module) - (when (and (not (assoc-ref opts 'ignore-dot-guile?)) - user-config - (file-exists? user-config)) - (load user-config)) - - ;; Do not exit repl on SIGINT. - ((@@ (ice-9 top-repl) call-with-sigint) - (lambda () - (start-repl)))))) - ((machine) - (machine-repl)) - (else - (leave (G_ "~a: unknown type of REPL~%") type)))))))) - -;; Local Variables: -;; eval: (put 'call-with-connection 'scheme-indent-function 1) -;; End: + (let ((scripts (reverse (assoc-ref opts 'scripts)))) + + (for-each (lambda (script) + (save-module-excursion + (lambda () + (set-user-module) + (load script)))) + scripts) + + (when (null? scripts) + (let ((type (assoc-ref opts 'type))) + (call-with-connection (assoc-ref opts 'listen) + (lambda () + (case type + ((guile) + (save-module-excursion + (lambda () + (set-user-module) + ;; Do not exit repl on SIGINT. + ((@@ (ice-9 top-repl) call-with-sigint) + (lambda () + (start-repl)))))) + ((machine) + (machine-repl)) + (else + (leave (G_ "~a: unknown type of REPL~%") type))))))))) + + ;; Local Variables: + ;; eval: (put 'call-with-connection 'scheme-indent-function 1) + ;; End: + )