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

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is 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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement