A souped-upped version of what you've come to expect in a .env
file loader, as per the 12-factor app methodology for configs. What's different? dotenv
expects the env files to be in the amazing EDN format for Clojure apps. Long story short: You've got a Clojure map as your config.
Store your configuration in an env file, and keep it checked out of source control. All there is to it.
Include the following dependency in your project.clj
file:
:dependencies [[com.knrz/dotenv "0.1.0"]]
It's simpler than you think!
(require '[dotenv.core :refer (env)])
;; env is now a map of every. single. environment. variable. simple.
;; Check out the Read This section for stuff you should read.
If you ever need to reload the env, a simple call to dotenv.core/get-env
reloads everything. For example:
(require '[dotenv.core :as dotenv])
(def env (atom dotenv/env))
(defn get-env []
@env)
(defn reload-env! []
(reset! env (dotenv/get-env)))
(P.S. Experienced Clojurians, let me know if I've done anything wrong in the above example.)
The env map contains any variables set from the following sources, with later sources overwriting earlier ones:
(System/getenv)
. Values are strings.(System/getProperties)
. Values are strings..env
file in root project directory. Values are whatever they are in edn..env.(current-env)
file in the root project directory, say,.env.development
. Values are whatever they are in edn.
For convenience's sake, all env var names are normalized. They are lowercased, underscores (_
) and periods (.
) in variable names are replaced by dashes (-
), and keywordized. For example, if you were to echo $HOME
in bash, you'd (println (:home env))
with dotenv. Whereas you would System.getProperty("user.dir")
in Java, you would (:user-dir env)
with dotenv.
current-envis a string resolved through the
LEIN_ENVor
BOOT_ENVenvironment variables. It defaults to
"development"`.
The :current-env
key houses the current environment. Also, :(current-env)?
(e.g. :test?
) is set to true
for the current environment.
This was born out of frustration with not understanding how environ, another environment management system, worked. That, and its interop with Leiningen had a certain bug I can't seem to remember.
Yes.
The standard MIT License, y'all.