Open An IEx Shell From An Elixir Script
I recently had occasion to want to start an IEx session from an Elixir script. Here’s how I was able to do it.
Method 1
Here’s a quick test script:
run.exs
:shell.start_interactive({IEx, :start, []})
System.no_halt(true)
System.no_halt(true)
is needed so that the interactive
session doesn’t die when the process running the script ends. You could
also use the elixir
command with the --no-halt
option instead to achieve the same effect.
When we run this script with just elixir
(instead of
iex
), we should still see an interactive IEx session
start.
$ elixir run.exs
Erlang/OTP 26 [erts-14.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]
Interactive Elixir (1.15.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Method 2
While researching, I found another way that works, but it’s even more obscure than the above method.
:user_drv.start_shell(%{initial_shell: {IEx, :start, []}})
I can’t find documentation for user_drv
, I had to browse
the code
directly.