Search code examples
emacsenvironment-variableselisp

assign value from environment variable to alist


I run Emacs in both office and home, while in office it is behind a firewall.

An environment variable http_proxy is only defined in my office PCs, I want my Emacs to check if environment variable http_proxy is not empty, if that is the case then use its value to setup url_proxy_service. So I tried something like this

 (setq proxy (getenv "http_proxy"))
 (setq url-proxy-services
    '(("http" . proxy)
      ("https" . proxy)))

I can see proxy picks up the value in environment variable http_proxy, but url-proxy-services becomes (("http" . proxy) ("https" . proxy)).

So it looks to me proxy is not dereferenced but used as a string literal, what should I do? Thanks.


Solution

  • You can use Backquote to evaluate proxy while quoting the list:

    (setq proxy (getenv "http_proxy"))
    (setq url-proxy-services
          `(("http"  . ,proxy)
            ("https" . ,proxy)))