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

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
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
seancribbs
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
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 42633 214
New
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Sub Categories:

We're in Beta

About us Mission Statement