Nicd

Nicd

DotenvParser - A simple library to parse .env files

I was refactoring my project to use config/runtime.exs to make the config more streamlined and allow configuring (nearly) all things with environment variables. This is fine for production where I define the variables in the Systemd unit file, but in development I don’t want to waste time having to set all of them or remembering to source them from somewhere.

So I decided to write a simple .env file parsing library so I can do this on top of my config file:

if Config.config_env() == :dev do
  DotenvParser.load_file(".env")
end

Now for developers, all the vars are nicely grabbed from the .env and they can be used in the runtime config (or inside the application code) with System.get_env/2. When execution ends, they are no longer set, so they don’t pollute your shell environment.

The library supports both uppercase and lowercase variable names, quoted values to prevent trimming of whitespace, comment lines and inline comments, double quoted values to unescape some escape codes like \n or \uXXXX, and lines that start with export (in case you want shell compatibility – but note that the escapes may be different).

Link to library: dotenv_parser | Hex

License: MIT (+ BSD 3 clause for one test file)

Most Liked

Nicd

Nicd

Some prior art, some of them I looked through and some I didn’t know of before this moment:

dotenv – I figured it was abandoned but in fact at pretty much the same time as I made my own, it has reactivated and pushed a new version. :smiley: Has a server mode with a reload function to reload values when they have been changed. Supports referring to previous variables in subsequent ones, which mine doesn’t. Doesn’t seem to have any unescape support?

envy – Puts values directly to system env (no possibility to return them). Doesn’t do unescaping. Has a Mix-based config reload system.

stillir – In Erlang, I don’t know how to use it.

exenv – Has different adapters and encryption support.

Nicd

Nicd

Thanks to a suggestion by @axelson, DotenvParser 2.0.0 now supports multiline environment variables. To see all supported formats, it’s probably easiest to look at the test file: test/data/.env · 735e1867f69d94da8abb68ad283a766a7c2efa56 · Mikko Ahlroth / dotenv-parser · GitLab (though the documentation lists supported escapes in double quoted env vars).

Nicd

Nicd

Serving suggestion

Since environment variables are always strings, so I wrote this helper function to grab variables and cast them to the correct type: lib/code_stats_web/config_helpers.ex · d189c46930d1c352af8ca63027d56b346814ab32 · CodeStats / code-stats · GitLab

You can see it in action in the aforementioned config/runtime.exs file.

Where Next?

Popular in Announcing Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19460 194
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement