This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/core/core-env.el

114 lines
4.9 KiB
EmacsLisp

;;; core-env.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(require 'core-dotspacemacs)
(require 'load-env-vars)
(defvar spacemacs-env-vars-file
(concat (or dotspacemacs-directory user-home-directory) ".spacemacs.env")
"Absolute path to the env file where environment variables are set.")
(defvar spacemacs-ignored-environment-variables
'(
"DBUS_SESSION_BUS_ADDRESS"
"GPG_AGENT_INFO"
"SSH_AGENT_PID"
"SSH_AUTH_SOCK"
)
"Ignored environments variables. This env. vars are not import in the
`.spacemacs.env' file.")
(defvar spacemacs--spacemacs-env-loaded nil
"non-nil if `spacemacs/load-spacemacs-env' has been called at least once.")
(defun spacemacs//init-spacemacs-env (&optional force)
"Attempt to fetch the environment variables from the users shell.
This solution is far from perfect and we should not rely on this function
a lot. We use it only to initialize the env file when it does not exist
yet.
If FORCE is non-nil then force the initialization of the file, note that the
current contents of the file will be overwritten."
(when (or force (not (file-exists-p spacemacs-env-vars-file)))
(with-temp-file spacemacs-env-vars-file
(let ((shell-command-switch (cond
((or(eq system-type 'darwin)
(eq system-type 'gnu/linux))
"-ic")
((eq system-type 'windows-nt) "-c")))
(executable (cond ((or(eq system-type 'darwin)
(eq system-type 'gnu/linux)) "env")
((eq system-type 'windows-nt) "set"))))
(insert
(concat
"# ---------------------------------------------------------------------------\n"
"# Spacemacs environment variables\n"
"# ---------------------------------------------------------------------------\n"
"# This file has been generated by Spacemacs. It contains all found\n"
"# environment variables defined in your default shell except the\n"
"# black listed variables defined in `spacemacs-ignored-environment-variables'.\n"
"#\n"
"# You can safely edit this file, Spacemacs won't overwite it unless you call\n"
"# the function `spacemacs/force-init-spacemacs-env'."
"\n"
"# If you don't want to use this file and manage your environment variables\n"
"# yourself then remove the call to `spacemacs/load-spacemacs-env' from your\n"
"# `dotspacemacs/user-env' function in your dotfile and replace it with your\n"
"# own initialization code. You can use `exec-path-from-shell' if you add it\n"
"# to your additional packages or simply use `setenv' and\n"
"# `(add-to-list 'exec-path ...)' which are built-in.\n"
"#\n"
"# It is recommended to get used to this file as it unambiguously and\n"
"# explicitly set the values of your environment variables.\n"
"# ---------------------------------------------------------------------------\n"
"\n"
"# Environment variables:\n"
"# ----------------------\n"))
(insert (string-join
(sort (split-string (shell-command-to-string executable) "\n")
'string-lessp)
"\n"))
(dolist (v spacemacs-ignored-environment-variables)
(flush-lines v (point-min) (point-max)))))
(spacemacs-buffer/warning
(concat "Spacemacs has imported your environment variables from "
"your shell and saved them to `%s'.\n"
"Open this file for more info (SPC f e e) or call "
"`spacemacs/edit-env' function.")
spacemacs-env-vars-file)))
(defun spacemacs/force-init-spacemacs-env ()
"Forces a reinitialization of environment variables."
(interactive)
(spacemacs//init-spacemacs-env t))
(defun spacemacs/edit-env ()
"Open the env file for edition."
(interactive)
(if (and spacemacs--spacemacs-env-loaded
(file-exists-p spacemacs-env-vars-file))
(progn
(find-file spacemacs-env-vars-file)
(when (fboundp 'dotenv-mode)
(dotenv-mode)))
;; fallback to the dotspacemacs/user-env
(dotspacemacs/go-to-user-env)))
(defun spacemacs/load-spacemacs-env (&optional force)
"Load the environment variables from the `.spacemacs.env' file.
If FORCE is non-nil then force the loading of environment variables from env.
file."
(interactive "P")
(setq spacemacs--spacemacs-env-loaded t)
(when (or force (display-graphic-p))
(spacemacs//init-spacemacs-env)
(load-env-vars spacemacs-env-vars-file)))
(provide 'core-env)