Qqwy

Qqwy

TypeCheck Core Team

How exactly does Elixir start your code?

Something I found myself wondering about recently, is how exactly a bunch of .ex files is actually executed.

I know that, if you have created a Mix project, you can run your files using Mix, which will compile your files and its dependencies, and finally run YourAppName.Application.start/2 (How does Mix know where to find this? I presume the application section of the mix.exs file, is that correct?).

But what if I wanted to, for the sake of education, try to run a project without Mix, how would this be done? I guess we first use elixirc to compile the .ex files, but what then? Where exactly does something become an OTP application?

I am very interested to learn how this actually works under the hood! :smiley:

Most Liked

rvirding

rvirding

Creator of Erlang

I think one that is useful here is to understand what is going on at a lower level when you create applications and releases. While you will generally use tools like mix, or rebar3 in the erlang world, to do this, knowing what is going on will definitely help you in the long run, especially if you need to do something the tools don’t do.

Some interesting links are into the Erlang documentation about Applications and Releases. These are at the low level but they do show what is going on. A good book is of course Learn You Some Erlang which has chapters on Applications and Releases. The whole book is very good, even if it is about erlang :wink:, as it goes through the whole functional/concurrent/fault-tolerant bits in a very good way. There are deeper books if you want more about systems.

Yes, these books are all in Erlang but at this level the mapping erlang<->elixir is pretty straight-forward.

IMAO going from erlang to elixir is easier than going the other way as the erlang language is simpler doesn’t contain so many features as elixir. And no, I have no intention into getting into a pointless argument about this, people get so worked up and lose their sense of humour.

OvermindDL1

OvermindDL1

Which uses elixirc to compile them to beam files in the _build directory.

Exactly, erlang has a standard of how/where to find the applications (so it knows how to launch them) and those settings in mix create the file _build/dev/my_server/ebin/my_server.app, which is the standard erlang method of ‘describing’ an application. In this file it is a single erlang term (documented in the erlang reference manual if curious about details) of the form (in erlang syntax since that is what it is, so lowercase-starting identifiers are atoms) {application, my_server, Opts} where Opts is an erlang property-list of settings, the usual ones are {description, "The string description of it"} (that is an erlang string, so a char-list), and {modules, Modules} where Modules is a list of atoms that the application ‘contains’ and supplies to the system, as well as other Opts like {vsn, "0.2.0"} and others. The 2 important ones to make sure your thing actually starts up right (technically modules could be left empty, you will just have issues with upgrading/downgrading/hotswapping and such) are the mod option and the applications option. The {applications, Applications} is an list of Application atoms that should be started before this application, and the mod option is of the form {mod, {ApplicationModuleAtom, Args}} where ApplicationModuleAtom will be something like 'Elixir.MyServer', which is your use Application module where start is called. The Args here is passed as the second argument to your application’s start/2 function, it’s usually just nil (Erlang’s nil, the empty list []) traditionally, but can be quite useful for loading the same application multiple times in to a system with different options in each (this takes more finagling that mix does not support).

The BEAM will read that file for each application and load the applications in order so dependencies are properly resolved. :slight_smile:

But yeah, Mix generates this file for you based on what it scans of your modules (auto-filled in modules section is awesome!) and so forth. :slight_smile:

It becomes an OTP Application via the *.app file above, we had to define that file ourselves ‘in the old days’. :wink:

Eiji

Eiji

@Qqwy: Have you looked at a build utility that allows you to to use mix packages in an elixir script? I think that should be better than plain scripts without mix. For example if you have test.ex in your current directory then you can run mix_script compile test.ex and finally you will have test binary that you can execute anytime you want. mix_script will automatically generate and compile your project into binary, so you do not need to create whole project for simple learning 5-minute code.
Hope that it will help you :slight_smile:

notriddle

notriddle

Yes, exactly. It points at your app module, which is expected to comply with the Application protocol.

NobbZ

NobbZ

Of course there is nothing stopping you from doing it the erlang way. Except a small little thing… You need to resolve dependencies because of macros.

Then just compile your sources in correct order, moving the BEAM files into their correct location such that elixirc can pick them up to execute macros.

Of course you’ll need to install dependencies manually as well, and you will be able to write your *.app file manually as you did back in those days.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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

We're in Beta

About us Mission Statement