sashaafm

sashaafm

Retrieving Mix configurations at compile-time

Hey guys, continuing from my other thread I’m still building and exploring some compile-time stuff that Elixir can provide.

Right now I’m trying to operate on the Mix config files at compile-time, but I keep getting nil values from the app keys. At first I thought that maybe the configs could only be read at runtime, but then I found this in the Mix.Config doc page:

Configuration set using Mix.Config will set the application env, so that Application.get_env/3 and other Application functions can be used at run or compile time to retrieve or change the configuration.

Is there a specific way to retrieve the config values at compile-time, then? I’ve been trying both:

inside a macro

defmodule A do
  defmacro test do
    IO.inspect Application.get_env(:app, :key)
  end
end

and just inside a module

defmodule A do
  IO.inspect Application.get_env(:app, :key)
end

but I always get nil :frowning2:. Any tips would be appreciated.

Marked As Solved

hubertlepicki

hubertlepicki

You must be doing something wrong. This should, and in my case - does work.

Check out this repo and run mix compile:

my results:

➜  compiletimeconfig git:(master) ✗ mix compile
Compiling 1 file (.ex)
"Hello"
Generated compiletimeconfig app

Which correctly prints “Hello” configured in config/config.ex

Here is the relevant commit:

Also Liked

sashaafm

sashaafm

Just looked at your repo and figured that I had the import_config command commented on my config.exs. I literally spent my whole afternoon trying to solve this. Even wrote a parser for the config file! :rage: :cry: :cry: :cry:

Thank you @hubertlepicki

hubertlepicki

hubertlepicki

yes, always tripple check if you do not have a typo or 1 character error before writing a parser of your own :wink:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement