How would I build a TOML config file system?

imagine having a custom toml/ini file for you elixir configs
example: my_iex.toml

title = "Elixir TOML config"

# this install deps for us once iex -S mix is ran
[custom_deps]
name = ecto_sql
version = custom | auto (automatically find the latest/best version)

# setting up database (config.exs) files 
[database]
sandbox = false
production = true
system = true

...

It would setup or make setting up our project for other people much easier, right?
I am quite familiar but also not that familiar with elixir, but do you guys - as some Elixir pros - have some advice of how to build something like this?
What do I need (custom libs, functions …), what do I have to take care about, what could be extremely hard …

Please don’t say something like
‘this already exists …’
‘thats boring …’

I want to do something like this for the experience …

That’s fair.

I’d start with a TOML parsing library like the top hit on hexdocs: toml

Then you would convert various parsed settings into their corresponding code inside mix.exs, I’d presume. That’s one – and the easier – option.

If you are very brave you can just reverse engineer how the normal mix.exs is being parsed and introduce code that does the same thing but without executing Elixir code and only by parsing TOML – tough stuff but not impossible. You did mention you want to gain more experience with Elixir so probably do not go this route as it involves basically contributing to core Elixir and that is not necessarily easy.

Just starting with Toml.decode and using the return value(s) to emulate what mix.exs does should be quite doable though.

1 Like

https://hexdocs.pm/toml_config/readme.html
That package should provide the functionality you need to get started. If you want to build it yourself look into the Config.Provider documentation and the toml lib posted above.

The biggest thing that jumps out: this would need to be able to run before dependencies are available (before they’re even downloaded) so you can’t just rely on a Hex package to parse TOML.

Providing a TOML config file for end users is a fine idea and much better experience for them than editing runtime.exs, if that’s the sort of app you’re building. You don’t really want them going into the release folder and you would have to manage syncing the file with each release.

If you want to parse the TOML yourself you could try doing it by hand or writing a parser with NimbleParsec. Then look at Config.Reader and Config.Parser.

You can find some inspiration in the vapor library.

2 Likes