+1
-1
doc/languages-frameworks/index.xml
+1
-1
doc/languages-frameworks/index.xml
···
18
18
<xi:include href="idris.section.xml" />
19
19
<xi:include href="ios.section.xml" />
20
20
<xi:include href="java.xml" />
21
-
<xi:include href="lua.xml" />
21
+
<xi:include href="lua.section.xml" />
22
22
<xi:include href="node.section.xml" />
23
23
<xi:include href="ocaml.xml" />
24
24
<xi:include href="perl.xml" />
+252
doc/languages-frameworks/lua.section.md
+252
doc/languages-frameworks/lua.section.md
···
1
+
---
2
+
title: Lua
3
+
author: Matthieu Coudron
4
+
date: 2019-02-05
5
+
---
6
+
7
+
# User's Guide to Lua Infrastructure
8
+
9
+
## Using Lua
10
+
11
+
### Overview of Lua
12
+
13
+
Several versions of the Lua interpreter are available: luajit, lua 5.1, 5.2, 5.3.
14
+
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.
15
+
16
+
Lua libraries are in separate sets, with one set per interpreter version.
17
+
18
+
The interpreters have several common attributes. One of these attributes is
19
+
`pkgs`, which is a package set of Lua libraries for this specific
20
+
interpreter. E.g., the `busted` package corresponding to the default interpreter
21
+
is `lua.pkgs.busted`, and the lua 5.2 version is `lua5_2.pkgs.busted`.
22
+
The main package set contains aliases to these package sets, e.g.
23
+
`luaPackages` refers to `lua5_1.pkgs` and `lua52Packages` to
24
+
`lua5_2.pkgs`.
25
+
26
+
### Installing Lua and packages
27
+
28
+
#### Lua environment defined in separate `.nix` file
29
+
30
+
Create a file, e.g. `build.nix`, with the following expression
31
+
```nix
32
+
with import <nixpkgs> {};
33
+
34
+
lua5_2.withPackages (ps: with ps; [ busted luafilesystem ])
35
+
```
36
+
and install it in your profile with
37
+
```shell
38
+
nix-env -if build.nix
39
+
```
40
+
Now you can use the Lua interpreter, as well as the extra packages (`busted`,
41
+
`luafilesystem`) that you added to the environment.
42
+
43
+
#### Lua environment defined in `~/.config/nixpkgs/config.nix`
44
+
45
+
If you prefer to, you could also add the environment as a package override to the Nixpkgs set, e.g.
46
+
using `config.nix`,
47
+
```nix
48
+
{ # ...
49
+
50
+
packageOverrides = pkgs: with pkgs; {
51
+
myLuaEnv = lua5_2.withPackages (ps: with ps; [ busted luafilesystem ]);
52
+
};
53
+
}
54
+
```
55
+
and install it in your profile with
56
+
```shell
57
+
nix-env -iA nixpkgs.myLuaEnv
58
+
```
59
+
The environment is is installed by referring to the attribute, and considering
60
+
the `nixpkgs` channel was used.
61
+
62
+
#### Lua environment defined in `/etc/nixos/configuration.nix`
63
+
64
+
For the sake of completeness, here's another example how to install the environment system-wide.
65
+
66
+
```nix
67
+
{ # ...
68
+
69
+
environment.systemPackages = with pkgs; [
70
+
(lua.withPackages(ps: with ps; [ busted luafilesystem ]))
71
+
];
72
+
}
73
+
```
74
+
75
+
### How to override a Lua package using overlays?
76
+
77
+
Use the following overlay template:
78
+
79
+
```nix
80
+
final: prev:
81
+
{
82
+
83
+
lua = prev.lua.override {
84
+
packageOverrides = luaself: luaprev: {
85
+
86
+
luarocks-nix = luaprev.luarocks-nix.overrideAttrs(oa: {
87
+
pname = "luarocks-nix";
88
+
src = /home/my_luarocks/repository;
89
+
});
90
+
};
91
+
92
+
luaPackages = lua.pkgs;
93
+
}
94
+
```
95
+
96
+
### Temporary Lua environment with `nix-shell`
97
+
98
+
99
+
There are two methods for loading a shell with Lua packages. The first and recommended method
100
+
is to create an environment with `lua.buildEnv` or `lua.withPackages` and load that. E.g.
101
+
```sh
102
+
$ nix-shell -p 'lua.withPackages(ps: with ps; [ busted luafilesystem ])'
103
+
```
104
+
opens a shell from which you can launch the interpreter
105
+
```sh
106
+
[nix-shell:~] lua
107
+
```
108
+
The other method, which is not recommended, does not create an environment and requires you to list the packages directly,
109
+
110
+
```sh
111
+
$ nix-shell -p lua.pkgs.busted lua.pkgs.luafilesystem
112
+
```
113
+
Again, it is possible to launch the interpreter from the shell.
114
+
The Lua interpreter has the attribute `pkgs` which contains all Lua libraries for that specific interpreter.
115
+
116
+
117
+
## Developing with Lua
118
+
119
+
Now that you know how to get a working Lua environment with Nix, it is time
120
+
to go forward and start actually developing with Lua. There are two ways to
121
+
package lua software, either it is on luarocks and most of it can be taken care
122
+
of by the luarocks2nix converter or the packaging has to be done manually.
123
+
Let's present the luarocks way first and the manual one in a second time.
124
+
125
+
### Packaging a library on luarocks
126
+
127
+
[Luarocks.org](www.luarocks.org) is the main repository of lua packages.
128
+
The site proposes two types of packages, the rockspec and the src.rock
129
+
(equivalent of a [rockspec](https://github.com/luarocks/luarocks/wiki/Rockspec-format) but with the source).
130
+
These packages can have different build types such as `cmake`, `builtin` etc .
131
+
132
+
Luarocks-based packages are generated in pkgs/development/lua-modules/generated-packages.nix from
133
+
the whitelist maintainers/scripts/luarocks-packages.csv and updated by running maintainers/scripts/update-luarocks-packages.
134
+
135
+
[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).
136
+
The automation only goes so far though and some packages need to be customized.
137
+
These customizations go in `pkgs/development/lua-modules/overrides.nix`.
138
+
For instance if the rockspec defines `external_dependencies`, these need to be manually added in in its rockspec file then it won't work.
139
+
140
+
You can try converting luarocks packages to nix packages with the command `nix-shell -p luarocks-nix` and then `luarocks nix PKG_NAME`.
141
+
Nix rely on luarocks to install lua packages, basically it runs:
142
+
`luarocks make --deps-mode=none --tree $out`
143
+
144
+
#### Packaging a library manually
145
+
146
+
You can develop your package as you usually would, just don't forget to wrap it
147
+
within a `toLuaModule` call, for instance
148
+
```nix
149
+
mynewlib = toLuaModule ( stdenv.mkDerivation { ... });
150
+
```
151
+
152
+
There is also the `buildLuaPackage` function that can be used when lua modules
153
+
are not packaged for luarocks. You can see a few examples at `pkgs/top-level/lua-packages.nix`.
154
+
155
+
## Lua Reference
156
+
157
+
### Lua interpreters
158
+
159
+
Versions 5.1, 5.2 and 5.3 of the lua interpreter are available as
160
+
respectively `lua5_1`, `lua5_2` and `lua5_3`. Luajit is available too.
161
+
The Nix expressions for the interpreters can be found in `pkgs/development/interpreters/lua-5`.
162
+
163
+
164
+
#### Attributes on lua interpreters packages
165
+
166
+
Each interpreter has the following attributes:
167
+
168
+
- `interpreter`. Alias for `${pkgs.lua}/bin/lua`.
169
+
- `buildEnv`. Function to build lua interpreter environments with extra packages bundled together. See section *lua.buildEnv function* for usage and documentation.
170
+
- `withPackages`. Simpler interface to `buildEnv`.
171
+
- `pkgs`. Set of Lua packages for that specific interpreter. The package set can be modified by overriding the interpreter and passing `packageOverrides`.
172
+
173
+
174
+
#### `buildLuarocksPackage` function
175
+
176
+
The `buildLuarocksPackage` function is implemented in `pkgs/development/interpreters/lua-5/build-lua-package.nix`
177
+
The following is an example:
178
+
```nix
179
+
luaposix = buildLuarocksPackage {
180
+
pname = "luaposix";
181
+
version = "34.0.4-1";
182
+
183
+
src = fetchurl {
184
+
url = "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/luaposix-34.0.4-1.src.rock";
185
+
sha256 = "0yrm5cn2iyd0zjd4liyj27srphvy0gjrjx572swar6zqr4dwjqp2";
186
+
};
187
+
disabled = (luaOlder "5.1") || (luaAtLeast "5.4");
188
+
propagatedBuildInputs = [ bit32 lua std_normalize ];
189
+
190
+
meta = with stdenv.lib; {
191
+
homepage = "https://github.com/luaposix/luaposix/";
192
+
description = "Lua bindings for POSIX";
193
+
maintainers = with maintainers; [ vyp lblasc ];
194
+
license.fullName = "MIT/X11";
195
+
};
196
+
};
197
+
```
198
+
199
+
The `buildLuarocksPackage` delegates most tasks to luarocks:
200
+
201
+
* it adds `luarocks` as an unpacker for `src.rock` files (zip files really).
202
+
* configurePhase` writes a temporary luarocks configuration file which location
203
+
is exported via the environment variable `LUAROCKS_CONFIG`.
204
+
* the `buildPhase` does nothing.
205
+
* `installPhase` calls `luarocks make --deps-mode=none --tree $out` to build and
206
+
install the package
207
+
* In the `postFixup` phase, the `wrapLuaPrograms` bash function is called to
208
+
wrap all programs in the `$out/bin/*` directory to include `$PATH`
209
+
environment variable and add dependent libraries to script's `LUA_PATH` and
210
+
`LUA_CPATH`.
211
+
212
+
By default `meta.platforms` is set to the same value as the interpreter unless overridden otherwise.
213
+
214
+
#### `buildLuaApplication` function
215
+
216
+
The `buildLuaApplication` function is practically the same as `buildLuaPackage`.
217
+
The difference is that `buildLuaPackage` by default prefixes the names of the packages with the version of the interpreter.
218
+
Because with an application we're not interested in multiple version the prefix is dropped.
219
+
220
+
#### lua.withPackages function
221
+
222
+
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.
223
+
Using the `withPackages` function, the previous example for the luafilesystem environment can be written like this:
224
+
```nix
225
+
with import <nixpkgs> {};
226
+
227
+
lua.withPackages (ps: [ps.luafilesystem])
228
+
```
229
+
230
+
`withPackages` passes the correct package set for the specific interpreter version as an argument to the function. In the above example, `ps` equals `luaPackages`.
231
+
But you can also easily switch to using `lua5_2`:
232
+
```nix
233
+
with import <nixpkgs> {};
234
+
235
+
lua5_2.withPackages (ps: [ps.lua])
236
+
```
237
+
238
+
Now, `ps` is set to `lua52Packages`, matching the version of the interpreter.
239
+
240
+
241
+
### Possible Todos
242
+
243
+
* export/use version specific variables such as `LUA_PATH_5_2`/`LUAROCKS_CONFIG_5_2`
244
+
* let luarocks check for dependencies via exporting the different rocktrees in temporary config
245
+
246
+
### Lua Contributing guidelines
247
+
248
+
Following rules should be respected:
249
+
250
+
* Make sure libraries build for all Lua interpreters.
251
+
* Commit names of Lua libraries should reflect that they are Lua libraries, so write for example `luaPackages.luafilesystem: 1.11 -> 1.12`.
252
+
-36
doc/languages-frameworks/lua.xml
-36
doc/languages-frameworks/lua.xml
···
1
-
<section xmlns="http://docbook.org/ns/docbook"
2
-
xmlns:xlink="http://www.w3.org/1999/xlink"
3
-
xml:id="sec-language-lua">
4
-
<title>Lua</title>
5
-
6
-
<para>
7
-
Lua packages are built by the <varname>buildLuaPackage</varname> function. This function is implemented in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/lua-modules/generic/default.nix"> <filename>pkgs/development/lua-modules/generic/default.nix</filename></link> and works similarly to <varname>buildPerlPackage</varname>. (See <xref linkend="sec-language-perl"/> for details.)
8
-
</para>
9
-
10
-
<para>
11
-
Lua packages are defined in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/lua-packages.nix"><filename>pkgs/top-level/lua-packages.nix</filename></link>. Most of them are simple. For example:
12
-
<programlisting>
13
-
fileSystem = buildLuaPackage {
14
-
name = "filesystem-1.6.2";
15
-
src = fetchurl {
16
-
url = "https://github.com/keplerproject/luafilesystem/archive/v1_6_2.tar.gz";
17
-
sha256 = "1n8qdwa20ypbrny99vhkmx8q04zd2jjycdb5196xdhgvqzk10abz";
18
-
};
19
-
meta = {
20
-
homepage = "https://github.com/keplerproject/luafilesystem";
21
-
hydraPlatforms = stdenv.lib.platforms.linux;
22
-
maintainers = with maintainers; [ flosse ];
23
-
};
24
-
};
25
-
</programlisting>
26
-
</para>
27
-
28
-
<para>
29
-
Though, more complicated package should be placed in a seperate file in <link
30
-
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/lua-modules"><filename>pkgs/development/lua-modules</filename></link>.
31
-
</para>
32
-
33
-
<para>
34
-
Lua packages accept additional parameter <varname>disabled</varname>, which defines the condition of disabling package from luaPackages. For example, if package has <varname>disabled</varname> assigned to <literal>lua.luaversion != "5.1"</literal>, it will not be included in any luaPackages except lua51Packages, making it only be built for lua 5.1.
35
-
</para>
36
-
</section>