johanvanzyl

johanvanzyl

Reading a list of strings and a number as config values from environment variable

I’m deploying my Phoenix app with edeliver and distillery and reading config values from environment variables. I can read normal strings just fine, but how do I handle 1.) a list of strings and 2.) numbers?

For 1, the list of strings:
In my config/prod.exs i have allowed_users: ["${ALLOWED_USERS}"],

Here is my sys.config file with the problematic variable:
allowed_users,[<<"${ALLOWED_USERS}">>]},

My environment variable being exported is (i’ve escaped the strings, but it’s not really helping):
export ALLOWED_USERS="\"john\", \"alice\""

When I don’t use an environment variable in my config file and just enter the values directly, then the compiled sys.config looks as follows, so that’s what I need:
allowed_users,[<<"john">>,<<"alice">>],

I can export the following export ALLOWED_USERS="john\">>,<<"alice" as the environment variable and that works, but it feels clunky.

Also, question 2. How can I get a number for a config value that is exported as an environment variable?
My config/prod.exs has this variable enable_salt_workers: "${ENABLE_SALT_WORKERS}", which
i export as export ENABLE_SALT_WORKERS=true, but it ends up as a string in sys.config as follows {enable_salt_workers,<<"true">>}, instead of {enable_salt_workers,true},

Any help would be appreciated.

Marked As Solved

sribe

sribe

Environment variables are always simple strings, nothing more. If you want to encode something more complex into them, then you’ll have to parse them after reading them.

If you have a lot of more complex “stuff” that you need to communicate via environment variables, then you might want to consider a string of JSON as the value…

Also Liked

OvermindDL1

OvermindDL1

This isn’t an Elixir thing, this just just how Environment Variables work regardless of any operating system I’ve seen, they are just a single string, not lists or numbers or anything of the form, hence why parsing is required regardless of the language.

sribe

sribe

Two things to keep in mind:

  1. mix.exs is not some simple template, it’s full-blown Elixir, so you can use the full language for parsing, including defp of some some helper functions, so enable_salt_workers: my_parse_env_list("${ENABLE_SALT_WORKERS}")

  2. All .exs files are run at compile time; so when you pull in env vars in prod.exs, you’re getting the ones on your dev system, not the ones from your production system. To actually use env vars loaded at launch instead of build, you have to read them in elsewhere, when your application is starting up–sounds like you may have already found some of the posts on how to do that.

johanvanzyl

johanvanzyl

Thanks @OvermindDL1, good to know.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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 t...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

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 29603 241
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement