Skip to content
Snippets Groups Projects
Commit 8e3a3bc2 authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

environment: Move iteration outside of 'for-each-search-path'.

* guix/search-paths.scm (search-path-definition): New procedure.
* guix/scripts/environment.scm (for-each-search-path): Rename to...
  (evaluate-input-search-paths): ... this.  Remove 'proc' and 'pure?'
  parameters, and return directly the list of search-path/value pairs.
  (create-environment): Use 'for-each' and 'evaluate-input-search-paths'
  instead of 'for-each-search-path'.
  (show-search-paths): Use 'for-each', 'search-path-definition', and
  'evaluate-search-paths' instead of 'for-each-search-path'.
parent b9ea6c6b
No related branches found
No related tags found
No related merge requests found
...@@ -36,11 +36,9 @@ (define-module (guix scripts environment) ...@@ -36,11 +36,9 @@ (define-module (guix scripts environment)
#:use-module (srfi srfi-98) #:use-module (srfi srfi-98)
#:export (guix-environment)) #:export (guix-environment))
(define (for-each-search-path proc inputs derivations pure?) (define (evaluate-input-search-paths inputs derivations)
"Apply PROC for each native search path in INPUTS in addition to 'PATH'. "Evaluate the native search paths of INPUTS, a list of packages, of the
Use the output paths of DERIVATIONS to build each search path. When PURE? is outputs of DERIVATIONS, and return a list of search-path/value pairs."
#t, the existing search path value is ignored. Otherwise, the existing search
path value is appended."
(let ((directories (append-map (lambda (drv) (let ((directories (append-map (lambda (drv)
(map (match-lambda (map (match-lambda
((_ . output) ((_ . output)
...@@ -51,14 +49,7 @@ (define (for-each-search-path proc inputs derivations pure?) ...@@ -51,14 +49,7 @@ (define (for-each-search-path proc inputs derivations pure?)
(delete-duplicates (delete-duplicates
(append-map package-native-search-paths (append-map package-native-search-paths
inputs))))) inputs)))))
(for-each (match-lambda (evaluate-search-paths paths directories)))
((($ <search-path-specification> variable _ sep) . value)
(let ((current (getenv variable)))
(proc variable
(if (and current (not pure?))
(string-append value sep current)
value)))))
(evaluate-search-paths paths directories))))
;; Protect some env vars from purification. Borrowed from nix-shell. ;; Protect some env vars from purification. Borrowed from nix-shell.
(define %precious-variables (define %precious-variables
...@@ -78,15 +69,26 @@ (define (create-environment inputs derivations pure?) ...@@ -78,15 +69,26 @@ (define (create-environment inputs derivations pure?)
PURE? is #t, unset the variables in the current environment. Otherwise, PURE? is #t, unset the variables in the current environment. Otherwise,
augment existing enviroment variables with additional search paths." augment existing enviroment variables with additional search paths."
(when pure? (purify-environment)) (when pure? (purify-environment))
(for-each-search-path setenv inputs derivations pure?)) (for-each (match-lambda
((($ <search-path-specification> variable _ separator) . value)
(let ((current (getenv variable)))
(setenv variable
(if (and current (not pure?))
(string-append value separator current)
value)))))
(evaluate-input-search-paths inputs derivations)))
(define (show-search-paths inputs derivations pure?) (define (show-search-paths inputs derivations pure?)
"Display the needed search paths to build an environment that contains the "Display the needed search paths to build an environment that contains the
packages within INPUTS. When PURE? is #t, do not augment existing environment packages within INPUTS. When PURE? is #t, do not augment existing environment
variables with additional search paths." variables with additional search paths."
(for-each-search-path (lambda (variable value) (for-each (match-lambda
(format #t "export ~a=\"~a\"~%" variable value)) ((search-path . value)
inputs derivations pure?)) (display
(search-path-definition search-path value
#:kind (if pure? 'exact 'prefix)))
(newline)))
(evaluate-input-search-paths inputs derivations)))
(define (show-help) (define (show-help)
(display (_ "Usage: guix environment [OPTION]... PACKAGE... (display (_ "Usage: guix environment [OPTION]... PACKAGE...
......
...@@ -36,7 +36,8 @@ (define-module (guix search-paths) ...@@ -36,7 +36,8 @@ (define-module (guix search-paths)
search-path-specification->sexp search-path-specification->sexp
sexp->search-path-specification sexp->search-path-specification
evaluate-search-paths evaluate-search-paths
environment-variable-definition)) environment-variable-definition
search-path-definition))
;;; Commentary: ;;; Commentary:
;;; ;;;
...@@ -160,7 +161,7 @@ (define* (environment-variable-definition variable value ...@@ -160,7 +161,7 @@ (define* (environment-variable-definition variable value
#:key #:key
(kind 'exact) (kind 'exact)
(separator ":")) (separator ":"))
"Return a the definition of VARIABLE to VALUE in Bash syntax: "Return a the definition of VARIABLE to VALUE in Bash syntax.
KIND can be either 'exact (return the definition of VARIABLE=VALUE), KIND can be either 'exact (return the definition of VARIABLE=VALUE),
'prefix (return the definition where VALUE is added as a prefix to VARIABLE's 'prefix (return the definition where VALUE is added as a prefix to VARIABLE's
...@@ -178,4 +179,14 @@ (define* (environment-variable-definition variable value ...@@ -178,4 +179,14 @@ (define* (environment-variable-definition variable value
(format #f "export ~a=\"$~a${~a:+~a}~a\"" (format #f "export ~a=\"$~a${~a:+~a}~a\""
variable variable variable separator value)))) variable variable variable separator value))))
(define* (search-path-definition search-path value
#:key (kind 'exact))
"Similar to 'environment-variable-definition', but applied to a
<search-path-specification>."
(match search-path
(($ <search-path-specification> variable _ separator)
(environment-variable-definition variable value
#:kind kind
#:separator separator))))
;;; search-paths.scm ends here ;;; search-paths.scm ends here
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment