Mix project for a simple script

So, I am using elixir for the advent of code daily challenges. Initially I just had a simple .exs file and I would just invoke it with elixir <file-name> (or with #!/usr/bin/env elixir as the first line). That worked fine.

Ok, fast forward to more complex scripts and now I need to use some external packages (say, the Heap package).

I naturally switched to mix so I can cleanly define my dependencies in my mix.exs file. I created a simple app without supervisor (mix new day17)

I am running into some issues running my script.

Now imagine my script is a simple IO.puts(:hello) (in the default lib/p.exs, outside of a defmodule).

If I leave it as is, mix will execute this during its compile phase, so calling mix run will indeed execute that IO.puts. Unfortunately calling mix run a second won’t (because it’s already compiled).

So if I want mix run to always run my code, I guess I have to define a proper app in my mix.exs. Easy. I defined a start/2 function in the P module file (and moved my IO.puts in there) and added mod: {P, []} to the def application section in mix.exs.

This works (and it will properly execute my IO.puts whenever I invoke mix run), except that it fails at the end because it expect my start function to return the pid of a forked process, etc… the whole supervisor tree thing, which is definitely not what I want for my simple script.

Am I missing something obvious here ? (yes, I could also use iex -S mix and then invoke something like P.start manually, and then c("lib/p.ex") whenever I change anything in my file, but that seems quite tedious)

Not a response to your ultimate question, but if this is all you need then Mix.install solves that problem without needing any other changes to your script.

Right, I actually did try that (should have specified this in my post). And you are right. It does work (the script executes properly)

The problem there is that the editors I tried (vscode, nvim) don’t give me intellisense for the dynamically imported packages (say the Heap package), which sucks. This indeed would have been the simplest solution…


Have you tried livebook?

My solution for this was to define a test that runs my “entry point function” and use mix test to execute it whenever I wanted. There is also a section about running mix test with a filesystem watcher if you want to have even less manual work to do. You can also only run stale tests so you can use your one mix project for all the days and just run the test case(/-s) related to your current day.

Not exactly an answer to your question, sorry, but might be helpful either way… :sweat_smile:

actually, this is pretty good. It might not be perfect, but it does the job for sure.

thanks a lot IloSophiep

1 Like

D4no0: I haven’t had a chance to try lovebook yet. Although I keep hearing about it. I was trying to see if a simpler local solution would work or try to figure if I was completely misusing mix. Thx.