guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] scripts: Add 'environment' command.


From: David Thompson
Subject: [PATCH] scripts: Add 'environment' command.
Date: Wed, 08 Oct 2014 18:48:09 -0400
User-agent: Notmuch/0.18.1 (http://notmuchmail.org) Emacs/24.3.1 (x86_64-pc-linux-gnu)

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/mbacj3k992756mxskp26pd8b476mynhk-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?

>From 298dcc1dd3aac49e033debeea154c91b25229c14 Mon Sep 17 00:00:00 2001
From: David Thompson <address@hidden>
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 <address@hidden>
+;;;
+;;; 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

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]