unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] scripts: Add 'environment' command.
@ 2014-10-08 22:48 David Thompson
  2014-10-09  6:44 ` Alex Kost
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: David Thompson @ 2014-10-08 22:48 UTC (permalink / raw)
  To: guix-devel

[-- Attachment #1: Type: text/plain, Size: 3911 bytes --]

Hello Guix hackers,

Below is a preliminary version of a new guix command called
'environment'.  For those familiar with Nix, it works a lot like
nix-shell.

The purpose of 'guix environment' is to assist hackers in creating
reproducible development environments without polluting their package
profile.  'guix environment' takes a package (or packages), builds all
of the necessary inputs, and creates a shell environment to use them.
For example, 'guix environment guile' adds these environment variables:

  export PATH="/gnu/store/hzq21w0m7akpc3b9gxq9p0vlr74k8pib-pkg-config-0.27.1/bin:/gnu/store/3yiqz9wmwx6b7hpbapg5q39sjx33kh0j-bash-4.3.27/bin:/gnu/store/26vfhzfgl2iqfwa92ij8zqssgc4amh9y-gcc-4.8.3/bin:/gnu/store/2x8wq5w4cxgmz0f982gz37wzdglj0865-ld-wrapper-boot3-0/bin:/gnu/store/4cbvfyx31xyh7pmmvv3w5h0689pcnlnw-glibc-2.20/bin:/gnu/store/4cbvfyx31xyh7pmmvv3w5h0689pcnlnw-glibc-2.20/sbin:/gnu/store/dlg2zv7w3d3bfj5qg7yhskvgj65080p2-binutils-cross-boot0-2.24/bin:/gnu/store/pcizz33yzwyiirfsinbg08v3aha6krx4-make-boot0-4.0/bin:/gnu/store/qgx70g83pab3b08ff6cl0rrzv33bf074-diffutils-3.3/bin:/gnu/store/37w1i5q2iqm0alymc1iiibs12nv87wap-findutils-4.4.2/bin:/gnu/store/1qza3p5n6w5ciphli8wm05jam72jczfd-file-5.19/bin:/gnu/store/jfavabhnk1am05pyivk7ryixznqa49l4-binutils-bootstrap-0/bin:/gnu/store/mbacj3k992756m
 xskp26pd8b476mynhk-bootstrap-binaries-0/bin:/gnu/store/52vg2sr63d5vpz4np3sa1vf5y5gyhnrf-readline-6.3/bin:/gnu/store/z8ifyrrgawhmd5w3v0fdjdv52cwhb0pd-ncurses-5.9/bin"
  export PKG_CONFIG_PATH="/gnu/store/8f9db6s6j77w130y9b13wsnsbalhfrf9-libffi-3.1/lib/pkgconfig:/gnu/store/8g6zcbj2fzzl6xis3mcsmpxrqsfsaxs4-libgc-7.4.2/lib/pkgconfig"
  export CPATH="/gnu/store/26vfhzfgl2iqfwa92ij8zqssgc4amh9y-gcc-4.8.3/include:/gnu/store/4cbvfyx31xyh7pmmvv3w5h0689pcnlnw-glibc-2.20/include:/gnu/store/rw5s9immq72plzajqm8g8i4sfg8z0kyc-linux-libre-headers-3.3.8/include:/gnu/store/1qza3p5n6w5ciphli8wm05jam72jczfd-file-5.19/include:/gnu/store/8f9db6s6j77w130y9b13wsnsbalhfrf9-libffi-3.1/include:/gnu/store/52vg2sr63d5vpz4np3sa1vf5y5gyhnrf-readline-6.3/include:/gnu/store/z8ifyrrgawhmd5w3v0fdjdv52cwhb0pd-ncurses-5.9/include:/gnu/store/3j7h3gz5qh1v0j1xjjb2xxr2b3kqcsv6-libunistring-0.9.4/include:/gnu/store/wz94w6nr5wsvy06inwfpc0pxqp1cpkly-libtool-2.4.2/include:/gnu/store/8g6zcbj2fzzl6xis3mcsmpxrqsfsaxs4-libgc-7.4.2/include:/gnu/store/l61k4nkiw8wc064ynf17l40sfgczyh56-gmp-6.0.0a/include"
  export LIBRARY_PATH="/gnu/store/4cbvfyx31xyh7pmmvv3w5h0689pcnlnw-glibc-2.20/lib:/gnu/store/1qza3p5n6w5ciphli8wm05jam72jczfd-file-5.19/lib:/gnu/store/8f9db6s6j77w130y9b13wsnsbalhfrf9-libffi-3.1/lib:/gnu/store/52vg2sr63d5vpz4np3sa1vf5y5gyhnrf-readline-6.3/lib:/gnu/store/z8ifyrrgawhmd5w3v0fdjdv52cwhb0pd-ncurses-5.9/lib:/gnu/store/3j7h3gz5qh1v0j1xjjb2xxr2b3kqcsv6-libunistring-0.9.4/lib:/gnu/store/wz94w6nr5wsvy06inwfpc0pxqp1cpkly-libtool-2.4.2/lib:/gnu/store/8g6zcbj2fzzl6xis3mcsmpxrqsfsaxs4-libgc-7.4.2/lib:/gnu/store/l61k4nkiw8wc064ynf17l40sfgczyh56-gmp-6.0.0a/lib"

With this environment, you can grab the guile source code and build it,
knowing that all the dependencies have been satisfied.  Furthermore,
this system makes it easy to work on projects that require different
versions of the same software without clashes.  Bye-bye RVM and
virtualenv!

By default, running 'guix environment' spawns a new $SHELL process,
because it is usually what one would want to do.  However, the '--exec'
flag can be used to specify the command to run.

Additionally, the default behavior is to prepend search paths to the
existing environment variable values so that one has access to other
installed software e.g. git.  To clear the existing environment firstf,
the '--pure' flag can be used.

Finally, the '--load' flag can be used to read a package from a file
instead of searching $GUIX_PACKAGE_PATH.

There is no documentation yet.  I wanted to get feedback first before
writing it in case substantial changes are made.  :)

WDYT?


[-- Attachment #2: 0001-scripts-Add-environment-command.patch --]
[-- Type: text/x-diff, Size: 9884 bytes --]

From 298dcc1dd3aac49e033debeea154c91b25229c14 Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
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.
---
 Makefile.am                  |   1 +
 guix/scripts/environment.scm | 212 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 213 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 =					\
   guix/scripts/system.scm			\
   guix/scripts/lint.scm				\
   guix/scripts/import/nix.scm			\
+  guix/scripts/environment.scm			\
   guix.scm					\
   $(GNU_SYSTEM_MODULES)
 
diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
new file mode 100644
index 0000000..a4aaa86
--- /dev/null
+++ b/guix/scripts/environment.scm
@@ -0,0 +1,212 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2014 David Thompson <davet@gnu.org>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+(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 gexp)
+  #:use-module (guix build utils)
+  #:use-module (guix build-system gnu)
+  #:use-module (guix scripts build)
+  #:use-module (gnu packages)
+  #:use-module (gnu packages commencement)
+  #:use-module (ice-9 format)
+  #:use-module (ice-9 match)
+  #:use-module (ice-9 pretty-print)
+  #: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 search
+path value is appended."
+  (let ((paths (map derivation->output-path derivations)))
+    (for-each (match-lambda
+               (($ <search-path-specification>
+                   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))))))
+
+(define (purify-environment)
+  "Unset almost all environment variables.  A small number of variables such
+as 'HOME' and 'USER' are left untouched."
+  (for-each unsetenv
+            (filter (lambda (variable)
+                      ;; Protect some env vars from purification.  Borrowed
+                      ;; from nix-shell.
+                      (not (member variable
+                                   '("HOME" "USER" "LOGNAME" "DISPLAY"
+                                     "TERM" "TZ" "PAGER"))))
+                    (map car (get-environment-variables)))))
+
+(define (create-environment inputs derivations pure?)
+  "Set the needed environment variables for all packages within INPUTS.  When
+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 the
+packages within INPUTS.  When PURE? is #t, do not augment existing environment
+variables with additional search paths."
+  (for-each-search-path (lambda (variable value)
+                          (format #t "export ~a=\"~a\"~%" variable value))
+                        inputs derivations pure?))
+
+(define (show-help)
+  (display (_ "Usage: guix environment --package=PACKAGE [OPTION]...
+Build an environment that includes the dependencies of PACKAGE.\n"))
+  (display (_ "
+  -p, --package=[PACKAGE]
+                         build environment for PACKAGE"))
+  (display (_ "
+  -e, --exec             shell command to execute"))
+  (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 . ,(getenv "SHELL"))
+    (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)))
+         %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 (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))))
+  (if (list? packages)
+      (delete-duplicates
+       (append-map transitive-inputs packages))
+      (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."
+  (with-store store
+    (run-with-store store
+      (mlet* %store-monad ((drvs (sequence %store-monad
+                                           (map package->derivation inputs))))
+        (mbegin %store-monad
+          (show-what-to-build* drvs
+                               #:use-substitutes? (assoc-ref opts 'substitutes?)
+                               #:dry-run? #f)
+          (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))
+
+  (let* ((opts (parse-options))
+         (pure? (assoc-ref opts 'pure))
+         (command (assoc-ref opts 'exec))
+         ;; Load from file if given, otherwise search for packages.
+         (inputs (packages->transitive-inputs
+                  (or (and=> (assoc-ref opts 'load) load)
+                      (map specification->package
+                           (pick-all opts 'package)))))
+         (drvs (build-inputs inputs opts)))
+    (cond ((assoc-ref opts 'search-paths)
+           (show-search-paths inputs drvs pure?))
+          (else
+           (create-environment inputs drvs pure?)
+           (system command)))))
-- 
2.1.0


[-- Attachment #3: Type: text/plain, Size: 136 bytes --]


-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-08 22:48 [PATCH] scripts: Add 'environment' command David Thompson
@ 2014-10-09  6:44 ` Alex Kost
  2014-10-09 16:50 ` Eric Bavier
  2014-10-09 19:30 ` Ludovic Courtès
  2 siblings, 0 replies; 17+ messages in thread
From: Alex Kost @ 2014-10-09  6:44 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson (2014-10-09 02:48 +0400) wrote:

> Hello Guix hackers,
>
> Below is a preliminary version of a new guix command called
> 'environment'.  For those familiar with Nix, it works a lot like
> nix-shell.
>
> The purpose of 'guix environment' is to assist hackers in creating
> reproducible development environments without polluting their package
> profile.  'guix environment' takes a package (or packages), builds all
> of the necessary inputs, and creates a shell environment to use them.
> For example, 'guix environment guile' adds these environment variables:

[...]

> With this environment, you can grab the guile source code and build it,
> knowing that all the dependencies have been satisfied.  Furthermore,
> this system makes it easy to work on projects that require different
> versions of the same software without clashes.  Bye-bye RVM and
> virtualenv!

[...]

My only comment is: This is GREAT!!  Thank you very much for working on
this.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-08 22:48 [PATCH] scripts: Add 'environment' command David Thompson
  2014-10-09  6:44 ` Alex Kost
@ 2014-10-09 16:50 ` Eric Bavier
  2014-10-09 16:54   ` Thompson, David
  2014-10-09 19:30 ` Ludovic Courtès
  2 siblings, 1 reply; 17+ messages in thread
From: Eric Bavier @ 2014-10-09 16:50 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson writes:

> Hello Guix hackers,
>
> Below is a preliminary version of a new guix command called
> 'environment'.  For those familiar with Nix, it works a lot like
> nix-shell.
[...]
> By default, running 'guix environment' spawns a new $SHELL process,
> because it is usually what one would want to do.  However, the '--exec'
> flag can be used to specify the command to run.

For this option, together with the --search-paths option, would it be
possible to customize the syntax of the output from the
show-search-paths function?  This might be a question for other guix
commands as well.  Either the current shell or the shell from --exec
might set variables in a different way.  E.g. the user is using the
"fish" shell, and doesn't want to spawn a new process, they should be
able to::

  eval (guix environment --search-paths guile)

Btw, fish uses 'set -x PATH "..."' to export PATH set to "..."

Just a thought.

> Finally, the '--load' flag can be used to read a package from a file
> instead of searching $GUIX_PACKAGE_PATH.

Could we instead have a -L or --load-path option like for `guix build`?
This would be useful if the user wants to give multiple packages to
`guix environment` that are all outside $GUIX_PACKAGE_PATH.

I'm very excited for this.

-- 
Eric Bavier

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-09 16:50 ` Eric Bavier
@ 2014-10-09 16:54   ` Thompson, David
  2014-10-09 17:26     ` Eric Bavier
  0 siblings, 1 reply; 17+ messages in thread
From: Thompson, David @ 2014-10-09 16:54 UTC (permalink / raw)
  To: Eric Bavier; +Cc: guix-devel

On Thu, Oct 9, 2014 at 12:50 PM, Eric Bavier <ericbavier@gmail.com> wrote:
> David Thompson writes:
>
>> By default, running 'guix environment' spawns a new $SHELL process,
>> because it is usually what one would want to do.  However, the '--exec'
>> flag can be used to specify the command to run.
>
> For this option, together with the --search-paths option, would it be
> possible to customize the syntax of the output from the
> show-search-paths function?  This might be a question for other guix
> commands as well.  Either the current shell or the shell from --exec
> might set variables in a different way.  E.g. the user is using the
> "fish" shell, and doesn't want to spawn a new process, they should be
> able to::
>
>   eval (guix environment --search-paths guile)
>
> Btw, fish uses 'set -x PATH "..."' to export PATH set to "..."
>
> Just a thought.
>

I have thought about this, too.  I think it would be a good future
improvement for all guix commands that output search paths.

>> Finally, the '--load' flag can be used to read a package from a file
>> instead of searching $GUIX_PACKAGE_PATH.
>
> Could we instead have a -L or --load-path option like for `guix build`?
> This would be useful if the user wants to give multiple packages to
> `guix environment` that are all outside $GUIX_PACKAGE_PATH.
>

The -L flag also works to add to the load path.  Lower-case 'l' calls
'load' on the given file and expects a package or list of packages in
return.  For this I envision having  a 'package.scm' or similar file
in my git repos so that I can run 'guix environment -l package.scm' to
create a development environment without altering load paths or having
'package.scm' define a module.

> I'm very excited for this.
>

Thanks for the feedback!

- Dave

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-09 16:54   ` Thompson, David
@ 2014-10-09 17:26     ` Eric Bavier
  0 siblings, 0 replies; 17+ messages in thread
From: Eric Bavier @ 2014-10-09 17:26 UTC (permalink / raw)
  To: Thompson, David; +Cc: guix-devel


Thompson, David writes:

> On Thu, Oct 9, 2014 at 12:50 PM, Eric Bavier <ericbavier@gmail.com> wrote:
>> David Thompson writes:
>>
>>> Finally, the '--load' flag can be used to read a package from a file
>>> instead of searching $GUIX_PACKAGE_PATH.
>>
>> Could we instead have a -L or --load-path option like for `guix build`?
>> This would be useful if the user wants to give multiple packages to
>> `guix environment` that are all outside $GUIX_PACKAGE_PATH.
>>
>
> The -L flag also works to add to the load path.

I see now that it plugs into (guix scripts build)'s option handling.
Thanks for pointing that out.

> Lower-case 'l' calls 'load' on the given file and expects a package or
> list of packages in return.  For this I envision having a
> 'package.scm' or similar file in my git repos so that I can run 'guix
> environment -l package.scm' to create a development environment
> without altering load paths or having 'package.scm' define a module.

Yes, and then a new importer that reads package.scm to populate the
inputs field ;)

-- 
Eric Bavier

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-08 22:48 [PATCH] scripts: Add 'environment' command David Thompson
  2014-10-09  6:44 ` Alex Kost
  2014-10-09 16:50 ` Eric Bavier
@ 2014-10-09 19:30 ` Ludovic Courtès
  2014-10-10  2:32   ` David Thompson
  2 siblings, 1 reply; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-09 19:30 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

Hey!

David Thompson <dthompson2@worcester.edu> skribis:

> The purpose of 'guix environment' is to assist hackers in creating
> reproducible development environments without polluting their package
> profile.  'guix environment' takes a package (or packages), builds all
> of the necessary inputs, and creates a shell environment to use them.

Awesome!

[...]

> By default, running 'guix environment' spawns a new $SHELL process,
> because it is usually what one would want to do.  However, the '--exec'
> flag can be used to specify the command to run.
>
> Additionally, the default behavior is to prepend search paths to the
> existing environment variable values so that one has access to other
> installed software e.g. git.  To clear the existing environment firstf,
> the '--pure' flag can be used.
>
> Finally, the '--load' flag can be used to read a package from a file
> instead of searching $GUIX_PACKAGE_PATH.

This looks good to me.

> From 298dcc1dd3aac49e033debeea154c91b25229c14 Mon Sep 17 00:00:00 2001
> From: David Thompson <dthompson2@worcester.edu>
> 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.

[...]

> +(define (purify-environment)
> +  "Unset almost all environment variables.  A small number of variables such
> +as 'HOME' and 'USER' are left untouched."
> +  (for-each unsetenv
> +            (filter (lambda (variable)
> +                      ;; Protect some env vars from purification.  Borrowed
> +                      ;; from nix-shell.
> +                      (not (member variable
> +                                   '("HOME" "USER" "LOGNAME" "DISPLAY"
> +                                     "TERM" "TZ" "PAGER"))))
> +                    (map car (get-environment-variables)))))

Maybe put the list of env. vars in a global variable, say
‘%precious-variables’, and then:

  (remove (cut member <> %precious-variables)
          (match (get-environment-variables)
            (((names . values) ...)
             names)))

(My allergy to ‘car’ goes this far.  ;-))

> +  (display (_ "
> +  -e, --exec             shell command to execute"))

Perhaps make it -E, and use -e consistently with ‘guix build’?  It may
be possible to reuse ‘options/resolve-packages’ for that.

> +(define %default-options
> +  ;; Default to opening a new shell.
> +  `((exec . ,(getenv "SHELL"))

(or (getenv "SHELL") "/bin/sh")

> +(define (build-inputs inputs opts)
> +  "Build the packages in INPUTS using the build options in OPTS."
> +  (with-store store
> +    (run-with-store store
> +      (mlet* %store-monad ((drvs (sequence %store-monad
> +                                           (map package->derivation inputs))))
> +        (mbegin %store-monad
> +          (show-what-to-build* drvs
> +                               #:use-substitutes? (assoc-ref opts 'substitutes?)
> +                               #:dry-run? #f)
> +          (set-build-options-from-command-line* opts)
> +          (built-derivations drvs)
> +          (return drvs))))))

The store should rather be kept open around (system command).  Otherwise
the above derivations and their outputs could be GC’d (it should be
possible to check that by trying to run ‘guix gc -d XXX’ on one of them
from within the sub-shell.)

> +;; 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))
> +
> +  (let* ((opts (parse-options))
> +         (pure? (assoc-ref opts 'pure))
> +         (command (assoc-ref opts 'exec))
> +         ;; Load from file if given, otherwise search for packages.
> +         (inputs (packages->transitive-inputs
> +                  (or (and=> (assoc-ref opts 'load) load)
> +                      (map specification->package
> +                           (pick-all opts 'package)))))
> +         (drvs (build-inputs inputs opts)))

Would be good to honor --dry-run as well.  It would just print what
needs to be built and exit.

Thank you!

Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-09 19:30 ` Ludovic Courtès
@ 2014-10-10  2:32   ` David Thompson
  2014-10-10 12:09     ` David Thompson
  0 siblings, 1 reply; 17+ messages in thread
From: David Thompson @ 2014-10-10  2:32 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

[-- Attachment #1: Type: text/plain, Size: 3575 bytes --]

Ludovic Courtès <ludo@gnu.org> writes:

[...]

>> +(define (purify-environment)
>> +  "Unset almost all environment variables.  A small number of variables such
>> +as 'HOME' and 'USER' are left untouched."
>> +  (for-each unsetenv
>> +            (filter (lambda (variable)
>> +                      ;; Protect some env vars from purification.  Borrowed
>> +                      ;; from nix-shell.
>> +                      (not (member variable
>> +                                   '("HOME" "USER" "LOGNAME" "DISPLAY"
>> +                                     "TERM" "TZ" "PAGER"))))
>> +                    (map car (get-environment-variables)))))
>
> Maybe put the list of env. vars in a global variable, say
> ‘%precious-variables’, and then:
>
>   (remove (cut member <> %precious-variables)
>           (match (get-environment-variables)
>             (((names . values) ...)
>              names)))
>
> (My allergy to ‘car’ goes this far.  ;-))
>

Done.  Very elegant.

>> +  (display (_ "
>> +  -e, --exec             shell command to execute"))
>
> Perhaps make it -E, and use -e consistently with ‘guix build’?  It may
> be possible to reuse ‘options/resolve-packages’ for that.
>

Done.  Flag changed to '-E', and '-e'/'--expression' flag added.  I
wrote my another version of 'options/resolve-packages' since my needs
are different than 'guix build'.

>> +(define %default-options
>> +  ;; Default to opening a new shell.
>> +  `((exec . ,(getenv "SHELL"))
>
> (or (getenv "SHELL") "/bin/sh")
>

Done.

>> +(define (build-inputs inputs opts)
>> +  "Build the packages in INPUTS using the build options in OPTS."
>> +  (with-store store
>> +    (run-with-store store
>> +      (mlet* %store-monad ((drvs (sequence %store-monad
>> +                                           (map package->derivation inputs))))
>> +        (mbegin %store-monad
>> +          (show-what-to-build* drvs
>> +                               #:use-substitutes? (assoc-ref opts 'substitutes?)
>> +                               #:dry-run? #f)
>> +          (set-build-options-from-command-line* opts)
>> +          (built-derivations drvs)
>> +          (return drvs))))))
>
> The store should rather be kept open around (system command).  Otherwise
> the above derivations and their outputs could be GC’d (it should be
> possible to check that by trying to run ‘guix gc -d XXX’ on one of them
> from within the sub-shell.)
>

Great point.  Done.

>> +;; 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))
>> +
>> +  (let* ((opts (parse-options))
>> +         (pure? (assoc-ref opts 'pure))
>> +         (command (assoc-ref opts 'exec))
>> +         ;; Load from file if given, otherwise search for packages.
>> +         (inputs (packages->transitive-inputs
>> +                  (or (and=> (assoc-ref opts 'load) load)
>> +                      (map specification->package
>> +                           (pick-all opts 'package)))))
>> +         (drvs (build-inputs inputs opts)))
>
> Would be good to honor --dry-run as well.  It would just print what
> needs to be built and exit.
>

Done.

Updated patch attached.  Thanks for the feedback!


[-- Attachment #2: 0001-scripts-Add-environment-command.patch --]
[-- Type: text/x-diff, Size: 10831 bytes --]

From 3dffa5a126173d46c22750a7d7f454e2c24ce4ea Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
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.
---
 Makefile.am                  |   1 +
 guix/scripts/environment.scm | 237 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 238 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 =					\
   guix/scripts/system.scm			\
   guix/scripts/lint.scm				\
   guix/scripts/import/nix.scm			\
+  guix/scripts/environment.scm			\
   guix.scm					\
   $(GNU_SYSTEM_MODULES)
 
diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
new file mode 100644
index 0000000..5c0d2b8
--- /dev/null
+++ b/guix/scripts/environment.scm
@@ -0,0 +1,237 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2014 David Thompson <davet@gnu.org>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+(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 (gnu packages commencement)
+  #: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 search
+path value is appended."
+  (let ((paths (map derivation->output-path derivations)))
+    (for-each (match-lambda
+               (($ <search-path-specification>
+                   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 such
+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.  When
+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 the
+packages within INPUTS.  When PURE? is #t, do not augment existing environment
+variables with additional search paths."
+  (for-each-search-path (lambda (variable value)
+                          (format #t "export ~a=\"~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=EXPR  create environment for the package that EXPR
+                         evaluates to"))
+  (display (_ "
+  -l, --load=FILE        create environment for the package that the code within
+                         FILE evaluates to"))
+  (display (_ "
+  -E, --exec             shell command to execute"))
+  (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))))))
-- 
2.1.0


[-- Attachment #3: Type: text/plain, Size: 136 bytes --]


-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10  2:32   ` David Thompson
@ 2014-10-10 12:09     ` David Thompson
  2014-10-10 16:37       ` Ludovic Courtès
  0 siblings, 1 reply; 17+ messages in thread
From: David Thompson @ 2014-10-10 12:09 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

[-- Attachment #1: Type: text/plain, Size: 110 bytes --]

> Updated patch attached.  Thanks for the feedback!
>

Tried my hand at writing some docs.  New patch below.


[-- Attachment #2: 0001-scripts-Add-environment-command.patch --]
[-- Type: text/x-diff, Size: 13957 bytes --]

From 26cac530c7a1f69864fc819bdc14553eacd9dc23 Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
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                |  73 +++++++++++++
 guix/scripts/environment.scm | 236 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 310 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 =					\
   guix/scripts/system.scm			\
   guix/scripts/lint.scm				\
   guix/scripts/import/nix.scm			\
+  guix/scripts/environment.scm			\
   guix.scm					\
   $(GNU_SYSTEM_MODULES)
 
diff --git a/doc/guix.texi b/doc/guix.texi
index c9760f5..5b9c933 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -2417,6 +2417,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::   Creating new environments from package inputs.
 @end menu
 
 @node Invoking guix build
@@ -2826,6 +2827,78 @@ and exit.
 
 @end table
 
+@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 a package,
+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
+
+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.
+
+@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=make
+@end example
+
+If the specified packages are not built yet, @command{guix environment}
+automatically builds them.  The build process may be controlled with the
+same options that can be passed to the @command{guix build} command
+(@pxref{Invoking guix build, common build options}).
+
+The following options are available:
+
+@table @code
+@item --expression=@var{expr}
+@itemx -e @var{expr}
+Create an environment for the package that @var{expr} evaluates to.
+
+@item --load=@var{file}
+@itemx -l @var{file}
+Create an environment for the package that the code within @var{file}
+evaluates to.
+
+@item --exec=@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 needed environment variable definitions.
+@end table
+
 @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 © 2014 David Thompson <davet@gnu.org>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+(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 search
+path value is appended."
+  (let ((paths (map derivation->output-path derivations)))
+    (for-each (match-lambda
+               (($ <search-path-specification>
+                   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 such
+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.  When
+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 the
+packages within INPUTS.  When PURE? is #t, do not augment existing environment
+variables with additional search paths."
+  (for-each-search-path (lambda (variable value)
+                          (format #t "export ~a=\"~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=EXPR  create environment for the package that EXPR
+                         evaluates to"))
+  (display (_ "
+  -l, --load=FILE        create environment for the package that the code within
+                         FILE evaluates to"))
+  (display (_ "
+  -E, --exec=COMMAND     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))))))
-- 
2.1.0


[-- Attachment #3: Type: text/plain, Size: 137 bytes --]



-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10 12:09     ` David Thompson
@ 2014-10-10 16:37       ` Ludovic Courtès
  2014-10-10 18:09         ` David Thompson
  0 siblings, 1 reply; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-10 16:37 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

> From 26cac530c7a1f69864fc819bdc14553eacd9dc23 Mon Sep 17 00:00:00 2001
> From: David Thompson <dthompson2@worcester.edu>
> 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’t mention it before but it would be wonderful if you could come
up with a little test case.  It’s 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 ‘guile’ in $PATH.

WDYT?  That can come in a later commit if you prefer.

> diff --git a/doc/guix.texi b/doc/guix.texi
> index c9760f5..5b9c933 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -2417,6 +2417,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::   Creating new environments from package inputs.

“Setting up development environments.” maybe?

We should also add a

  @dircategory Software development
  @direntry
  ...

at the top of the file for this node.

> +@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 a package,

s/a package/one or more packages/

> +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.

+ something like “, in which case all the inputs ...  For example, the
command below spawns a shell where all the dependencies of both Guile
and Emacs are available:”

> +@item --exec=@var{command}
> +@item -e @var{command}

Should be -E.

> +@item --search-paths
> +Display needed environment variable definitions.

“Display the environment variable definitions that make up the
environment”, or something like that?  (I find “needed” ambiguous.)

> +@end table

Here add something like:

  It also supports all the common build options that @command{guix
  build} supports (@pxref{Invoking guix build, common build options}).

The rest is good!

Thanks,
Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10 16:37       ` Ludovic Courtès
@ 2014-10-10 18:09         ` David Thompson
  2014-10-10 20:47           ` Ludovic Courtès
  0 siblings, 1 reply; 17+ messages in thread
From: David Thompson @ 2014-10-10 18:09 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

Ludovic Courtès <ludo@gnu.org> writes:

> David Thompson <dthompson2@worcester.edu> skribis:
>
>> From 26cac530c7a1f69864fc819bdc14553eacd9dc23 Mon Sep 17 00:00:00 2001
>> From: David Thompson <dthompson2@worcester.edu>
>> 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’t mention it before but it would be wonderful if you could come
> up with a little test case.  It’s 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 ‘guile’ 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.


[-- Attachment #2: 0001-scripts-Add-environment-command.patch --]
[-- Type: text/x-diff, Size: 14578 bytes --]

From bdcebf9db0c93f4cabc388acf20720fdcae90037 Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
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 =					\
   guix/scripts/system.scm			\
   guix/scripts/lint.scm				\
   guix/scripts/import/nix.scm			\
+  guix/scripts/environment.scm			\
   guix.scm					\
   $(GNU_SYSTEM_MODULES)
 
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
 
+@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
 
 @node Invoking guix build
@@ -2840,6 +2847,81 @@ and exit.
 
 @end table
 
+@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=make
+@end example
+
+The following options are available:
+
+@table @code
+@item --expression=@var{expr}
+@itemx -E @var{expr}
+Create an environment for the package that @var{expr} evaluates to.
+
+@item --load=@var{file}
+@itemx -l @var{file}
+Create an environment for the package that the code within @var{file}
+evaluates to.
+
+@item --exec=@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 © 2014 David Thompson <davet@gnu.org>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+(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 search
+path value is appended."
+  (let ((paths (map derivation->output-path derivations)))
+    (for-each (match-lambda
+               (($ <search-path-specification>
+                   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 such
+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.  When
+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 the
+packages within INPUTS.  When PURE? is #t, do not augment existing environment
+variables with additional search paths."
+  (for-each-search-path (lambda (variable value)
+                          (format #t "export ~a=\"~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=EXPR  create environment for the package that EXPR
+                         evaluates to"))
+  (display (_ "
+  -l, --load=FILE        create environment for the package that the code within
+                         FILE evaluates to"))
+  (display (_ "
+  -E, --exec=COMMAND     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))))))
-- 
2.1.0


[-- Attachment #3: Type: text/plain, Size: 137 bytes --]



-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10 18:09         ` David Thompson
@ 2014-10-10 20:47           ` Ludovic Courtès
  2014-10-10 22:16             ` David Thompson
  0 siblings, 1 reply; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-10 20:47 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

>> I didn’t mention it before but it would be wonderful if you could come
>> up with a little test case.  It’s 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 ‘guile’ 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.

Ah OK.

> +@table @code
> +@item --expression=@var{expr}
> +@itemx -E @var{expr}
> +Create an environment for the package that @var{expr} evaluates to.

[...]

> +@item --exec=@var{command}
> +@item -e @var{command}
> +Execute @var{command} in the new environment.

Typo: swap -e/-E.

The rest is alright, OK to commit!

Thank you!

Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10 20:47           ` Ludovic Courtès
@ 2014-10-10 22:16             ` David Thompson
  2014-10-11 10:35               ` Ludovic Courtès
  0 siblings, 1 reply; 17+ messages in thread
From: David Thompson @ 2014-10-10 22:16 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

Ludovic Courtès <ludo@gnu.org> writes:

> The rest is alright, OK to commit!
>

Fixed typo and pushed!

-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-10 22:16             ` David Thompson
@ 2014-10-11 10:35               ` Ludovic Courtès
  2014-10-11 12:27                 ` David Thompson
  2014-10-12  4:43                 ` Setting environment variables in .bashrc vs .bash_profile Mark H Weaver
  0 siblings, 2 replies; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-11 10:35 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

> Fixed typo and pushed!

Yay!

I just noticed that ‘guix environment foo’, without -E, doesn’t work for
me because my .bashrc & co. reset PATH, CPATH, etc.

Conversely, ‘guix environment foo -E /bin/sh’ works, because when
invoked as ‘sh’, Bash does not read .bashrc.

I’m not sure if it’s something wrong with my Bash initialization files
or something that might affect others.  WDYT?

Thanks,
Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-11 10:35               ` Ludovic Courtès
@ 2014-10-11 12:27                 ` David Thompson
  2014-10-11 21:52                   ` Ludovic Courtès
  2014-10-12  4:43                 ` Setting environment variables in .bashrc vs .bash_profile Mark H Weaver
  1 sibling, 1 reply; 17+ messages in thread
From: David Thompson @ 2014-10-11 12:27 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

Ludovic Courtès <ludo@gnu.org> writes:

> David Thompson <dthompson2@worcester.edu> skribis:
>
>> Fixed typo and pushed!
>
> Yay!
>
> I just noticed that ‘guix environment foo’, without -E, doesn’t work for
> me because my .bashrc & co. reset PATH, CPATH, etc.
>
> Conversely, ‘guix environment foo -E /bin/sh’ works, because when
> invoked as ‘sh’, Bash does not read .bashrc.
>
> I’m not sure if it’s something wrong with my Bash initialization files
> or something that might affect others.  WDYT?
>

I don't think it's something "wrong" with your files, really, but it is
incompatible with the way 'guix environment' currently operates.

I took another look at nix-build to see how they do things.  They create
a bashrc file in a temporary directory and run bash (or whatever
$NIX_BUILD_SHELL is) with the '--rcfile' flag so that the user's bashrc
isn't used.  We could do something similar, but right now I don't have
any reason to believe that all shells accept an '--rcfile' option, so it
could easily break other setups.

Thoughts?

-- 
David Thompson
Web Developer - Free Software Foundation - http://fsf.org
GPG Key: 0FF1D807
Support the FSF: https://fsf.org/donate

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] scripts: Add 'environment' command.
  2014-10-11 12:27                 ` David Thompson
@ 2014-10-11 21:52                   ` Ludovic Courtès
  0 siblings, 0 replies; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-11 21:52 UTC (permalink / raw)
  To: David Thompson; +Cc: guix-devel

David Thompson <dthompson2@worcester.edu> skribis:

> I don't think it's something "wrong" with your files, really, but it is
> incompatible with the way 'guix environment' currently operates.
>
> I took another look at nix-build to see how they do things.  They create
> a bashrc file in a temporary directory and run bash (or whatever
> $NIX_BUILD_SHELL is) with the '--rcfile' flag so that the user's bashrc
> isn't used.  We could do something similar, but right now I don't have
> any reason to believe that all shells accept an '--rcfile' option, so it
> could easily break other setups.

POSIX specifies the ‘ENV’ environment variable for that:

  http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html

So that’s one thing that could be used.  Apparently Bash checks
‘BASH_ENV’ instead, unless invoked as ‘sh’ (info "(bash) Bash Startup
Files").

In the longer run, another option would be to have a Bash built-in
command, in the form of a plug-in loaded with ‘enable FOO.so’, that
would modify the environment variables of the running Bash process
(recutils has an example of that.)

My 2¢,
Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Setting environment variables in .bashrc vs .bash_profile
  2014-10-11 10:35               ` Ludovic Courtès
  2014-10-11 12:27                 ` David Thompson
@ 2014-10-12  4:43                 ` Mark H Weaver
  2014-10-12 21:10                   ` Ludovic Courtès
  1 sibling, 1 reply; 17+ messages in thread
From: Mark H Weaver @ 2014-10-12  4:43 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

ludo@gnu.org (Ludovic Courtès) writes:

> I just noticed that ‘guix environment foo’, without -E, doesn’t work for
> me because my .bashrc & co. reset PATH, CPATH, etc.
>
> Conversely, ‘guix environment foo -E /bin/sh’ works, because when
> invoked as ‘sh’, Bash does not read .bashrc.
>
> I’m not sure if it’s something wrong with my Bash initialization files
> or something that might affect others.  WDYT?

FWIW, I've always put my environment variable settings in .bash_profile
instead of .bashrc for this reason.  I want to be able to set up an
alternate environment and launch interactive subshells without resetting
everything.

I vaguely remember noticing that the standalone Guix system set things
up in such a way that my environment was reset in every interactive
shell.  If so, I would be in favor of changing that.

On the other hand, I end up with
/gnu/store/10fr8jbnrb3gzyc0967m7ar64ch9ggk8-windowmaker-0.95.6/bin in
front of my PATH within my entire X session, because of our windowmaker
wrapper.

    Thoughts?
       Mark

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Setting environment variables in .bashrc vs .bash_profile
  2014-10-12  4:43                 ` Setting environment variables in .bashrc vs .bash_profile Mark H Weaver
@ 2014-10-12 21:10                   ` Ludovic Courtès
  0 siblings, 0 replies; 17+ messages in thread
From: Ludovic Courtès @ 2014-10-12 21:10 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guix-devel

Mark H Weaver <mhw@netris.org> skribis:

> ludo@gnu.org (Ludovic Courtès) writes:
>
>> I just noticed that ‘guix environment foo’, without -E, doesn’t work for
>> me because my .bashrc & co. reset PATH, CPATH, etc.
>>
>> Conversely, ‘guix environment foo -E /bin/sh’ works, because when
>> invoked as ‘sh’, Bash does not read .bashrc.
>>
>> I’m not sure if it’s something wrong with my Bash initialization files
>> or something that might affect others.  WDYT?
>
> FWIW, I've always put my environment variable settings in .bash_profile
> instead of .bashrc for this reason.  I want to be able to set up an
> alternate environment and launch interactive subshells without resetting
> everything.

OK.  (I never took the time to understand what’s supposed to go in
.bashrc and what’s supposed to go in .bash_profile; now’s the time to
fix it.  ;-))

> I vaguely remember noticing that the standalone Guix system set things
> up in such a way that my environment was reset in every interactive
> shell.  If so, I would be in favor of changing that.

/etc/profile defines a bunch of environment variables (see
gnu/system.scm.)

In turn, new user accounts get a default .bashrc (see shadow.scm) that
sources /etc/profile.

If I got it right, it should be called .bash_profile instead of .bashrc,
and should source .bashrc when present?

> On the other hand, I end up with
> /gnu/store/10fr8jbnrb3gzyc0967m7ar64ch9ggk8-windowmaker-0.95.6/bin in
> front of my PATH within my entire X session, because of our windowmaker
> wrapper.

Yeah, another bug that needs to be fixed.  Could you file it?

Thanks,
Ludo’.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2014-10-12 21:10 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-08 22:48 [PATCH] scripts: Add 'environment' command David Thompson
2014-10-09  6:44 ` Alex Kost
2014-10-09 16:50 ` Eric Bavier
2014-10-09 16:54   ` Thompson, David
2014-10-09 17:26     ` Eric Bavier
2014-10-09 19:30 ` Ludovic Courtès
2014-10-10  2:32   ` David Thompson
2014-10-10 12:09     ` David Thompson
2014-10-10 16:37       ` Ludovic Courtès
2014-10-10 18:09         ` David Thompson
2014-10-10 20:47           ` Ludovic Courtès
2014-10-10 22:16             ` David Thompson
2014-10-11 10:35               ` Ludovic Courtès
2014-10-11 12:27                 ` David Thompson
2014-10-11 21:52                   ` Ludovic Courtès
2014-10-12  4:43                 ` Setting environment variables in .bashrc vs .bash_profile Mark H Weaver
2014-10-12 21:10                   ` Ludovic Courtès

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).