From patchwork Thu Mar 31 11:09:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 38248 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 A965F27BBEA; Thu, 31 Mar 2022 12:16:13 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id B5DD627BBE9 for ; Thu, 31 Mar 2022 12:16:12 +0100 (BST) Received: from localhost ([::1]:45142 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nZsmh-0007uK-Ug for patchwork@mira.cbaines.net; Thu, 31 Mar 2022 07:16:11 -0400 Received: from eggs.gnu.org ([209.51.188.92]:37182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZshj-0001gS-DO for guix-patches@gnu.org; Thu, 31 Mar 2022 07:11:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:44131) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nZshj-0003Um-31 for guix-patches@gnu.org; Thu, 31 Mar 2022 07:11:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nZshi-0002Dm-TT for guix-patches@gnu.org; Thu, 31 Mar 2022 07:11:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54393] [PATCH v2 3/3] shell: Add '--export-manifest'. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 31 Mar 2022 11:11:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54393 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54393@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54393-submit@debbugs.gnu.org id=B54393.16487250518436 (code B ref 54393); Thu, 31 Mar 2022 11:11:02 +0000 Received: (at 54393) by debbugs.gnu.org; 31 Mar 2022 11:10:51 +0000 Received: from localhost ([127.0.0.1]:38024 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nZshS-0002Bn-1d for submit@debbugs.gnu.org; Thu, 31 Mar 2022 07:10:50 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48990) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nZsh9-00029V-Oz for 54393@debbugs.gnu.org; Thu, 31 Mar 2022 07:10:32 -0400 Received: from [2001:470:142:3::e] (port=54758 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZsh2-0003B8-7Y; Thu, 31 Mar 2022 07:10:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=zMtQau/T4yno5/RcDHSy26kiWvX4+EEuHFFVjRyOOf4=; b=i6W7WBHwCkff/+6MrOro s92bLs84nfKUc6XE4zlDtXkw4EygEpVYYiUD2efE11BqVAqWWyvayyuNgjq4JWQz91RiysogndZSF cwpSkK68uNkCOZ2xKtNx+8v56VfqwZOBwzIZFZfTaeM3TOvUo5oIWuBEjU7A9dDWIBwi9QVcPZTWx fHGbMo/KHHuSNFJq95wYQr/B17UveUU+tWV9zOD387dytlOdUS+OqDAux4QhidtqlahgHk58kNx2e JF0iNiKtVCvjjcUnQWNUnSaVIw6NqGt7IMAjmntSMnjiWoBQKmSOpiAlFjOrjfBLSMIWnsQ9Z2+8n HNGcu9gehOz+zA==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:61384 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nZsh1-0002dj-8d; Thu, 31 Mar 2022 07:10:20 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Thu, 31 Mar 2022 13:09:57 +0200 Message-Id: <20220331110957.31829-3-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220331110957.31829-1-ludo@gnu.org> References: <87r173huph.fsf@gnu.org> <20220331110957.31829-1-ludo@gnu.org> MIME-Version: 1.0 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/shell.scm (show-help, %options): Add '--export-manifest'. (manifest-entry-version-prefix, manifest->code*) (export-manifest): New procedures. (guix-shell): Honor '--export-manifest'. * tests/guix-shell-export-manifest.sh: New file. * Makefile.am (SH_TESTS): Add it. * doc/guix.texi (Invoking guix shell): Document '--export-manifest'. (Invoking guix environment): Link to it. (Invoking guix pack): Likewise. --- Makefile.am | 1 + doc/guix.texi | 57 +++++++++++++++ guix/scripts/shell.scm | 109 +++++++++++++++++++++++++++- tests/guix-shell-export-manifest.sh | 84 +++++++++++++++++++++ 4 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 tests/guix-shell-export-manifest.sh diff --git a/Makefile.am b/Makefile.am index 91243f2cad..20ab7dd5f0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -572,6 +572,7 @@ SH_TESTS = \ tests/guix-environment.sh \ tests/guix-environment-container.sh \ tests/guix-shell.sh \ + tests/guix-shell-export-manifest.sh \ tests/guix-graph.sh \ tests/guix-describe.sh \ tests/guix-repl.sh \ diff --git a/doc/guix.texi b/doc/guix.texi index e8ef4286be..85a91019be 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -5848,6 +5848,55 @@ This is similar to the same-named option in @command{guix package} (@pxref{profile-manifest, @option{--manifest}}) and uses the same manifest files. +See @option{--export-manifest} below on how to obtain a first manifest. + +@cindex manifest, exporting +@anchor{shell-export-manifest} +@item --export-manifest +Write to standard output a manifest suitable for @option{--manifest} +corresponding to given command-line options. + +This is a way to ``convert'' command-line arguments into a manifest. +For example, imagine you are tired of typing long lines and would like +to get a manifest equivalent to this command line: + +@example +guix shell -D guile git emacs emacs-geiser emacs-geiser-guile +@end example + +Just add @option{--export-manifest} to the command line above: + +@example +guix shell --export-manifest \ + -D guile git emacs emacs-geiser emacs-geiser-guile +@end example + +@noindent +... and you get a manifest along these lines: + +@lisp +(concatenate-manifests + (list (specifications->manifest + (list "git" + "emacs" + "emacs-geiser" + "emacs-geiser-guile")) + (package->development-manifest + (specification->package "guile")))) +@end lisp + +You can store it into a file, say @file{manifest.scm}, and from there +pass it to @command{guix shell} or indeed pretty much any @command{guix} +command: + +@example +guix shell -m manifest.scm +@end example + +Voilà, you've converted a long command line into a manifest! That +conversion process honors package transformation options (@pxref{Package +Transformation Options}) so it should be lossless. + @item --profile=@var{profile} @itemx -p @var{profile} Create an environment containing the packages installed in @var{profile}. @@ -6235,6 +6284,10 @@ This is similar to the same-named option in @command{guix package} (@pxref{profile-manifest, @option{--manifest}}) and uses the same manifest files. +@xref{shell-export-manifest, @command{guix shell --export-manifest}}, +for information on how to ``convert'' command-line options into a +manifest. + @item --ad-hoc Include all specified packages in the resulting environment, as if an @i{ad hoc} package were defined with them as inputs. This option is @@ -6693,6 +6746,10 @@ for use on machines that do not have Guix installed. Note that you can specify @emph{either} a manifest file @emph{or} a list of packages, but not both. +@xref{shell-export-manifest, @command{guix shell --export-manifest}}, +for information on how to ``convert'' command-line options into a +manifest. + @item --system=@var{system} @itemx -s @var{system} Attempt to build for @var{system}---e.g., @code{i686-linux}---instead of diff --git a/guix/scripts/shell.scm b/guix/scripts/shell.scm index 1eab05d737..5e7f454f4c 100644 --- a/guix/scripts/shell.scm +++ b/guix/scripts/shell.scm @@ -21,7 +21,8 @@ (define-module (guix scripts shell) #:use-module ((guix diagnostics) #:select (location)) #:use-module (guix scripts environment) #:autoload (guix scripts build) (show-build-options-help) - #:autoload (guix transformations) (transformation-option-key? + #:autoload (guix transformations) (options->transformation + transformation-option-key? show-transformation-options-help) #:use-module (guix scripts) #:use-module (guix packages) @@ -41,7 +42,12 @@ (define-module (guix scripts shell) #:use-module ((guix build utils) #:select (mkdir-p)) #:use-module (guix cache) #:use-module ((ice-9 ftw) #:select (scandir)) - #:autoload (gnu packages) (cache-is-authoritative?) + #:autoload (ice-9 pretty-print) (pretty-print) + #:autoload (gnu packages) (cache-is-authoritative? + package-unique-version-prefix + specification->package + specification->package+output + specifications->manifest) #:export (guix-shell)) (define (show-help) @@ -55,10 +61,13 @@ (define (show-help) -D, --development include the development inputs of the next package")) (display (G_ " -f, --file=FILE add to the environment the package FILE evaluates to")) + (display (G_ " -q inhibit loading of 'guix.scm' and 'manifest.scm'")) (display (G_ " --rebuild-cache rebuild cached environment, if any")) + (display (G_ " + --export-manifest print a manifest for the given options")) (show-environment-options-help) (newline) @@ -112,6 +121,10 @@ (define %options ;; 'wrapped-option'. (alist-delete 'ad-hoc? result))) + (option '("export-manifest") #f #f + (lambda (opt name arg result) + (alist-cons 'export-manifest? #t result))) + ;; For consistency with 'guix package', support '-f' rather than ;; '-l' like 'guix environment' does. (option '(#\f "file") #t #f @@ -380,6 +393,94 @@ (define (key->file key) (loop rest system file specs)) ((_ . rest) (loop rest system file specs))))) + +;;; +;;; Exporting a manifest. +;;; + +(define (manifest-entry-version-prefix entry) + "Search among all the versions of ENTRY's package that are available, and +return the shortest unambiguous version prefix for this package." + (package-unique-version-prefix (manifest-entry-name entry) + (manifest-entry-version entry))) + +(define (manifest->code* manifest extra-manifests) + "Like 'manifest->code', but insert a 'concatenate-manifests' call that +concatenates MANIFESTS, a list of expressions." + (if (null? (manifest-entries manifest)) + (match extra-manifests + ((one) one) + (lst `(concatenate-manifests ,@extra-manifests))) + (match (manifest->code manifest + #:entry-package-version + manifest-entry-version-prefix) + (('begin exp ... last) + `(begin + ,@exp + ,(match extra-manifests + (() last) + (_ `(concatenate-manifests + (list ,last ,@extra-manifests))))))))) + +(define (export-manifest opts port) + "Write to PORT a manifest corresponding to OPTS." + (define (manifest-lift proc) + (lambda (entry) + (match (manifest-entry-item entry) + ((? package? p) + (manifest-entry + (inherit (package->manifest-entry (proc p))) + (output (manifest-entry-output entry)))) + (_ + entry)))) + + (define (validated-spec spec) + ;; Return SPEC if it's validate package spec. + (specification->package+output spec) + spec) + + (let* ((transform (options->transformation opts)) + (specs (reverse + (filter-map (match-lambda + (('package 'ad-hoc-package spec) + (validated-spec spec)) + (_ #f)) + opts))) + (extras (reverse + (filter-map (match-lambda + (('package 'package spec) + ;; Make sure SPEC is valid. + (specification->package spec) + + ;; XXX: This is an approximation: + ;; transformation options are not applied. + `(package->development-manifest + (specification->package ,spec))) + (_ #f)) + opts))) + (manifest (concatenate-manifests + (cons (map-manifest-entries + (manifest-lift transform) + (specifications->manifest specs)) + (filter-map (match-lambda + (('manifest . file) + (load-manifest file)) + (_ #f)) + opts))))) + (display (G_ "\ +;; What follows is a \"manifest\" equivalent to the command line you gave. +;; You can store it in a file that you may then pass to any 'guix' command +;; that accepts a '--manifest' (or '-m') option.\n") + port) + (match (manifest->code* manifest extras) + (('begin exp ...) + (for-each (lambda (exp) + (newline port) + (pretty-print exp port)) + exp)) + (exp + (pretty-print exp port))))) + ;;; ;;; One-time hints. @@ -445,4 +546,6 @@ (define interactive? cache-entries #:entry-expiration entry-expiration))) - (guix-environment* opts)) + (if (assoc-ref opts 'export-manifest?) + (export-manifest opts (current-output-port)) + (guix-environment* opts))) diff --git a/tests/guix-shell-export-manifest.sh b/tests/guix-shell-export-manifest.sh new file mode 100644 index 0000000000..cbb90f04bf --- /dev/null +++ b/tests/guix-shell-export-manifest.sh @@ -0,0 +1,84 @@ +# GNU Guix --- Functional package management for GNU +# Copyright © 2022 Ludovic Courtès +# +# This file is part of GNU Guix. +# +# GNU Guix is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or (at +# your option) any later version. +# +# GNU Guix is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Guix. If not, see . + +# +# Test 'guix shell --export-manifest'. +# + +guix shell --version + +tmpdir="t-guix-manifest-$$" +trap 'rm -r "$tmpdir"' EXIT +mkdir "$tmpdir" + +manifest="$tmpdir/manifest.scm" + +# Basics. +guix shell --export-manifest guile-bootstrap > "$manifest" +test "$(guix build -m "$manifest")" = "$(guix build guile-bootstrap)" + +guix shell -m "$manifest" --bootstrap -- \ + "$SHELL" -c 'guix package --export-manifest -p "$GUIX_ENVIRONMENT"' > \ + "$manifest.second" +for m in "$manifest" "$manifest.second" +do + grep -v '^;' < "$m" > "$m.new" # filter out comments + mv "$m.new" "$m" +done + +cat "$manifest" +cat "$manifest.second" + +cmp "$manifest" "$manifest.second" + +# Combining manifests. +guix shell --export-manifest -m "$manifest" gash gash-utils \ + > "$manifest.second" +guix build -m "$manifest.second" -d | \ + grep "$(guix build guile-bootstrap -d)" +guix build -m "$manifest.second" -d | \ + grep "$(guix build gash -d)" + +# Package transformation option. +guix shell --export-manifest guile guix --with-latest=guile-json > "$manifest" +grep 'options->transformation' "$manifest" +grep '(with-latest . "guile-json")' "$manifest" + +# Development manifest. +guix shell --export-manifest -D guile git > "$manifest" +grep 'package->development-manifest' "$manifest" +grep '"guile"' "$manifest" +guix build -m "$manifest" -d | \ + grep "$(guix build -e '(@@ (gnu packages commencement) gcc-final)' -d)" +guix build -m "$manifest" -d | \ + grep "$(guix build git -d)" + +# Test various combinations to make sure generated code uses interfaces +# correctly. +for options in \ + "coreutils grep sed" \ + "gsl openblas gcc-toolchain --tune" \ + "guile -m $manifest.previous" \ + "git:send-email gdb guile:debug" \ + "git -D coreutils" +do + guix shell --export-manifest $options > "$manifest" + cat "$manifest" + guix shell -m "$manifest" -n + mv "$manifest" "$manifest.previous" +done