3 ## Using Lua {#lua-userguide}
5 ### Overview of Lua {#lua-overview}
7 Several versions of the Lua interpreter are available: luajit, lua 5.1, 5.2, 5.3.
8 The attribute `lua` refers to the default interpreter, it is also possible to refer to specific versions, e.g. `lua5_2` refers to Lua 5.2.
10 Lua libraries are in separate sets, with one set per interpreter version.
12 The interpreters have several common attributes. One of these attributes is
13 `pkgs`, which is a package set of Lua libraries for this specific
14 interpreter. E.g., the `busted` package corresponding to the default interpreter
15 is `lua.pkgs.busted`, and the lua 5.2 version is `lua5_2.pkgs.busted`.
16 The main package set contains aliases to these package sets, e.g.
17 `luaPackages` refers to `lua5_1.pkgs` and `lua52Packages` to
20 Note that nixpkgs patches the non-luajit interpreters to avoid referring to
21 `/usr` and have `;;` (a [placeholder](https://www.lua.org/manual/5.1/manual.html#pdf-package.path) replaced with the default LUA_PATH) work correctly.
23 ### Installing Lua and packages {#installing-lua-and-packages}
25 #### Lua environment defined in separate `.nix` file {#lua-environment-defined-in-separate-.nix-file}
27 Create a file, e.g. `build.nix`, with the following expression
30 with import <nixpkgs> {};
32 lua5_2.withPackages (ps: with ps; [ busted luafilesystem ])
35 and install it in your profile with
40 Now you can use the Lua interpreter, as well as the extra packages (`busted`,
41 `luafilesystem`) that you added to the environment.
43 #### Lua environment defined in `~/.config/nixpkgs/config.nix` {#lua-environment-defined-in-.confignixpkgsconfig.nix}
45 If you prefer to, you could also add the environment as a package override to the Nixpkgs set, e.g.
51 packageOverrides = pkgs: with pkgs; {
52 myLuaEnv = lua5_2.withPackages (ps: with ps; [ busted luafilesystem ]);
57 and install it in your profile with
60 nix-env -iA nixpkgs.myLuaEnv
62 The environment is installed by referring to the attribute, and considering
63 the `nixpkgs` channel was used.
65 #### Lua environment defined in `/etc/nixos/configuration.nix` {#lua-environment-defined-in-etcnixosconfiguration.nix}
67 For the sake of completeness, here's another example how to install the environment system-wide.
72 environment.systemPackages = with pkgs; [
73 (lua.withPackages(ps: with ps; [ busted luafilesystem ]))
78 ### How to override a Lua package using overlays? {#how-to-override-a-lua-package-using-overlays}
80 Use the following overlay template:
86 lua = prev.lua.override {
87 packageOverrides = luaself: luaprev: {
89 luarocks-nix = luaprev.luarocks-nix.overrideAttrs(oa: {
90 pname = "luarocks-nix";
91 src = /home/my_luarocks/repository;
96 luaPackages = lua.pkgs;
100 ### Temporary Lua environment with `nix-shell` {#temporary-lua-environment-with-nix-shell}
103 There are two methods for loading a shell with Lua packages. The first and recommended method
104 is to create an environment with `lua.buildEnv` or `lua.withPackages` and load that. E.g.
107 $ nix-shell -p 'lua.withPackages(ps: with ps; [ busted luafilesystem ])'
110 opens a shell from which you can launch the interpreter
116 The other method, which is not recommended, does not create an environment and requires you to list the packages directly,
119 $ nix-shell -p lua.pkgs.busted lua.pkgs.luafilesystem
121 Again, it is possible to launch the interpreter from the shell.
122 The Lua interpreter has the attribute `pkgs` which contains all Lua libraries for that specific interpreter.
125 ## Developing with lua {#lua-developing}
127 Now that you know how to get a working Lua environment with Nix, it is time
128 to go forward and start actually developing with Lua. There are two ways to
129 package lua software, either it is on luarocks and most of it can be taken care
130 of by the luarocks2nix converter or the packaging has to be done manually.
131 Let's present the luarocks way first and the manual one in a second time.
133 ### Packaging a library on luarocks {#packaging-a-library-on-luarocks}
135 [Luarocks.org](https://luarocks.org/) is the main repository of lua packages.
136 The site proposes two types of packages, the `rockspec` and the `src.rock`
137 (equivalent of a [rockspec](https://github.com/luarocks/luarocks/wiki/Rockspec-format) but with the source).
139 Luarocks-based packages are generated in [pkgs/development/lua-modules/generated-packages.nix](https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/lua-modules/generated-packages.nix) from
140 the whitelist maintainers/scripts/luarocks-packages.csv and updated by running
141 the package `luarocks-packages-updater`:
145 nix-shell -p luarocks-packages-updater --run luarocks-packages-updater
148 [luarocks2nix](https://github.com/nix-community/luarocks) is a tool capable of generating nix derivations from both rockspec and src.rock (and favors the src.rock).
149 The automation only goes so far though and some packages need to be customized.
150 These customizations go in [pkgs/development/lua-modules/overrides.nix](https://github.com/NixOS/nixpkgs/tree/master/pkgs/development/lua-modules/overrides.nix).
151 For instance if the rockspec defines `external_dependencies`, these need to be manually added to the overrides.nix.
153 You can try converting luarocks packages to nix packages with the command `nix-shell -p luarocks-nix` and then `luarocks nix PKG_NAME`.
155 #### Packaging a library manually {#packaging-a-library-manually}
157 You can develop your package as you usually would, just don't forget to wrap it
158 within a `toLuaModule` call, for instance
162 mynewlib = toLuaModule ( stdenv.mkDerivation { /* ... */ });
166 There is also the `buildLuaPackage` function that can be used when lua modules
167 are not packaged for luarocks. You can see a few examples at `pkgs/top-level/lua-packages.nix`.
169 ## Lua Reference {#lua-reference}
171 ### Lua interpreters {#lua-interpreters}
173 Versions 5.1, 5.2, 5.3 and 5.4 of the lua interpreter are available as
174 respectively `lua5_1`, `lua5_2`, `lua5_3` and `lua5_4`. Luajit is available too.
175 The Nix expressions for the interpreters can be found in `pkgs/development/interpreters/lua-5`.
177 #### Attributes on lua interpreters packages {#attributes-on-lua-interpreters-packages}
179 Each interpreter has the following attributes:
181 - `interpreter`. Alias for `${pkgs.lua}/bin/lua`.
182 - `buildEnv`. Function to build lua interpreter environments with extra packages bundled together. See section *lua.buildEnv function* for usage and documentation.
183 - `withPackages`. Simpler interface to `buildEnv`.
184 - `pkgs`. Set of Lua packages for that specific interpreter. The package set can be modified by overriding the interpreter and passing `packageOverrides`.
186 #### `buildLuarocksPackage` function {#buildluarockspackage-function}
188 The `buildLuarocksPackage` function is implemented in `pkgs/development/interpreters/lua-5/build-luarocks-package.nix`
189 The following is an example:
192 luaposix = buildLuarocksPackage {
194 version = "34.0.4-1";
197 url = "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/luaposix-34.0.4-1.src.rock";
198 hash = "sha256-4mLJG8n4m6y4Fqd0meUDfsOb9RHSR0qa/KD5KCwrNXs=";
200 disabled = (luaOlder "5.1") || (luaAtLeast "5.4");
201 propagatedBuildInputs = [ bit32 lua std_normalize ];
204 homepage = "https://github.com/luaposix/luaposix/";
205 description = "Lua bindings for POSIX";
206 maintainers = with lib.maintainers; [ vyp lblasc ];
207 license.fullName = "MIT/X11";
213 The `buildLuarocksPackage` delegates most tasks to luarocks:
215 * it adds `luarocks` as an unpacker for `src.rock` files (zip files really).
216 * `configurePhase` writes a temporary luarocks configuration file which location
217 is exported via the environment variable `LUAROCKS_CONFIG`.
218 * the `buildPhase` does nothing.
219 * `installPhase` calls `luarocks make --deps-mode=none --tree $out` to build and
221 * In the `postFixup` phase, the `wrapLuaPrograms` bash function is called to
222 wrap all programs in the `$out/bin/*` directory to include `$PATH`
223 environment variable and add dependent libraries to script's `LUA_PATH` and
226 It accepts as arguments:
228 * 'luarocksConfig': a nix value that directly maps to the luarocks config used during
231 By default `meta.platforms` is set to the same value as the interpreter unless overridden otherwise.
233 #### `buildLuaApplication` function {#buildluaapplication-function}
235 The `buildLuaApplication` function is practically the same as `buildLuaPackage`.
236 The difference is that `buildLuaPackage` by default prefixes the names of the packages with the version of the interpreter.
237 Because with an application we're not interested in multiple version the prefix is dropped.
239 #### lua.withPackages function {#lua.withpackages-function}
241 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.
242 Using the `withPackages` function, the previous example for the luafilesystem environment can be written like this:
245 lua.withPackages (ps: [ps.luafilesystem])
248 `withPackages` passes the correct package set for the specific interpreter version as an argument to the function. In the above example, `ps` equals `luaPackages`.
249 But you can also easily switch to using `lua5_1`:
252 lua5_1.withPackages (ps: [ps.lua])
255 Now, `ps` is set to `lua5_1.pkgs`, matching the version of the interpreter.
257 ### Lua Contributing guidelines {#lua-contributing}
259 Following rules should be respected:
261 * Commit names of Lua libraries should reflect that they are Lua libraries, so write for example `luaPackages.luafilesystem: 1.11 -> 1.12`.