Lua for Elixir

by davydog187on 5/13/2025, 1:03 PMwith 81 comments

by rickcarlinoon 5/15/2025, 2:22 PM

We used Luerl, the underlying Lua engine, in production for years as a sandboxed scripting environment for FarmBot devices back in the day. Users could execute scripts directly on the device to control peripherals. It was a solid library and the community support was great. From an ergonomics perspective, developers preferred this approach over API calls. I am surprised more projects don’t provide a Lua scripting layer.

by interroboinkon 5/15/2025, 6:31 PM

  This is not embedding the C Lua runtime and compiler, but rather a
  complete implementation of Lua 5.3. This feat is made possible by the
  underlying Luerl library, which implements a Lua parser, compiler,
  and runtime, all in Erlang.
Okay, that's actually pretty cool (:

Also a sign of Lua's maturity and popularity, that it's got various independent implementations (LuaJIT, this one, perhaps others I don't know about).

by _accoon 5/15/2025, 7:17 PM

This is so cool. A key benefit is that it's not embedding the C Lua runtime and compiler, but rather implements Lua in the host language (Elixir/Erlang).

When sandboxing user code in another runtime, you need to serialize the data to and from that runtime. That comes with a performance penalty.

So, for example, if you sandbox code in WASM, you need to pick a transport data format, like JSON. You need to serialize Elixir data structures into JSON, send it to WASM, and then deserialize the result. For a high-performance data pipeline, this adds up!

But if your sandbox is in the host language, no serialization/de-serialization is required. You can execute the sandboxed language in microseconds.

I wrote more about this here: https://blog.sequinstream.com/why-we-built-mini-elixir/

Wish this library existed just a couple months ago!

by Philpaxon 5/15/2025, 2:22 PM

Very nice, but it is confusing to name a library for using a language the same thing as that language. I suppose this is meant to be a temporary state of affairs while Lua (the library) gets merged into Luerl, but I would have personally called it Luerl++ or such.

by davydog187on 5/15/2025, 3:43 PM

If you're looking for a cool example of Lua running on the BEAM, check out the creator of Luerl (Robert Virding) space ship demo

https://github.com/rvirding/ship-demo

by gilgamesh3on 5/15/2025, 3:55 PM

Cool project, congrats.

I am trying to understand why would anyone prefer to use Lua to create script instead of Elixir, which supports running scripts. While Lua has lots of users the language just have too many wrong design choices. If I had the choice between Elixir and Lua for scripts I would use Elixir every time.

by joshpriceon 5/15/2025, 2:04 PM

This is awesome! Can't wait to find some use cases to embed a sandboxed and safe mini Lua language in our apps.

https://hexdocs.pm/lua/Lua.html

by pentacent_hqon 5/15/2025, 6:38 PM

I've been considering adding Lua support to Keila for a while already. It seems like a perfect fit for when I'm going to implement email automation and want to allow custom business logic. This would certainly make that plan easier. Thanks OP for sharing the library with the community!

by rdtscon 5/15/2025, 6:04 PM

davydog187, I just wanted to thank you for stepping up and lending Robert a hand. It's nice to see new releases and lots of cleanups happening in Luerl. The project is a real gem and it's nice to see new activity in it!

by dlojudiceon 5/15/2025, 8:59 PM

Fun fact (probably well-known fact for HN audience): Both Lua and Elixir were created by Brazilians. Lua by Roberto Ierusalimschy and team at PUC-Rio in 1993, and Elixir by José Valim in 2011

by jkaufmann_on 5/15/2025, 5:42 PM

I have been waiting for this my whole life

by antfarmon 5/22/2025, 8:46 AM

Lua on the BEAM - Dave Lucia & Robert Virding | Code BEAM Europe 2024

https://www.youtube.com/watch?v=4YBBoXXH_98

by rvirdingon 5/19/2025, 2:25 PM

The reason for calling it Luerl was that it was/is an implementation of Lua on Erlang. It is Dave who has been working with a more serious Elixir interface. The original one, which still exists, had a very simple Elixir interface module which just flipped the argument ordering to calls to be more Elixiry in feel.

by dpflanon 5/15/2025, 4:00 PM

Is this influenced by "Embedding Python in Elixir"?

https://dashbit.co/blog/running-python-in-elixir-its-fine

It certainly is more attractive in implementation. Well done!

by iLemmingon 5/15/2025, 5:12 PM

Does that mean I can now write Fennel to target BEAM?

by binary132on 5/15/2025, 5:51 PM

How sandboxed is BEAM really?

by codingbearon 5/15/2025, 4:41 PM

what is the Lua equivalent for JVM ecosystem? An embeddable language which you allows for running user scripts in a sandbox?

by xvilkaon 5/15/2025, 2:15 PM

There is also Lua implementation in safe Rust - Piccolo[1]

[1] https://github.com/kyren/piccolo

by behnamohon 5/15/2025, 2:17 PM

Recently, I had a simple Flask project, just a single Python file, and wanted to convert it to Elixir using AI. But what I got was a sprawling directory of modules and files in Elixir. Apparently, that’s just how things are done in the Elixir world. That moment made me realize why some languages catch on while others struggle. If spinning up a basic web server already feels like jumping through hoops, I can’t imagine the overhead for more complex projects.

I'm sure there are people who think Elixir/Phoenix >> <other-solutions>, but for me it was a nightmare.

OTOH, the language is beautiful as it's macros all the way down.