Skip to content

Commit

Permalink
tweak(core): better envvars management
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Jul 19, 2023
1 parent 2445798 commit 7180101
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/me-vars.el
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ MinEmacs hooks will be run in this order:
"^HOME$" "^\\(OLD\\)?PWD$" "^SHLVL$" "^PS1$" "^R?PROMPT$" "^TERM\\(CAP\\)?$"
"^USER$" "^GIT_CONFIG" "^INSIDE_EMACS$" "^SESSION_MANAGER$" "^_$"
"^JOURNAL_STREAM$" "^INVOCATION_ID$" "^MANAGERPID$" "^SYSTEMD_EXEC_PID$"
"^DESKTOP_STARTUP_ID$"
"^DESKTOP_STARTUP_ID$" "^LS_?COLORS$" "^$"
;; KDE session
"^KDE_\\(FULL_SESSION\\|APPLICATIONS_.*\\|SESSION_\\(UID\\|VERSION\\)\\)$"
;; X server, Wayland, or services' env that shouldn't be persisted
Expand Down
27 changes: 26 additions & 1 deletion elisp/+minemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,30 @@ If N and M = 1, there's no benefit to using this macro over `remove-hook'.
(let (byte-compile-warnings)
(+shutup! (byte-compile fn)))))))

(defvar +shell-file-name (or (getenv "SHELL") shell-file-name))

(defvar +shell-extra-args
(pcase +shell-file-name
("fish" (list "-l"))
((rx (or "tsch" "csh")) (list "-d"))
(t (list "-l" "-i"))))

(defun +shell-command-to-string (command &optional keep-errors shell)
(interactive "s")
(with-temp-buffer
(unless
(zerop
(call-process-shell-command
(string-join
(append (list (or shell +env-shell))
+env-shell-args
(list "-c" "'" command "'")
(unless keep-errors (list "2>> /tmp/minemacs-env-err")))
" ")
nil (current-buffer)))
(user-error "The `%s' command exited with a non-zero code." command))
(buffer-string)))

;;;###autoload
(defun +env-save ()
"Load environment variables from shell and save them to `+env-file'."
Expand All @@ -404,7 +428,8 @@ If N and M = 1, there's no benefit to using this macro over `remove-hook'.
(let ((env-vars
(mapcar ; Get environment variables from shell into an alist
(lambda (l) (let ((s (string-split l "="))) (cons (car s) (string-join (cdr s) "="))))
(string-lines (shell-command-to-string "env") "="))))
;; "env --null" ends lines with null byte instead of newline
(string-split (+shell-command-to-string "env --null") "\0"))))
;; Special treatment for the "PATH" variable, save it to `exec-path'
(when-let ((path (alist-get "PATH" env-vars nil nil #'string=)))
(insert "\n;; Adding PATH content to `exec-path'\n"
Expand Down

0 comments on commit 7180101

Please sign in to comment.