unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: Danny O'Brien <danny@spesh.com>
Cc: guix-devel@gnu.org, Brendan Tildesley <mail@brendan.scot>
Subject: Re: ‘guix environment’ vs. ‘.bash_profile’
Date: Wed, 16 Sep 2020 22:17:06 +0200	[thread overview]
Message-ID: <87v9gdlbnh.fsf@gnu.org> (raw)
In-Reply-To: <874ko14bjt.fsf@spesh.com> (Danny O'Brien's message of "Sun, 13 Sep 2020 14:22:14 -0700")

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

Hey,

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…?

Ludo’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 3163 bytes --]

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?

  parent reply	other threads:[~2020-09-16 20:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-13  5:24 ‘guix environment’ vs. ‘.bash_profile’ Brendan Tildesley
2020-09-13 21:22 ` Danny O'Brien
2020-09-16 16:47   ` Ludovic Courtès
2020-09-16 17:58     ` Ricardo Wurmus
2020-09-16 20:17   ` Ludovic Courtès [this message]
  -- strict thread matches above, loose matches on Subject: below --
2020-09-12 12:49 Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v9gdlbnh.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=danny@spesh.com \
    --cc=guix-devel@gnu.org \
    --cc=mail@brendan.scot \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).