How to run functions from dev.exs

I’m trying to run a function in one of my config files, in this case dev.exs, which will return the IPv6 of one of my network interfaces. This is not working (highlighted yellow):


although I know the function does work.

I’ve also put the function into a module and placed the module in the config folder, and the lib folder, but they weren’t able to be found by dev.exs either. So how and where do I define a function or module, so that it can be used in dev.exs?

Separately, is there an easier way to find the IPv6 of my “tun0” interface, than this module/function?

defmodule GetIp do

  def get_ygg_ip do
    :inet.getifaddrs() 
    |> elem(1) 
    |> Enum.filter(fn x -> elem(x, 0) == ~c"tun0" end) 
    |> Enum.at(0) 
    |> elem(1) 
    |> Enum.at(1) 
    |> elem(1)
  end

end

if you want to test the function you will have to rename ~c"tun0" to something the exists on your computer when using ifconfig (leftmost column ahead of the colons are the interface names)

It is not meant for that. dev.exs is a configuration file and not somewhere you write functions to be called from elsewhere. It is executed at compile time.

If you really want to go down this path, you can assign it to a configuration variable config :test2, someip: ... and retrieve it later with Application.compile_env(:test2, :someip).

https://hexdocs.pm/elixir/Application.html#compile_env/3

1 Like

Config files are executed before compilation, so your code does not exist when those files are evaluated.

Except for the runtime.exs file that is executed on application boot. So in that file you can call your modules. But note that your application will not be started yet so you can only call pure functions. get_ygg_ip looks fine in that regard.

1 Like