[Proposal] Optional file imports in config.exs

31 views
Skip to first unread message

Aaron Renner

unread,
Feb 20, 2019, 5:55:38 PM2/20/19
to elixir-lang-core
Hi,

I'm curious what you'd think of having a variant of Mix.Config.import_config/1 that doesn't raise an error when the config file is missing. I'd like to be able to import optional configs, like config/prod.secret.exs if it exists, but not error if it's missing. Right now, I write my config/config.exs like this

# config/config.exs

use Mix.Config

config
:my_app, setting: true

#Import env specific config
import_config
"#{Mix.env()}.exs"

# Import secrets across all environments
try do
  import_config
"config.secret.exs"
rescue
 
Code.LoadError -> :ok
end

# Import env specific settings
try do
  import_config
"#{Mix.env()}.secret.exs"
rescue
 
Code.LoadError -> :ok
end


If you include a wildcard in the path to import_config/1 it doesn't raise an error if no files are found. What would you think about adding an additional argument to import_config that allows me to flag the import as optional? Then I could write my config like this.


# config/config.exs

use Mix.Config

config
:my_app, setting: true

#Import env specific config
import_config
"#{Mix.env()}.exs"

# Import secrets across all environments
import_config
"config.secret.exs", optional: true

# Import env specific settings
import_config
"#{Mix.env()}.secret.exs", optional: true


Any suggestions or different approaches I should try would be greatly appreciated!

Thanks,
Aaron Renner

José Valim

unread,
Feb 20, 2019, 6:03:08 PM2/20/19
to elixir-l...@googlegroups.com
A import_config_if_available has been proposed in the past (I believe by me) but rejected because you can do a "FIle.exists?" check before.

We are planning to slightly rework the configuration system for Elixir v1.9 due to releases so it may be that it makes the cut but for now I would wait until the discussion about configuration restart (I am siting on one or two drafts) or bring it up for rediscussion once v1.9 is out.


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/dca1f2df-aaf8-4967-b52b-d790fdad327b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron Renner

unread,
Feb 20, 2019, 6:10:31 PM2/20/19
to elixir-lang-core
Thanks for the quick response. I also tried it with File.exists?, but the pipeline seemed a little distracting.

# Import secrets across all environments
if "config.secret.exs" |> Path.expand(__DIR__) |> File.exists?() do
  import_config "config.secret.exs"
end

# Import env specific settings
if "#{Mix.env()}.secret.exs" |> Path.expand(__DIR__) |> File.exists?() do
  import_config "#{Mix.env()}.secret.exs"
end

I'm happy to wait for the configuration restart discussion or after Elixir 1.9.

Thanks again for your feedback!

Aaron Renner
Reply all
Reply to author
Forward
0 new messages