How to pass arguments to a module at compile time?

I’ve done some work on Phoenix plugs and they have the init() method that is used to pass arguments to the plug at compile time. How can I do that with my own modules? I know module attributes are evaluated at compile time…

Relatedly, how can I read config values that are set using the module name, e.g. like what is done in Phoenix with its web Endpoint.

config :my_app, MyAppWeb.Endpoint, [some: "thing"]

How do I get that “thing” from there? Is it as simple as Application.get_env(:my_app, MyAppWeb.Endpoint) ?

Many thanks!


This doesn’t happen because of anything the plug module is doing, it happens because of where the plug module is called. If you have

defmodule A do
  def hello, do: IO.puts "Hello World"

You can call this function either at runtime as normal, or you can have another module call that within its body, which means it gets called when compiling that other module:

defmodule B do