Building a Config DSL
Create a declarative config language that loads via dofile() in a sandbox.
Building a Config DSL is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Goal: Declarative Config
A config DSL lets users express configuration in a readable, declarative style while using Lua's full power (conditionals, loops, functions) behind the scenes.
Config Function Pattern
Expose a config(tbl) function that validates and stores configuration. Call it with a table literal for clean syntax.
local function config(tbl)
-- validate and store
return tbl
end
-- config.lua:
return config {
host = "localhost",
port = 8080,
debug = os.getenv("DEBUG") == "1",
}Nested Config with Sections
Support nested sections for organized configuration.
return config {
database = { host="db.local", port=5432 },
cache = { host="cache.local", ttl=300 },
logging = { level="INFO", file="/var/log/app.log" },
}Helper Functions in Config
Allow calling helper functions within the config for computed values.
local function env(name, default)
return os.getenv(name) or default
end
return config {
host = env("DB_HOST", "localhost"),
port = tonumber(env("DB_PORT", "5432")),
}Sandboxed Config Loading
For security, load the config in a restricted environment that allows only safe helpers.
local function loadConfig(path)
local safeEnv = {
config = config,
env = env,
math = math,
table = table,
}
local fn, err = loadfile(path, "t", safeEnv)
if not fn then error(err) end
return fn()
endSchema Validation
Validate required fields and types after loading.
local function validate(cfg)
assert(type(cfg.host) == "string", "host must be string")
assert(type(cfg.port) == "number", "port must be number")
assert(cfg.port > 0 and cfg.port < 65536, "port out of range")
end
validate(loadConfig("config.lua"))Default Values
Merge loaded config with defaults to ensure all required keys have values.
local defaults = {host="localhost", port=8080, debug=false}
local function loadWithDefaults(path)
local cfg = loadConfig(path)
for k, v in pairs(defaults) do
if cfg[k] == nil then cfg[k] = v end
end
return cfg
endEnvironment Override
Allow environment variables to override config file values — essential for 12-factor app design.
Config Inheritance
Load a base config then overlay environment-specific config (development, staging, production).
Immutable Config
After loading, freeze the config table with a __newindex that raises an error, preventing accidental runtime mutation.
Documentation in Config
Use comments and structured key names as self-documentation. A well-designed config DSL is self-explanatory to DevOps teams.
Config DSL Question
Why load a config file with a sandboxed _ENV?
Recap: Building a Config DSL
Expose a config(tbl) function. Support nested sections and helper functions. Load in a sandboxed environment. Validate required fields and types. Provide defaults and allow environment variable overrides for 12-factor compliance.
Frequently asked questions
Is the “Building a Config DSL” lesson free?
Yes — the full text of “Building a Config DSL” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Config DSL”?
Create a declarative config language that loads via dofile() in a sandbox. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Config DSL” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.