How do I make my first makefile?

Hello

I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But after two years, there is still so much to discover and knowledge I don’t have obtained yet.

I’m working on a private project now, using Elixir, but I find it kind of hard to learn this new step I want to implement in the private project: The setup of the Makefile

I looked at the Makefile of my job but this was such a huge difference with the examples I’ve came across on different sites. This makes it difficult to know what is necessary and what’s not.

The Makefile for my project should start the server, make sure the database is spinning, Docker containers are online and operating and that’s probably it for now.

How do I start? What’s important, what’s not?

Kind regards
Stef

Makefiles are for old timers, like me. Yes, it is powerful; however, it comes with a lot of sharp edges and Makefile beyond certain size will be hopelessly unmaintainable.

Before opening a big can of worms, please ask yourself: “Do I really need a Makefile?”

For a first Makefile, I’d keep it as thin task aliases rather than real build logic. Start with .PHONY targets like deps (mix deps.get), db (docker compose up -d db), setup (depends on db, then mix ecto.setup) and server (depends on db, then iex -S mix phx.server). Since Docker Compose already handles starting the containers, you can add a healthcheck to the database service and use docker compose up -d --wait so the server target only runs once Postgres is actually ready. Remember that recipe lines need tabs, not spaces. If it grows beyond a handful of targets, mix aliases in mix.exs are a good alternative since they stay in Elixir.

You are after a simple bash script. Makefile is an overkill and misuse for this kind of task.

If your goal is to write tasks to set your project up, and not to learn the quirks of make, then I can highly recommend mise and mise tasks.

I’ve found it more usefull than make, because I can pin the tool versions (like databases) and mise installs them. Two advantages over make are the arguments and flags and the task descriptions. No need for a help task with awk magic, which in my experience go stale after a while as the Makefile changes and the flags make the invoked scripts tidier.

Another option imo is Introduction - Just Programmer's Manual. It’s like a makefile, but tries to actually be a task runner not a build system.

Hello,

Since I’m still learning a lot about Elixir, I’m not educated enough to answer in a proper way.
It’s just what I know because of my job. :confused:

Sorry for the late response!

That does look interesting. I’ll take a look at it!

Well yes, for local development, I can spin up my docker container from docker desktop en just type in the run phx.server command and those would be the most important tasks.

Came here to say this. mise (née rtx) was first a version manager like asdf. Then they added environment management like direnv and tasks like make and now you have 1 quite good tool and its ecosystem (hk, usage and fnox) to standardize on.

Over time I’ve ported all of our internal projects to mise. One big win was in our infrastructure project, where I was using make, direnv and aws-vault, where I was able to consolidate to mise + fnox with much better integration.

Anyway, it’s banger, highly rec’d.

Yeah, I looked into mise and decided to implement it in my project!