Home Explore Blog CI



nixpkgs

3rd chunk of `doc/languages-frameworks/lua.section.md`
6adee0563279f6739541d1511746fc33971095cb32e54f17000000010000090f
You can try converting luarocks packages to nix packages with the command `nix-shell -p luarocks-nix` and then `luarocks nix PKG_NAME`.

#### Packaging a library manually {#packaging-a-library-manually}

You can develop your package as you usually would, just don't forget to wrap it
within a `toLuaModule` call, for instance

```nix
{
  mynewlib = toLuaModule (
    stdenv.mkDerivation {
      # ...
    }
  );
}
```

There is also the `buildLuaPackage` function that can be used when lua modules
are not packaged for luarocks. You can see a few examples at `pkgs/top-level/lua-packages.nix`.

## Lua Reference {#lua-reference}

### Lua interpreters {#lua-interpreters}

Versions 5.1, 5.2, 5.3 and 5.4 of the lua interpreter are available as
respectively `lua5_1`, `lua5_2`, `lua5_3` and `lua5_4`. Luajit is available too.
The Nix expressions for the interpreters can be found in `pkgs/development/interpreters/lua-5`.

#### Attributes on lua interpreters packages {#attributes-on-lua-interpreters-packages}

Each interpreter has the following attributes:

- `interpreter`. Alias for `${pkgs.lua}/bin/lua`.
- `buildEnv`. Function to build lua interpreter environments with extra packages bundled together. See section *lua.buildEnv function* for usage and documentation.
- `withPackages`. Simpler interface to `buildEnv`.
- `pkgs`. Set of Lua packages for that specific interpreter. The package set can be modified by overriding the interpreter and passing `packageOverrides`.

#### `buildLuarocksPackage` function {#buildluarockspackage-function}

The `buildLuarocksPackage` function is implemented in `pkgs/development/interpreters/lua-5/build-luarocks-package.nix`
The following is an example:
```nix
{
  luaposix = buildLuarocksPackage {
    pname = "luaposix";
    version = "34.0.4-1";

    src = fetchurl {
      url = "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/luaposix-34.0.4-1.src.rock";
      hash = "sha256-4mLJG8n4m6y4Fqd0meUDfsOb9RHSR0qa/KD5KCwrNXs=";
    };
    disabled = (luaOlder "5.1") || (luaAtLeast "5.4");
    propagatedBuildInputs = [
      bit32
      lua
      std_normalize
    ];

    meta = {
      homepage = "https://github.com/luaposix/luaposix/";
      description = "Lua bindings for POSIX";
      maintainers = with lib.maintainers; [
        vyp

Title: Manual Lua Packaging, Lua Interpreters, and `buildLuarocksPackage`
Summary
This section covers manual packaging of Lua libraries using `toLuaModule` and `buildLuaPackage`. It then transitions to Lua interpreter versions (5.1, 5.2, 5.3, 5.4, and LuaJIT), their locations in Nixpkgs, and attributes such as `interpreter`, `buildEnv`, `withPackages`, and `pkgs`. Finally, it introduces the `buildLuarocksPackage` function with an example showing how to use it to package `luaposix`.