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: https://hex.pm/packages/dotenv_parser

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: https://gitlab.com/code-stats/code-stats/-/blob/d189c46930d1c352af8ca63027d56b346814ab32/lib/code_stats_web/config_helpers.ex

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

Where Next?

Popular in Libraries Top

Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Sub Categories:

We're in Beta

About us Mission Statement