Doom multiple environments via dotenv

Hello, I decided to keep my doom folder at github, so I could sync my config between laptop and desktop.

This is okay but laptop has different resolution than the desktop, making the fonts appear small. Please note, my elisp knowledge is worth a week.

My first attempt was to add bare elisp to config.el. After a few rounds, I made stuff like this work:

(setq my-font-size
      (if (getenv "emacs_font_size")
          (string-to-number (getenv "emacs_font_size"))
        my-default-font-size))

(setq doom-font
      (font-spec :family my-font
                 :size my-font-size
                 :weight my-font-weight))

The few rounds I mentioned above can be summarized as:

  1. Exporting the variables from the shell. I had zsh, so that also took a few rounds, but none the less managed to do it.

  2. Making doom recognize the new envs. This was taken care of with a doom sync.

  3. I wanted to do the same for pitch font as well. So that let me with 6 envs to export and import.

All was working fine, but then the process didn’t seem very streaming. So with my week’s worth of elisp, I decided to write some functions for proof of concept.

(defun mb/load-env ()
  "loads local dotenv"
  (interactive)
  (setq envs  (split-string (f-read "~/.doom.d/.env") "\n" t))

  (dolist (env envs)
    (set 'keyval (split-string-and-unquote env "="))
    (set 'key (car keyval))
    (set 'val (car (cdr keyval)))
    (setenv key val)
    (print "done")
    ))

(defun mb/set-env-fonts()
  "sets font from env"
  (interactive)
  (mb/load-env)
  (setq doom_font (getenv "doom_font"))
  (setq doom_font_length (length (split-string doom_font "\, ")))
  (setq doom_fonte (split-string doom_font "\, "))

  (if (and (> doom_font_length 1) doom_font)
      (progn (setq doom-font
                   (font-spec :family (nth 0 doom_fonte)
                              :size (string-to-number(nth 1 doom_fonte))
                              :weight (make-symbol (concat (nth 2 doom_fonte)))))
             (print (nth 1 doom_fonte))
             (doom/reload-font))))
  1. This reads the .env through emacs, doesn’t require any shell magic.

  2. This also does not put any bare elisp into the config, I can hook this somewhere, and can call without exiting emacs.

This is working fine too, but before I go and try to optimize this, I wanted to ask if I am reinventing the wheel and was there already a better way to handle this task of dotenv for doom.

You mean like GitHub - pkulev/dotenv.el: Dotenv - Emacs plugin for loading project's .env file.?

If you’re just trying to replicate local settings, you don’t need much more than GitHub - elken/doom: Doom Emacs config

Thank you for the package, it’s definitely cleaner than my dotenv function :slight_smile:

I will use it instead for reading the .env file and use the second function to update the fonts and do the reload.

(add-hook 'doom-init-ui-hook #'mb/set-env-fonts )

Do you see any issues with using this hook for font update, or is there an earlier one?

P.S It somehow didn’t occur to me to use local-settings logic with a file exists, but it’s already too late to go lazy :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.