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.

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:

2 Likes

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

1 Like

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

1 Like