Home Explore Blog CI



nixpkgs

4th chunk of `doc/languages-frameworks/lua.section.md`
cd46fce9cdb15b9be998d06c0cd4610cdf77c259d5f77c060000000100000d15
- `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
        lblasc
      ];
      license.fullName = "MIT/X11";
    };
  };
}
```

The `buildLuarocksPackage` delegates most tasks to luarocks:

* it adds `luarocks` as an unpacker for `src.rock` files (zip files really).
* `configurePhase` writes a temporary luarocks configuration file which location
is exported via the environment variable `LUAROCKS_CONFIG`.
* the `buildPhase` does nothing.
* `installPhase` calls `luarocks make --deps-mode=none --tree $out` to build and
install the package
* In the `postFixup` phase, the `wrapLuaPrograms` bash function is called to
  wrap all programs in the `$out/bin/*` directory to include `$PATH`
  environment variable and add dependent libraries to script's `LUA_PATH` and
  `LUA_CPATH`.

It accepts as arguments:

* 'luarocksConfig': a nix value that directly maps to the luarocks config used during
  the installation

By default `meta.platforms` is set to the same value as the interpreter unless overridden otherwise.

#### `buildLuaApplication` function {#buildluaapplication-function}

The `buildLuaApplication` function is practically the same as `buildLuaPackage`.
The difference is that `buildLuaPackage` by default prefixes the names of the packages with the version of the interpreter.
Because with an application we're not interested in multiple version the prefix is dropped.

#### lua.withPackages function {#lua.withpackages-function}

The `lua.withPackages` takes a function as an argument that is passed the set of lua packages and returns the list of packages to be included in the environment.
Using the `withPackages` function, the previous example for the luafilesystem environment can be written like this:

```nix
lua.withPackages (ps: [ ps.luafilesystem ])
```

`withPackages` passes the correct package set for the specific interpreter version as an argument to the function. In the above example, `ps` equals `luaPackages`.
But you can also easily switch to using `lua5_1`:

```nix
lua5_1.withPackages (ps: [ ps.lua ])
```

Now, `ps` is set to `lua5_1.pkgs`, matching the version of the interpreter.

### Lua Contributing guidelines {#lua-contributing}

Following rules should be respected:

* Commit names of Lua libraries should reflect that they are Lua libraries, so write for example `luaPackages.luafilesystem: 1.11 -> 1.12`.

Title: `buildLuarocksPackage`, `buildLuaApplication`, and `lua.withPackages` Functions
Summary
This section elaborates on the `buildLuarocksPackage` function, explaining its implementation, how it leverages LuaRocks, and the arguments it accepts. It also introduces `buildLuaApplication`, highlighting its similarity to `buildLuaPackage` but without version prefixes. Furthermore, the `lua.withPackages` function is described, showing how to create Lua environments with specific packages, and emphasizing its version-aware nature. Finally, it ends with contribution guidelines for Lua libraries.