from Hacker News

Lua for Elixir

by davydog187 on 5/13/25, 1:03 PM with 77 comments

  • by rickcarlino on 5/15/25, 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 interroboink on 5/15/25, 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 _acco on 5/15/25, 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 Philpax on 5/15/25, 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 davydog187 on 5/15/25, 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 gilgamesh3 on 5/15/25, 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 joshprice on 5/15/25, 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_hq on 5/15/25, 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 rdtsc on 5/15/25, 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 dlojudice on 5/15/25, 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/25, 5:42 PM

    I have been waiting for this my whole life
  • by dpflan on 5/15/25, 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 iLemming on 5/15/25, 5:12 PM

    Does that mean I can now write Fennel to target BEAM?
  • by binary132 on 5/15/25, 5:51 PM

    How sandboxed is BEAM really?
  • by codingbear on 5/15/25, 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 xvilka on 5/15/25, 2:15 PM

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

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

  • by behnamoh on 5/15/25, 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.