# How to use Lua packages in an embedded Lua script

**URL:** https://discourse.nixos.org/t/how-to-use-lua-packages-in-an-embedded-lua-script/78303
**Category:** Guides
**Created:** [June 16, 2026, 12:54am UTC](https://discourse.nixos.org/t/how-to-use-lua-packages-in-an-embedded-lua-script/78303 "2026-06-16T00:54:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rhendric](https://discourse.nixos.org/user_avatar/discourse.nixos.org/rhendric/32/5699_2.png) [@rhendric](https://discourse.nixos.org/u/rhendric)
#### Post date: [June 16, 2026, 12:54am UTC](https://discourse.nixos.org/t/how-to-use-lua-packages-in-an-embedded-lua-script/78303/1 "2026-06-16T00:54:54Z")

</div>

The Nixpkgs-endorsed way to write an executable Lua script is `writers.buildLua`; this accepts a `libraries` argument and gives the produced script a shebang to a Lua interpreter that has been wrapped to include the provided libraries in its search path.

This tip is for when you want to write a Lua script for an application that embeds Lua. In that case, wrapping the Lua interpreter is not possible. So how do you use packages from `luaPackages` in your script?

Here’s a solution I found. In this example, I wanted to add a script to Home Manager’s [`programs.mpv.scripts`](https://nix-community.github.io/home-manager/options.xhtml#opt-programs.mpv.scripts) that would use `luaPackages.ldbus`. The Lua package system exposes two variables that it uses to search for packages: `package.cpath` and `package.path`. In the general case, both must be updated, and they can be updated from within the Lua script. I found two helper functions in `luaPackages.luaLib` for formatting the search strings. Here is what I use as the text of my script:

```nix
with pkgs.luaPackages; ''
  package.cpath = '${luaLib.genLuaCPathAbsStr ldbus};' .. package.cpath
  package.path = '${luaLib.genLuaPathAbsStr ldbus};' .. package.path

  -- My use case: use ldbus to send a D-Bus message when mpv is in
  -- fullscreen mode to a service I wrote that dims the lights in the
  -- room.
  local ldbus = require 'ldbus'
  local conn = ldbus.bus.get 'session'

  mp.observe_property('fullscreen', 'bool', function (prop, val)
    if val then
      cmd = 'Dim'
    else
      cmd = 'UnDim'
    end
    conn:send(ldbus.message.new_method_call('...', '...', '...', cmd))
  end)
''

```

(Post better solutions if you know them! I’m only a dabbler in Lua.)

---

<div class="post-metadata">

### Author: ![mightyiam](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mightyiam/32/33998_2.png) [@mightyiam](https://discourse.nixos.org/u/mightyiam)
#### Post date: [June 17, 2026, 4:17am UTC](https://discourse.nixos.org/t/how-to-use-lua-packages-in-an-embedded-lua-script/78303/2 "2026-06-17T04:17:46Z")

</div>

Dabblers posting “here’s what worked for me 🤷” is awesome. Thank you.
