sen

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.

dev.exs
config :myapp, :enable, value: “${enable}”

Showing Posts 1 to 10

chensan

chensan

I don’t remember there’s such thing like ${} in Elixir, you might mean #{} for string interpolation?

You can use System.get_env("enable") to get environment variable in your dev.exs file.

sen

sen OP

Hi chensan,
Thanks for help.
seen like this , currently i am keep trying.
http://engineering.avvo.com/articles/using-env-with-elixir-and-docker.html

chensan

chensan

That post is using Exrm for building OTP release, that’s why it’s using ${}, also exrm is deprecated now. Since you’re configurating your dev.exs, there’s no need to use exrm, just use System.get_env.

pedromvieira

pedromvieira

Check out this guide Kubernetes Native Phoenix Apps.

I’ve had to adapt it a little to my umbrella use case but is really good and up to date.

dersar00

dersar00

Hi, @sen!
For using environment variables you can create a .env file anywhere in your project folder, I prefer to create it in project_name/config folder, with a content like here:

export FILE_FOLDER='/home/username/file_folder'
export FIRST_VARIABLE='some_var'

after saving file, source it using source config/.env from project directory, then u can use those variables in your project using commmand:
System.get_env("FILE_FOLDER")
Good luck!

IMPORTANT: don’t add spaces before and after equal sign.

10
Post #5
dimitarvp

dimitarvp

I am very strongly interested in a blog post about this, if you decide to write one!

sen

sen OP

Thanks all.
I am doing like this, when i build and run docker after, can get the value in HomeController test.
But endpoint and route nothing to change, cant get the value.
I want to enable/disable plugin. if true will enable that plugin.
Anyone know the reason? if cant doing like this, i will change other way.

prod.exs
config :myapp, :enable, value: “${ENABLE}”

endpoint.ex
if Application.get_env(:myapp, :enable)[:value] do
disable/enable plugin…
end

Josh

Josh

I’m a little confused about the best way to store secrets.

When I looked for a way to automatically load a .env file, I found a dotenv package that says “This isn’t the Elixir way.”

The tutorial I read said to hard-code the Guardian secret in config/config.exs, but that doesn’t seem right.

I was going to do this, but I’m wondering if it isn’t a good idea because of the message in the README of that dotenv package:

# config/config.exs
config :app_name, AppName.Guardian,
       issuer: "app_name",
       secret_key: System.get_env("GUARDIAN_SECRET")

I could manually source config/.env before starting the server, but I’m probably going to forget to do that sometimes.

It’s confusing, because prod.secret.exs also uses System.get_env("DATABASE_URL"). If secrets were meant to be hard-coded there, why would it look for environment variables by default and/or not be excluded automatically in .gitignore?

Does anyone know of an automated way to load the .env file that still follows “the Elixir way”?

Josh

Josh

I don’t know if this is the correct solution, but it looks like development keys are meant to be hard-coded into version control, like the default secret_key_base in config/config.exs? I usually create a .env for development when using other languages, but after looking through all the config files, it looks like the default setup will override the default config.exs settings (and dev.exs isn’t loaded) when you’re running in production.

(I’m guessing that the tutorial I read probably should have recommended putting the Guardian key in dev.exs instead of config.exs.)

If I’m mistaken, please let me know. :slight_smile:

BrightEyesDavid

BrightEyesDavid

@Josh, here’s what I’m doing for now, which I’m happy with so far. :slight_smile:

Development

I find text files convenient in dev, and don’t want to have to export env vars every time I run the app locally.

config/dev.secret.exs contains dev secrets that shouldn’t be in version control. (secret_key_base keys could be moved into this file, but I’ve left mine in dev.exs for now as it’s only for signing and encrypting in dev while testing.)

.gitignore contains **/*.secret.exs so that ‘secret’ files are kept out of Git version control.

Production

I use Mix releases and use environment variables via systemd, and so keep the System.get_env(ENV_VAR) calls.

Important to note is that there’s a new recommended way of handling run time (as opposed to compile time) config in Elixir >= 1.9: the Config module. The whole page is worth reading, but here’s the upshot:

The Config module in Elixir was introduced in v1.9 as a replacement to Mix.Config , which was specific to Mix and has been deprecated.

You can leverage Config instead of Mix.Config in two steps. The first step is to replace use Mix.Config at the top of your config files by import Config .

In Phoenix, rename prod.secret.exs to the special filename releases.exs.

Then, the environment variables read in this file are read at run time on the server, and not when building (perhaps locally).

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews