Skip to content

Lua Scripting

Carlos Hernandez edited this page Jul 14, 2026 · 2 revisions

Lua Scripting

The Setup-Exe installer runs a Lua script at install time. Everything the installer does — copying files, creating shortcuts, writing the registry, registering with Add/Remove Programs — is a call into the embala.* API from that script. There is one runtime, not two: the declarative [setup] section is lowered to a generated install.lua over the exact same API a hand-written script would call.

The honest part up front: the script is trusted code, not a sandbox. The Lua standard library is deliberately narrow (no io, no require), but that is a runaway/cancel budget, not a security boundary — a script can run any process on the machine via embala.exec. And only the reversible calls are journaled and undone; fs.remove, registry.delete, and exec are not. Which calls fall where is the whole point of the reference tables below.

Two modes

You get to the installer script one of two ways.

Declarative — [setup] lowers to a script

When [setup].script is absent, embala generates the install.lua for you from the declarative fields (see Configuration-Reference). This is the 80% path. This [setup]:

[setup]
arch = "x86_64"
main-executable = "hello.exe"
license = "license.txt"
files = [{ src = "dist/hello.exe", dest = "hello.exe" }]
components = [
  { id = "docs", label = "Documentation", description = "Offline readme", default = true, files = [{ src = "readme.txt", dest = "readme.txt" }] },
]

lowers to (abridged — the license text embeds inline, and a mode page is prepended only for install-mode = user-choice):

local pkg = embala.package

embala.ui.page{ kind = "welcome", title = pkg.display_name, body = pkg.description }
embala.ui.page{ kind = "license", must_accept = true, text = "…embedded license text…" }
embala.ui.page{ kind = "directory", default = embala.install_dir, allow_change = true }
embala.ui.page{
  kind = "components",
  items = {
    { id = "docs", label = "Documentation", description = "Offline readme", default = true },
  },
}

-- Payload files (always installed).
embala.fs.copy("hello.exe", "hello.exe")

-- Component "docs" (installed when selected).
if embala.ui.selected("docs") then
  embala.fs.copy("readme.txt", "readme.txt")
end

-- Start-menu shortcut for the main executable.
embala.shortcut.create{ name = pkg.display_name, target = "hello.exe", location = "start-menu", description = pkg.description }

-- Add/Remove Programs registration.
embala.arp.register{
  display_name = pkg.display_name,
  version = pkg.version,
  publisher = pkg.publisher,
  install_location = embala.install_dir,
  uninstall_string = '"' .. embala.install_dir .. '\\uninstall.exe"',
  display_icon = embala.install_dir .. '\\' .. "hello.exe",
}

embala.ui.page{ kind = "finish", run = { target = "hello.exe" } }

The generated script ships verbatim as plaintext in the installer, so it is auditable without extracting anything. Top-level files copy unconditionally; components[].files are wrapped in an if embala.ui.selected(id) guard so an unselected component installs nothing. The arp.register call is always emitted — the declarative path never leaves the app out of Add/Remove Programs.

Raw — script = ships your Lua verbatim

When [setup].script points at a file, that Lua ships unchanged and lowering is skipped. You own the whole install flow. In exchange, a raw script cannot be combined with the components, shortcuts, or main-executable sugar — those fields describe work the runtime would generate, and a raw script is expected to do that work itself. Setting them together is rejected at config-parse time rather than silently ignored.

uninstall-script works in both modes: point it at a raw uninstall.lua to run custom teardown. It runs in addition to the automatic log replay (see Rollback), not instead of it, so it only needs to handle what the journal does not already cover.

Full API reference

The global is embala. Every mutating call is routed through the install journal according to the tables below.

Context (read-only)

Snapshotted plain values and pure functions — reading them mutates nothing.

API Type Value
embala.package table { name, display_name, version, identifier, publisher, description, homepage, license } from [package]
embala.mode string Resolved install mode: "per-user" or "per-machine"
embala.install_dir string Resolved target directory. Re-synced after the directory/mode pages resolve, so later calls read the final path
embala.payload_dir string Staging directory the payload zip was extracted to (the root that fs.copy src is relative to). Absent at uninstall time
embala.arch() function → string "x86_64" or "aarch64"
embala.os_version() function → table { major, minor, build }
embala.log(msg) function Append a progress line. Shown on the progress page interactively; echoed to stdout when a console is attached (/S or launched from a shell)

src arguments resolve relative to payload_dir; dest arguments resolve relative to install_dir (an absolute dest is used as-is).

Reading the registry — embala.registry.get

A live registry read. Unlike the snapshotted context values above it hits the registry each call, but it is still a read: not journaled, not reversed, and not gated by the wizard — so it works before any page renders, which is exactly when you need it (WebView2 presence checks, previous-install detection).

Call Fields / signature Returns
embala.registry.get{…} hive, key (required); name (optional) string, integer, or nil
  • hive is "HKCU" or "HKLM" — an unknown hive is a hard error (parity with registry.set). Every other absence is soft.
  • sz and expand-sz values come back as a Lua string; dword as a Lua integer.
  • A missing key, a missing value, or a value of any other kind (binary, multi-sz, qword) returns nil — absence is an ordinary branch condition, not an error. Omit name to read the key's default (unnamed) value.
-- Is the Edge WebView2 runtime already installed (per-machine marker)?
local pv = embala.registry.get{
  hive = "HKLM",
  key = [[SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}]],
  name = "pv",
}
if not pv then
  -- ...download and run the bootstrapper (see Porting-from-NSIS)...
end

registry.get is registered in both the install and the uninstall engines, so uninstall.lua can read the registry too.

Reversible (journaled, undone on rollback and uninstall)

These calls each write a record to install.log before touching the system, so an interrupt leaves a journal that reverses work that might have happened. Every reversal is idempotent (delete-if-exists / rmdir-if-empty), so undoing a resource that was never created is harmless. Rollback (on error/cancel) and the uninstaller both replay this journal LIFO.

Call Fields / signature Behavior
embala.fs.copy(src, dest) two strings Copy one file. Creates missing parent dirs (each journaled)
embala.fs.copy_tree(src, dest) two strings Recursively copy a directory; every file and directory created is journaled
embala.fs.mkdir(path) string Create a directory and any missing ancestors; each new level is journaled
embala.shortcut.create{…} name, target (required); args, icon, workdir, description, location (optional) Write a .lnk via COM. location: "start-menu" (default) or "desktop", resolved under the install mode's folder. target resolves relative to install_dir; workdir defaults to the target's parent
embala.registry.set{…} hive, key, value (required); name, kind (optional) Write a registry value. hive: "HKCU" or "HKLM". kind: "sz" (default), "dword", or "expand-sz". Reversal deletes the value, and the whole key if this call created it
embala.arp.register{…} display_name, version, publisher, install_location, uninstall_string (required); display_icon, estimated_size, no_modify, no_repair (optional) Write the Add/Remove Programs entry under …\Uninstall\<identifier> in the mode's hive. Reversal removes the whole key
embala.env.set{…} name, value (required); scope (optional) Set an environment variable as REG_EXPAND_SZ (so a value containing %VARS% — e.g. a PATH addition — expands), then broadcast the environment change so running shells pick it up. scope: "user" or "machine", defaulting to the install mode. Reversal deletes the value and re-broadcasts
embala.download{…} url, sha256, dest (all required) Fetch over HTTPS using the system TLS stack, verify the SHA-256, and write the file. A checksum mismatch is a hard error. The written file is journaled like a copy

The sha256 on download is not optional — there is no unchecked download.

Destructive (NOT journaled, NOT undone)

These have no inverse, so they are applied but never written to the journal. Rollback and uninstall will not reverse them. Use with care.

Call Fields / signature Behavior
embala.fs.remove(path) string Delete one file (no error if already gone)
embala.fs.remove_tree(path) string Recursively delete a directory (no error if already gone)
embala.registry.delete{…} hive, key (required); name (optional) Delete a registry value when name is given, or the whole key tree when it is absent

Neither

Call Fields / signature Behavior
embala.exec{…} cmd (required); args, cwd, wait (optional) Run a process. With wait = true (default) blocks and returns { code, stdout, stderr }; with wait = false it fire-and-forgets and returns { code = 0 } on launch. Not journaled and never rolled back — if you make a change through exec, undoing it is your responsibility

UI — embala.ui

embala.ui.page{ kind = "…", … } registers a wizard page, in call order. The same registrations drive both the interactive wizard and headless/silent (/S) runs — in headless mode each page resolves its effect from config plus CLI flags instead of showing a window. All seven kinds:

kind Fields Headless (/S) resolution
mode — No-op. The mode is already resolved from /mode= or the manifest before the script runs; the page is only a render marker
welcome title, body (optional) No-op (informational)
license text (optional); must_accept (default true) Auto-accepted, and the acceptance is logged for the audit trail
directory default, allow_change (default true) (optional) Re-points install_dir to default unless /D= was given (the CLI wins). Must run before any mutating call — re-pointing after work has begun is a hard error
components items = { { id, label, description?, default? }, … } Selection comes from /components=<ids> if given, else each item's default. Unknown ids on the CLI are ignored
progress — Implicit and always present; accepting the name is a no-op
finish body (optional); run = { target, label? } (optional); links = { { label, url }, … } (optional) No-op. Rendered as a separate window after the script completes; the optional run offers to launch the target

embala.ui.selected(id) returns whether a component was chosen. Querying an id that no components page declared is a script error (a typo should fail loud, not silently read false).

At uninstall time the same call reads the resolved uninstall options declared in [setup] (see Setup-Exe and Configuration-Reference): embala.ui.selected("purge-data") is true when the user ticked that option's checkbox in the confirm dialog, when /options=purge-data was passed, or when the option's default is true and nothing overrode it. The declared uninstall-option ids are seeded into the uninstall engine, so querying an id no uninstall-options entry declared is the same hard error it is at install time. Guard destructive teardown with it:

-- uninstall.lua — only wipe app data when the user opted in.
if embala.ui.selected("purge-data") then
  embala.fs.remove_tree(embala.install_dir .. "\\data")
end

Rollback

Every reversible call appends to install.log before it mutates. On any error, or when the user cancels (Cancel / the window's [X] / Ctrl+C), the runtime replays that journal LIFO and best-effort — a stuck value cannot strand the rest of the cleanup, and an already-gone resource is a no-op. The uninstaller replays the same journal, which is why a log-driven uninstall works even with no hand-written uninstall.lua.

Two consequences worth stating plainly:

  • The destructive calls are one-way. fs.remove, fs.remove_tree, and registry.delete are never in the journal, so a later failure will not bring back what they deleted.
  • The wizard only appears if there is something to show. If the script registers at least one pre-install page (mode / welcome / license / components / directory), the interactive run shows the wizard and blocks on the user's Install/Cancel before the first mutation. A script that registers no pre-install pages (a bare hand-written script, or one with only a finish page) runs headless-style with no window even when launched interactively.

Raw-script responsibilities

In raw script = mode you inherit two jobs the declarative path handles for you.

ARP registration is yours. The generated script always emits embala.arp.register; a raw script does not get it for free. If you skip it, your app installs but never appears in Add/Remove Programs, and there is no UninstallString for Windows to invoke. Call embala.arp.register{…} yourself (see the worked lowering above for the standard field set built from the package identity and install_dir).

Know the sandbox you are in — because there barely is one. The script runs against a curated Lua standard library:

Loaded Not loaded
table, string, math, os io, package (no require), debug

io and package are omitted so filesystem access and module loading flow through the audited embala.* API rather than raw Lua, and debug is omitted because it is unsafe. But this is a runaway/cancel budget, not a security sandbox: the author is trusted, and embala.exec can run anything on the machine. Treat the script as the privileged code it is.

Where it fits

  • Setup-Exe — the installer backend this script runs inside, and its CLI flags (/S, /D=, /mode=, /components=).
  • Configuration-Reference — the [setup] fields the declarative path lowers from.
  • Code-Signing — signing the finished setup.exe, .msi, and the uninstaller.

Clone this wiki locally