Lua in RPM
A fairly unknown feature of RPM is that it comes with an embedded Lua interpreter. This page attempts to document the possibilities of the embedded Lua. Note that Lua-support is a compile-time option, the following assumes that your RPM is was built using --with-lua option.
Lua scriptlets
The internal Lua can be used as the interpreter of rpm any scriptlets (%pre, %post etc):
%pre -p <lua>
print("Hello from Lua")
The point? Remember, Lua is embedded in rpm. This means the Lua scriptlets run in the rpm process context, instead of forking a new process to execute something. This has a number of advantages over, say, using /bin/sh as scriptlet interpreter:
- No forking involved means it runs faster. Lua itself is fairly fast as an interpreter too.
- No external dependencies introduced to packages.
- The internal interpreter can run when there's nothing at all installed yet, because it doesn't need to be forked. Consider the initial install phase: before even /bin/sh is available to execute the simplest shell built-in commands, the shell's dependencies will have to be installed. What if some of those need scriptlets?
- Internal Lua is the only thing that can reliably run in %pretrans. On initial system installation, there's absolutely nothing in the environment where %pretrans scriptlets execute. This is a condition you cannot even detect with any other means: testing for existence of a file or directory would otherwise require a shell, which is not there.
- Syntax errors in <lua> scripts are detected at package build time.
- As it runs in within the rpm process context, it can do things that external process cannot do, such as define new macros.
While scriptlets shouldn't be allowed to fail normally, you can signal scriptlet failure status by using Lua's error(msg, [level]) function if you need to. As <lua> scriptlets run within the rpm process itself, care needs to be taken within the scripts - eg os.exit() must not be called (see ticket #167).
Lua macros
The internal Lua interpreter can be used for dynamic macro content creation:
%{lua: print("Requires: foo >= 1.2")}
The above is a silly example and doesn't even begin to show how powerful a feature this is. For a slightly more complex example, RPM itself uses this to implement %patches and %sources macros (new in RPM 4.6.0):
%patches %{lua: for i, p in ipairs(patches) do print(p.." ") end}
%sources %{lua: for i, s in ipairs(sources) do print(s.." ") end}
Available Lua extensions in RPM
In addition to all Lua standard libraries (subject to the Lua version rpm is linked to), a few custom extensions are available in the RPM internal Lua interpreter. These can be used in all contexts where the internal Lua can be used.
rpm extension
The following RPM specific functions are available:
| Function | Explanation | Example |
| define(arg) | Define a global macro. | rpm.define("foo 1") |
| expand(arg) | Perform macro expansion. | rpm.expand("%{_libdir}") |
| register() | Register an RPM hook | |
| unregister() | Unregister an RPM hook | |
| call() | Call an RPM hook | |
| interactive() | Launch interactive session (only for testing + debugging) | rpm --eval "%{lua: rpm.interactive()}" |
posix extension
Lua standard library offers fairly limited set of io operations. The posix extension greatly enhances what can be done from Lua. The following functions are available in "posix" namespace, ie to call them use posix.function().
| Function | Explanation | Example |
| access(path, [mode]) | Test file/directory accessibility | if posix.access("/bin/rpm", "x") then ... end |
| chdir(path) | Change directory | posix.chdir("/tmp") |
| chmod(path, modestring) | Change file/directory mode | |
| chown(path, uid, gid) | Change file/directory owner/group | |
| ctermid() | ||
| dir([path]) | Get directory contents - like readdir() | for i,p in pairs(posix.dir("/tmp")) do print(p.."\n") end |
| errno() | Get errno value and message | print(posix.errno()) |
| exec(path, [args...]) | Exec a program | |
| files([path]) | Iterate over directory contents | for f in posix.files("/tmp") do print(f..'\n') end |
| fork() | Fork a new process | |
| getcwd() | Get current directory | |
| getenv(name) | Get environment variable | |
| getgroup(gid) | Get group members (gid is number or string) | |
| getlogin() | Get login name | |
| getpasswd(uid, selector) | Get passwd information (username, uid, gid, shell, gecos...) | posix.getpasswd(posix.getlogin(), "shell") |
| getprocessid(selector) | Get process ID information (gid, uid, pid, ppid...) | posix.getprocessid("pid") |
| kill(pid, signal) | Send a signal to a process | |
| link(oldpath, newpath) | Create a hard link | |
| mkdir(path) | Create a new directory | |
| mkfifo(path) | Create a FIFO | |
| pathconf() | Get pathconf values | |
| putenv() | Change or add an environment variable | |
| readlink(path) | Read symlink value | |
| rmdir(path) | Remove a directory | posix.rmdir("/tmp") |
| setgid(gid) | Set group identity | |
| setuid(uid) | Set user identity | |
| sleep(seconds) | Sleep for specified number of seconds | posix.sleep(5) |
| stat(path, selector) | Perform stat() on a file/directory (mode, ino, dev, nlink, uid, gid...) | posix.stat("/tmp", "mode") |
| symlink(oldpath, newpath) | Create a symlink | |
| sysconf(name) | Access sysconf values | posix.sysconf("open_max") |
| times() | Get process times | |
| ttyname() | Get current tty name | |
| umask() | Get current umask | |
| uname([format]) | Get information about current kernel | posix.uname("%r") |
| utime() | Change access/modification time of an inode | |
| wait() | Wait for a child process | |
| setenv() | Change or add an environment variable | |
| unsetenv() | Remove a variable from environment |
rex extension
This extension adds regular expression matching to Lua.
A simple example:
expr = rex.new(<regex>)
if expr:match(<arg>) then
... do stuff ...
end
TODO: fully document
Extending and customizing
On initialization, RPM executes a global init.lua Lua initialization script, typically located in /usr/lib/rpm/init.lua. This can be used for performing custom runtime configuration of RPM and adding global functions and variables to the RPM Lua environment without recompiling rpm.

