Files
nixpkgs/nixos/tests/openresty-lua.nix
Ilan Joselevich eaffe290dd nixos/nginx: add lua option for Lua scripting support
Add `services.nginx.lua.{enable,extraPackages}` to enable OpenResty's
lua-nginx-module on a stock nginx. When enabled it adds the module,
includes lua-resty-core, and wires up lua_package_path /
lua_package_cpath (and lua_ssl_trusted_certificate) from a
luajit_openresty package set built from extraPackages.

When the configured package already bundles Lua (openresty), the module
and bundled libraries are not re-added; only the search path is set up so
its own lualib stays in use.

Migrate the openresty-lua test to the new option and add an nginx-lua
test covering the stock-nginx path.

Assisted-by: Claude:claude-opus-4-8
2026-06-25 00:56:45 +03:00

97 lines
3.0 KiB
Nix

{ pkgs, ... }:
{
name = "openresty-lua";
meta = with pkgs.lib.maintainers; {
maintainers = [ bbigras ];
};
nodes = {
webserver =
{ pkgs, ... }:
{
networking = {
extraHosts = ''
127.0.0.1 default.test
127.0.0.1 sandbox.test
'';
};
services.nginx = {
enable = true;
package = pkgs.openresty;
lua = {
enable = true;
extraPackages = p: [ p.markdown ];
};
virtualHosts."default.test" = {
default = true;
locations."/" = {
extraConfig = ''
default_type text/html;
access_by_lua '
local markdown = require "markdown"
markdown("source")
';
'';
};
};
virtualHosts."sandbox.test" = {
locations."/test1-write" = {
extraConfig = ''
content_by_lua_block {
local create = os.execute('${pkgs.coreutils}/bin/mkdir /tmp/test1-read')
local create = os.execute('${pkgs.coreutils}/bin/touch /tmp/test1-read/foo.txt')
local echo = os.execute('${pkgs.coreutils}/bin/echo worked > /tmp/test1-read/foo.txt')
}
'';
};
locations."/test1-read" = {
root = "/tmp";
};
locations."/test2-write" = {
extraConfig = ''
content_by_lua_block {
local create = os.execute('${pkgs.coreutils}/bin/mkdir /var/web/test2-read')
local create = os.execute('${pkgs.coreutils}/bin/touch /var/web/test2-read/bar.txt')
local echo = os.execute('${pkgs.coreutils}/bin/echo error-worked > /var/web/test2-read/bar.txt')
}
'';
};
locations."/test2-read" = {
root = "/var/web";
};
};
};
};
};
testScript =
{ nodes, ... }:
''
url = "http://localhost"
webserver.wait_for_unit("nginx")
webserver.wait_for_open_port(80)
http_code = webserver.succeed(
f"curl -w '%{{http_code}}' --head --fail {url}"
)
assert http_code.split("\n")[-1] == "200"
# This test checks the creation and reading of a file in sandbox mode.
# Checking write in temporary folder
webserver.succeed("$(curl -vvv http://sandbox.test/test1-write)")
webserver.succeed('test "$(curl -fvvv http://sandbox.test/test1-read/foo.txt)" = worked')
# Checking write in protected folder. In sandbox mode for the nginx service, the folder /var/web is mounted
# in read-only mode.
webserver.succeed("mkdir -p /var/web")
webserver.succeed("chown nginx:nginx /var/web")
webserver.succeed("$(curl -vvv http://sandbox.test/test2-write)")
assert "404 Not Found" in webserver.succeed(
"curl -vvv -s http://sandbox.test/test2-read/bar.txt"
)
'';
}