DidactMacros

DidactMacros

I'm looking for a thorough delineation regarding the use of the Config module and related directories

I’ve looked in Programming Elixir, and Elixir in Action as well as on this forum (even looking at proposals), in manuals and via search engines; I’ve not really found a sizable body of writing that is dedicated to “config”.

Is there any source that covers how to best format config files, what not to put in them, how to know what mix environments’ responsibilities are (regarding config), how and when to override default environment paths, where to place configuration files (apart from /config) as part of convention, and how import_config works with various args?

Apologies if I missed this material somewhere, but I have search up and down for a couple of days. What I have found is that understanding this aspect of app development in elixir can be critical later on with more advanced collaborative projects.

Marked As Solved

Eiji

Eiji

Not sure about this, but maybe it’s just me. I’m really quickly learning new things. Simply when I saw in example some_function_call() then I look for it in documentation when needed. Personally I never had a problem understanding the config.


So you were looking at wrong documentation. Your mistake is that you were looking at details (here config) without thinking about general things (here application). Look at the top of mix.exs and it suddenly becomes clear (i.e. use Mix.Project). :wink:

The config.exs is compile-time and code compilation is part of building your application. The building tool is mix and that’s why there’s more documentation. For example you can see environments section of Mix documentation.

Link to all Elixir’s core applications are here:


Could you please give some link? You may saw some old code for other version of Elixir core. Unfortunately macros are more rarely documented. Because of the quoted expressions there are less pattern-matching simply because for example list literals and the ones created using sigil_w have a different AST (Abstract Syntax Tree) representation and simply is_list/1 check may fail for them and also for example variables.

The current documentation uses Path.expand/1 which only accepts Path.t which points to IO.chardata(), so currently atoms does not work.

iex> Path.expand(:example)
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1

The following arguments were given to IO.chardata_to_string/1:

# 1
:example

Attempted function clauses (showing 2 out of 2):

def chardata_to_string(string) when is_binary(string)
def chardata_to_string(list) when is_list(list)

(elixir 1.16.0) lib/io.ex:687: IO.chardata_to_string/1
(elixir 1.16.0) lib/path.ex:818: Path.expand_home/1
(elixir 1.16.0) lib/path.ex:180: Path.expand/1
iex:1: (file)

Also Liked

tfwright

tfwright

Theoretically Home · Elixir School is where such a resource would be found if it existed. Maybe an effort could be organized to submit something. I certainly agree it is an important piece that deserves a comprehensive overview, especially with confusion around “config” vs “runtime”…

ityonemo

ityonemo

thing to know is that there are two important files:

  • config.exs which is compile-time configuration
  • runtime.exs which is run-time configuration

Often config.exs will be set up to punt to “.exs” but this must be explicit. Note that configuration is overridden “as written” so if something is set in test.exs it can be overridden by anything after your import_config call.

Also note that if you’re writing a library, all the stuff in your config.exs will NOT be called when your library is being compiled by your library’s user.

danj

danj

There are a bunch of mechanisms but the most core problem is configuration in deployment. However, I have found the best approach is to tackle that problem first then work backwards to development. Of course starting at the end isn’t possible when you’re just starting with all of this.

The most common deployment model is containers, but what I’m going to describe works well with any model of deployment and in development, the benefit there being a single mechanism can be used and tested through the entire life cycle.

There are two ways to inject deployment time data into a container, files and the environment. I’ve tried the first and I don’t recommend it. Getting a file injected in a container is a pain. I’ve been using the environment in combination with some assistance from a package (I wrote) Jetenv — Jetenv v0.1.1
which gives full access to the entire application environment and supports types and complex values.

With this approach you can configure any part of your application with predictable names and have full visibility into what’s being configured. It works well with secret managers since they work best with env vars. You can inject multi-line strings (think certificates) as well. And the same approach, though with different values typically, works in development. Source an env file and you’re ready to go. This is also nice because it provides a template for devops in building the deployment environment.

sodapopcan

sodapopcan

Are you asking in general or do you have an example of a language/framework that has something along the lines of what you’re looking for?

Eiji

Eiji

Hmm … config is just … config, so “not much” happens in them. It’s almost as same as json or yaml configuration files except that you can add some extra Elixir code to them. The biggest thing about config files is how they works across environments and that’s almost all. All the basics are covered in Config documentations.

Do you mean code formatting? It’s as same as in any other source code file, see the documentation for mix format task.

Almost everything - it’s easier to say what to put to them. Basically all you need is fetching the OS environment variables and using config_env() - both are well covered in Config documentation.

What “responsibilities” are you referencing to? Simply read the documentation what you are able to configure and use the ones you need. There are no “business logic” inside config or something like that. Simply configure your app and dependencies as you do in json or yaml files, but with a bit more of flexibility for example to fetch some data (like above in OS environment variables) and use them for configuration.

Best practice is rather to follow community standards as it’s easier for others to read your code. However nobody is limiting you what and how you do in your pet projects. If you want name them as you want. The default ones should be clear enough, so most people simply don’t touch configuration file names i.e. there is no need to do that.

Elixir does not have a framework which forces you to do something in specific direction, but gives you a good defaults. As above usually nobody is changing them. They are in config directory in both pet and huge business projects as keeping them in well known default location makes the most sense.

Simply you are passing a file name of file you want to import. There is no magic here except that you cannot import things in runtime.exs (also explained in Config documentation).

The materials are created when needed. If someone is asked to deploy a phoenix app to AWS then having production knowledge the tutorial is shared. Sometimes people have a problem finding something in documentation, but as above changing config naming is not common. Not much people were ever thinking about that and therefore there are not much resources about it. Same is for a database. By default in most projects PostgreSQL is more than enough. You are free to learn about other databases, but again if you don’t need it then you don’t have to learn about it.

Yes it is, but not so detailed. As long as you follow documentation and forum you should be fine. For most cases there are tools to help you like credo.

Possibly related:

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement