Skip to content

pkulev/dotenv.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenv.el — Plugin for loading .env project files.

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 ;-).

Installation

use-package

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))

Examples

Note that in the following examples installation-related use-package clauses are omitted.

Using built-in Emacs project manager

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))

Overwrite existing environment variables

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))))

Integration with projectile

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)))))

Transform variable name or contents using dotenv-transform-alist

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)))))

API reference

Customizable variables

nametypedescription
dotenv-file-namestringDotenv file name (default: .env).
dotenv-transform-alistalistList of predicate-transform pairs for
custom key or/and value processing.

Public functions

nameargumentsreturnsdescription
dotenv-loadabs-path (str)list of (k v) pairsLoad .env from file.
dotenv-load-dirroot-dir (str)list of (k v) pairsLoad .env from file in root-dir.
dotenv-getkey (str), path (str)stringReturns a string value by a key.
dotenv-update-envenv-pairs, ?overridenilUpdate env with env-pairs values.
dotenv-update-project-envroot-dir (str),Updates project environment
?overridenilwith .env by a given root dir.
dotenv-update-current-env?overridenilUpdates current project’s
environment (uses project.el to
locate the project root)

About

Dotenv - Emacs plugin for loading project's .env file.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors