From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Thompson Subject: Re: [PATCH] scripts: Add 'environment' command. Date: Fri, 10 Oct 2014 14:09:09 -0400 Message-ID: <87fvevdcfu.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me> References: <87oatmch5i.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me> <87siix59cs.fsf@gnu.org> <87r3yg1wpp.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me> <871tqgdt3o.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me> <87y4sn9900.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34833) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xced2-0007M6-8p for guix-devel@gnu.org; Fri, 10 Oct 2014 14:09:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xcecw-0007qv-1r for guix-devel@gnu.org; Fri, 10 Oct 2014 14:09:24 -0400 Received: from na6sys009bog037.obsmtp.com ([74.125.150.111]:46589) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xcecv-0007qe-SQ for guix-devel@gnu.org; Fri, 10 Oct 2014 14:09:18 -0400 Received: by mail-qc0-f176.google.com with SMTP id r5so2343756qcx.21 for ; Fri, 10 Oct 2014 11:09:16 -0700 (PDT) In-Reply-To: <87y4sn9900.fsf@gnu.org> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org To: Ludovic =?utf-8?Q?Court=C3=A8s?= Cc: guix-devel@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Ludovic Court=C3=A8s writes: > David Thompson skribis: > >> From 26cac530c7a1f69864fc819bdc14553eacd9dc23 Mon Sep 17 00:00:00 2001 >> From: David Thompson >> Date: Sun, 21 Sep 2014 13:40:05 -0400 >> Subject: [PATCH] scripts: Add 'environment' command. >> >> * guix/scripts/environment.scm: New file. >> * Makefile.am (MODULES): Add it. >> * doc/guix.texi ("Invoking guix environment"): New node. > > Nice! > > I didn=E2=80=99t mention it before but it would be wonderful if you could= come > up with a little test case. It=E2=80=99s a bit difficult, esp. since the= tests > cannot assume that will download & build the world. So basically the > test could do, in a tests/guix-environment.sh script: > > guix environment -e '(@@ (gnu packages bootstrap) %bootstrap-guile)' ... > > and make sure it at least sees =E2=80=98guile=E2=80=99 in $PATH. > > WDYT? That can come in a later commit if you prefer. > Hmm, it's actually a bit trickier than that because the bootstrap guile has no inputs, so the resulting purified environment is completely empty. In the meantime, the changes to the docs made sense, so here's an updated patch with those changes. --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-scripts-Add-environment-command.patch Content-Transfer-Encoding: quoted-printable >From bdcebf9db0c93f4cabc388acf20720fdcae90037 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 21 Sep 2014 13:40:05 -0400 Subject: [PATCH] scripts: Add 'environment' command. * guix/scripts/environment.scm: New file. * Makefile.am (MODULES): Add it. * doc/guix.texi ("Invoking guix environment"): New node. --- Makefile.am | 1 + doc/guix.texi | 82 +++++++++++++++ guix/scripts/environment.scm | 236 +++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 319 insertions(+) create mode 100644 guix/scripts/environment.scm diff --git a/Makefile.am b/Makefile.am index 7eecef2..4b823ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -94,6 +94,7 @@ MODULES =3D \ guix/scripts/system.scm \ guix/scripts/lint.scm \ guix/scripts/import/nix.scm \ + guix/scripts/environment.scm \ guix.scm \ $(GNU_SYSTEM_MODULES) =20 diff --git a/doc/guix.texi b/doc/guix.texi index 52b9d60..9a4a6b6 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -33,6 +33,12 @@ Documentation License''. Managing the operating system configuration. @end direntry =20 +@dircategory Software development +@direntry +* guix environment: (guix)Invoking guix environment + Building development environments with Guix. +@end direntry + @titlepage @title GNU Guix Reference Manual @subtitle Using the GNU Guix Functional Package Manager @@ -2431,6 +2437,7 @@ programming interface of Guix in a convenient way. * Invoking guix hash:: Computing the cryptographic hash of a file. * Invoking guix refresh:: Updating package definitions. * Invoking guix lint:: Finding errors in package definitions. +* Invoking guix environment:: Setting up development environments. @end menu =20 @node Invoking guix build @@ -2840,6 +2847,81 @@ and exit. =20 @end table =20 +@node Invoking guix environment +@section Invoking @command{guix environment} + +The purpose of @command{guix environment} is to assist hackers in +creating reproducible development environments without polluting their +package profile. The @command{guix environment} tool takes one or more +packages, builds all of the necessary inputs, and creates a shell +environment to use them. + +The general syntax is: + +@example +guix environment @var{options} @var{package}@dots{} +@end example + +The following examples spawns a new shell that is capable of building +the GNU Guile source code: + +@example +guix environment guile +@end example + +If the specified packages are not built yet, @command{guix environment} +automatically builds them. The new shell's environment is an augmented +version of the environment that @command{guix environment} was run in. +It contains the necessary search paths for building the given package +added to the existing environment variables. To create a ``pure'' +environment in which the original environment variables have been unset, +use the @code{--pure} option. + +Additionally, more than one package may be specified, in which case the +union of the inputs for the given packages are used. For example, the +command below spawns a shell where all of the dependencies of both Guile +and Emacs are available: + +@example +guix environment guile emacs +@end example + +Sometimes an interactive shell session is not desired. The +@code{--exec} option can be used to specify the command to run instead. + +@example +guix environment guile --exec=3Dmake +@end example + +The following options are available: + +@table @code +@item --expression=3D@var{expr} +@itemx -E @var{expr} +Create an environment for the package that @var{expr} evaluates to. + +@item --load=3D@var{file} +@itemx -l @var{file} +Create an environment for the package that the code within @var{file} +evaluates to. + +@item --exec=3D@var{command} +@item -e @var{command} +Execute @var{command} in the new environment. + +@item --pure +Unset existing environment variables when building the new environment. +This has the effect of creating an environment in which search paths +only contain package inputs. + +@item --search-paths +Display the environment variable definitions that make up the +environment. +@end table + +It also supports all of the common build options that @command{guix +build} supports (@pxref{Invoking guix build, common build options}). + @c ********************************************************************* @node GNU Distribution @chapter GNU Distribution diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm new file mode 100644 index 0000000..f86231b --- /dev/null +++ b/guix/scripts/environment.scm @@ -0,0 +1,236 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright =C2=A9 2014 David Thompson +;;; +;;; 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 . + +(define-module (guix scripts environment) + #:use-module (guix ui) + #:use-module (guix store) + #:use-module (guix derivations) + #:use-module (guix packages) + #:use-module (guix profiles) + #:use-module (guix utils) + #:use-module (guix monads) + #:use-module (guix build utils) + #:use-module (guix build-system gnu) + #:use-module (guix scripts build) + #:use-module (gnu packages) + #:use-module (ice-9 format) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (srfi srfi-37) + #:use-module (srfi srfi-98) + #:export (guix-environment)) + +(define (for-each-search-path proc inputs derivations pure?) + "Apply PROC for each native search path in INPUTS in addition to 'PATH'. +Use the output paths of DERIVATIONS to build each search path. When PURE?= is +#t, the existing search path value is ignored. Otherwise, the existing se= arch +path value is appended." + (let ((paths (map derivation->output-path derivations))) + (for-each (match-lambda + (($ + variable directories separator) + (let* ((current (getenv variable)) + (path ((@@ (guix build utils) search-path-as-list) + directories paths)) + (value (list->search-path-as-string path separator)= )) + (proc variable + (if (and current (not pure?)) + (string-append value separator current) + value))))) + (cons* (search-path-specification + (variable "PATH") + (directories '("bin" "sbin")) + (separator ":")) + (delete-duplicates + (append-map package-native-search-paths inputs)))))) + +;; Protect some env vars from purification. Borrowed from nix-shell. +(define %precious-variables + '("HOME" "USER" "LOGNAME" "DISPLAY" "TERM" "TZ" "PAGER")) + +(define (purify-environment) + "Unset almost all environment variables. A small number of variables su= ch +as 'HOME' and 'USER' are left untouched." + (for-each unsetenv + (remove (cut member <> %precious-variables) + (match (get-environment-variables) + (((names . _) ...) + names))))) + +(define (create-environment inputs derivations pure?) + "Set the needed environment variables for all packages within INPUTS. W= hen +PURE? is #t, unset the variables in the current environment. Otherwise, +augment existing enviroment variables with additional search paths." + (when pure? (purify-environment)) + (for-each-search-path setenv inputs derivations pure?)) + +(define (show-search-paths inputs derivations pure?) + "Display the needed search paths to build an environment that contains t= he +packages within INPUTS. When PURE? is #t, do not augment existing environ= ment +variables with additional search paths." + (for-each-search-path (lambda (variable value) + (format #t "export ~a=3D\"~a\"~%" variable value= )) + inputs derivations pure?)) + +(define (show-help) + (display (_ "Usage: guix environment [OPTION]... PACKAGE... +Build an environment that includes the dependencies of PACKAGE and execute= a +shell command in that environment.\n")) + (display (_ " + -e, --expression=3DEXPR create environment for the package that EXPR + evaluates to")) + (display (_ " + -l, --load=3DFILE create environment for the package that the cod= e within + FILE evaluates to")) + (display (_ " + -E, --exec=3DCOMMAND execute COMMAND in new environment")) + (display (_ " + --pure unset existing environment variables")) + (display (_ " + --search-paths display needed environment variable definitions")) + (newline) + (show-build-options-help) + (newline) + (display (_ " + -h, --help display this help and exit")) + (display (_ " + -V, --version display version information and exit")) + (show-bug-report-information)) + +(define %default-options + ;; Default to opening a new shell. + `((exec . ,(or (getenv "SHELL") "/bin/sh")) + (substitutes? . #t) + (max-silent-time . 3600) + (verbosity . 0))) + +(define %options + ;; Specification of the command-line options. + (cons* (option '(#\h "help") #f #f + (lambda args + (show-help) + (exit 0))) + (option '(#\V "version") #f #f + (lambda args + (show-version-and-exit "guix environment"))) + (option '("pure") #f #f + (lambda (opt name arg result) + (alist-cons 'pure #t result))) + (option '(#\E "exec") #t #f + (lambda (opt name arg result) + (alist-cons 'exec arg result))) + (option '("search-paths") #f #f + (lambda (opt name arg result) + (alist-cons 'search-paths #t result))) + (option '(#\l "load") #t #f + (lambda (opt name arg result) + (alist-cons 'load arg result))) + (option '(#\e "expression") #t #f + (lambda (opt name arg result) + (alist-cons 'expression arg result))) + (option '(#\n "dry-run") #f #f + (lambda (opt name arg result) + (alist-cons 'dry-run? #t result))) + %standard-build-options)) + +(define (pick-all alist key) + "Return a list of values in ALIST associated with KEY." + (define same-key? (cut eq? key <>)) + + (fold (lambda (pair memo) + (match pair + (((? same-key? k) . v) + (cons v memo)) + (_ memo))) + '() alist)) + +(define (options/resolve-packages opts) + "Return OPTS with package specification strings replaced by actual +packages." + (map (match-lambda + (('package . (? string? spec)) + `(package . ,(specification->package spec))) + (('expression . str) + (match (read/eval str) + ((? package? p) + `(package . ,p)))) + (('load . file) + `(package . ,(load (string-append (getcwd) "/" file)))) + (opt opt)) + opts)) + +(define (packages->transitive-inputs packages) + "Return a list of the transitive inputs for all PACKAGES." + (define (transitive-inputs package) + (filter-map (match-lambda + ((_ (? package? package)) package) + (_ #f)) + (bag-transitive-inputs + (package->bag package)))) + (delete-duplicates + (append-map transitive-inputs packages))) + +;; TODO: Deduplicate these. +(define show-what-to-build* + (store-lift show-what-to-build)) + +(define set-build-options-from-command-line* + (store-lift set-build-options-from-command-line)) + +(define (build-inputs inputs opts) + "Build the packages in INPUTS using the build options in OPTS." + (let ((substitutes? (assoc-ref opts 'substitutes?)) + (dry-run? (assoc-ref opts 'dry-run?))) + (mlet* %store-monad ((drvs (sequence %store-monad + (map package->derivation inputs))= )) + (mbegin %store-monad + (show-what-to-build* drvs + #:use-substitutes? substitutes? + #:dry-run? dry-run?) + (if dry-run? + (return #f) + (mbegin %store-monad + (set-build-options-from-command-line* opts) + (built-derivations drvs) + (return drvs))))))) + +;; Entry point. +(define (guix-environment . args) + (define (parse-options) + (args-fold* args %options + (lambda (opt name arg result) + (leave (_ "~A: unrecognized option~%") name)) + (lambda (arg result) + (alist-cons 'package arg result)) + %default-options)) + + (with-store store + (let* ((opts (parse-options)) + (pure? (assoc-ref opts 'pure)) + (command (assoc-ref opts 'exec)) + (inputs (packages->transitive-inputs + (pick-all (options/resolve-packages opts) 'package))) + (drvs (run-with-store store (build-inputs inputs opts)))) + (cond ((assoc-ref opts 'dry-run?) + #t) + ((assoc-ref opts 'search-paths) + (show-search-paths inputs drvs pure?)) + (else + (create-environment inputs drvs pure?) + (system command)))))) --=20 2.1.0 --=-=-= Content-Type: text/plain -- David Thompson Web Developer - Free Software Foundation - http://fsf.org GPG Key: 0FF1D807 Support the FSF: https://fsf.org/donate --=-=-=--