mcarvalho

mcarvalho

System.get_env vs. Application.get_env

What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.

Most Liked

NobbZ

NobbZ

System.get_env/3 gets from the system environment, while Application.get_env/3 gets from the applications environment.

The first is defined by the means of your operating system, while the later is mostly configured during build using the file(s) in config and local to your application.

The application environment is described in the docs: Application — Elixir v1.20.2

The system-environment is not, since it is not any part of elixir or the BEAM. It’s just there…

zenw0lf

zenw0lf

Replies above explain the difference between Application.get_env and System.get_env.

As for best practices, have a look at this: Configuration Demystified | Blog · Elixir School

mbuhot

mbuhot

My rule of thumb is to only use System.get_env during application initialization, eg in your Application.start, Repo.init, Endpoint.init callbacks.

Once the application is initialized, the environment should always be queried by Application.get_env, since System.get_env can only return strings.

I like the dev / test environment to be statically defined in the config.exs files, only reading from the system environment for prod builds.

Last Post!

pmarreck

pmarreck

Ah… good point. a value retrieval is a value retrieval, whether it happens once (at build) or many times (at runtime) depends on the caller. Of course. :man_facepalming:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
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

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
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 44167 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement