Dotenv parser rules:
- skip empty lines
- trim every string (remove all spaces before first non-whitespace char and after last non-whitespace char)
- skip commentary lines (
#at the line start) - skip lines which doesn’t look like an proper assignment
- trim variable name and its value
This package can be used as a library for manipulating .env files. Contact me if you’re making something interesting with it, I’m looking for usecases ;-).
This package is not published on Melpa yet, so you need something like quelpa to
install it directly from the source repository.
(use-package dotenv
:ensure nil
:quelpa
(dotenv :repo "pkulev/dotenv.el"
:fetcher github :upgrade t))Note that in the following examples installation-related use-package clauses are omitted.
Built-in project.el is quite neat, but has no project-switch-hook. The only solution
I found is to hook on prog-mode for now, very suboptimal.
(use-package dotenv
:hook
(prog-mode . dotenv-update-current-env))It doesn’t happen by default, pass t as override into dotenv-update-* function:
(use-package dotenv
:hook
(prog-mode . (lambda () (dotenv-update-current-env t))))Hook for loading project’s .env (if exists) after switching project via projectile.
(use-package dotenv
:after projectile
:hook
(projectile-after-switch-project-hook . (lambda () (dotenv-update-project-env (projectile-project-root)))))In this example .env file contains PYTHONPATH among other vaiables.
Assuming that PYTHONPATH contains list of pathes (delimeted by ’:’) relative to
the project’s root we can transform it into absolute.
To do that add to dotenv-transform-alist a pair of a predicate function and a
transform function. Predicate function takes k and v and returns t or nil,
transform function takes k and v and returns two-element list with new values for k and v.
(use-package dotenv
:after projectile
:config
(defun dotenv-absolutify-pythonpath (k v)
(list k (dotenv-absolutify-path-var-in-project v)))
(add-to-list
'dotenv-transform-alist
'((lambda (k v) (string= k "PYTHONPATH")) . dotenv-absolutify-pythonpath))
:hook
(projectile-after-switch-project-hook . (lambda () (dotenv-update-project-env (projectile-project-root)))))| name | type | description |
|---|---|---|
dotenv-file-name | string | Dotenv file name (default: .env). |
dotenv-transform-alist | alist | List of predicate-transform pairs for |
| custom key or/and value processing. |
| name | arguments | returns | description |
|---|---|---|---|
| dotenv-load | abs-path (str) | list of (k v) pairs | Load .env from file. |
| dotenv-load-dir | root-dir (str) | list of (k v) pairs | Load .env from file in root-dir. |
| dotenv-get | key (str), path (str) | string | Returns a string value by a key. |
| dotenv-update-env | env-pairs, ?override | nil | Update env with env-pairs values. |
| dotenv-update-project-env | root-dir (str), | Updates project environment | |
| ?override | nil | with .env by a given root dir. | |
| dotenv-update-current-env | ?override | nil | Updates current project’s |
| environment (uses project.el to | |||
| locate the project root) |