Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to "source .env" in Elixir to "load" Environment Variables from .env file? #18

Closed
nelsonic opened this issue Apr 9, 2018 · 6 comments

Comments

@nelsonic
Copy link
Member

nelsonic commented Apr 9, 2018

Scenario:
we have an .env file that contains several environment variables.
We want to run the equivalent of

source .env

but from within our config.exs file, so that it's run each time the app is started
(such that the source .env command does not need to be run manually)

We tried the following three variants:

System.cmd("source", [".env"])
:os.cmd("source .env")
"source .env" |> String.to_charlist |> :os.cmd

Sadly none of them worked for setting the environment variables.

Ended up cobbling together the following code:

try do
  File.stream!("./.env")
    |> Stream.map(&String.trim_trailing/1) # remove excess whitespace
    |> Enum.each(fn line -> line           # loop through each line
      |> String.replace("export ", "")     # remove "export" from line
      |> String.split("=")                 # split on the "=" (equals sign)
      |> Enum.reduce(fn(value, key) ->     # stackoverflow.com/q/33055834/1148249
        System.put_env(key, value)         # set each environment variable
      end)
    end)
rescue
  _ -> IO.puts "no .env file found!"
end

Which does work.
But it so many lines of code ...

Does anyone know why the source .env does not work?

@nelsonic
Copy link
Member Author

Considering just "porting" over env2 to elixir ... https://github.com/dwyl/env2/blob/master/lib/env.js
🤔

@curioustushar
Copy link

@nelsonic check few already available options https://github.com/avdi/dotenv_elixir https://github.com/BlakeWilliams/envy .

@nelsonic
Copy link
Member Author

@cse-tushar thanks.
had a look at the options ...
I think the best course of action might be to use the "Elixir Way" ... 🤔
image
That way I'm not encouraging others to "bend" Elixir/Phoenix to do something it's not meant to do ...

@hipertracker
Copy link

OptionParse can handle ini files including .env. You only need add the section to the file. Like in this example where I am using .env file to set the database in Phoenix. https://gist.github.com/hipertracker/5338d75e220b200dc92f6be3af65f5b5

@nelsonic
Copy link
Member Author

nelsonic commented Mar 1, 2020

@hipertracker that's a good gist for the DIY approach. 👍
Going to try envy on our current project to simplify development. Thanks @curioustushar 👍

@nelsonic
Copy link
Member Author

https://github.com/dwyl/envar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants