[Top][All Lists]

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

Re: ‘guix environment’ vs. ‘.bash_profile’

From: Ludovic Courtès
Subject: Re: ‘guix environment’ vs. ‘.bash_profile’
Date: Wed, 16 Sep 2020 22:17:06 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)


I was toying with the idea of automatically detecting discrepancies in
environment variables, so I tried the attached patch, which would check
/proc/PID/environ compared to the profile’s search path variables.

Unfortunately, that doesn’t work because Bash for instance does not
change its own environment variables (with setenv(3) or environ(7)) when
you type “export PATH=foo” or similar (which makes sense, after all).

I’m thinking the only solution left would be to grep ~/.bashrc and
similar for potentially problematic environment variable settings.
Should we do that every time?  Or…?


diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
index ad50281eb2..33d5bd736a 100644
--- a/guix/scripts/environment.scm
+++ b/guix/scripts/environment.scm
@@ -43,7 +43,9 @@
   #:use-module (ice-9 format)
   #:use-module (ice-9 match)
   #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 textual-ports)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-9 gnu)
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-37)
@@ -424,6 +426,50 @@ regexps in WHITE-LIST."
     ((program . args)
      (apply execlp program program args))))
+(define-immutable-record-type <environment-discrepancy>
+  (environment-discrepancy path expected actual)
+  environment-discrepancy?
+  (path     environment-discrepancy-path)
+  (expected environment-discrepancy-expected)
+  (actual   environment-discrepancy-actual))
+(define (process-environment-variables pid)
+  (define split-on-nul
+    (cute string-tokenize <>
+          (char-set-complement (char-set #\nul))))
+  (define (split-on-= str)
+    (let ((offset (string-index str #\=)))
+      (cons (string-take str offset)
+            (string-drop str (+ 1 offset)))))
+  (call-with-input-file (string-append "/proc/" (number->string pid)
+                                       "/environ")
+    (lambda (port)
+      (map split-on-=
+           (split-on-nul (get-string-all port))))))
+(define (process-environment-discrepancies pid profile manifest)
+  (let ((variables (process-environment-variables pid))
+        (paths     (profile-search-paths profile manifest)))
+    (filter-map (match-lambda
+                  ((path . value)
+                   (let ((name (search-path-specification-variable
+                                path)))
+                     (match (assoc-ref variables name)
+                       (#f
+                        (environment-discrepancy path value #f))
+                       (actual
+                        (pk 'var name actual value)
+                        (and (not (string-prefix? value
+                                                  actual))
+                             (environment-discrepancy path
+                                                      value actual)))))))
+                paths)))
+(define (check-environment pid profile manifest)
+  (pk 'disc (process-environment-discrepancies pid profile manifest)))
 (define* (launch-environment/fork command profile manifest
                                   #:key pure? (white-list '()))
   "Run COMMAND in a new process with an environment containing PROFILE, with
@@ -434,8 +480,11 @@ regexps in WHITE-LIST."
     (0 (launch-environment command profile manifest
                            #:pure? pure?
                            #:white-list white-list))
-    (pid (match (waitpid pid)
-           ((_ . status) status)))))
+    (pid
+     (sleep 1)
+     (check-environment pid profile manifest)
+     (match (waitpid pid)
+       ((_ . status) status)))))
 (define* (launch-environment/container #:key command bash user user-mappings
                                        profile manifest link-profile? network?

reply via email to

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