Lua Academy · Lesson

Building a Config DSL

Create a declarative config language that loads via dofile() in a sandbox.

Lesson 3 of 413 steps

Building a Config DSL is a free Lua Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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()
end

Schema 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
end

Environment 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Building a Config DSL” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Building a Config DSL”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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.

All lessons in this course

  1. DSL Design Principles in Lua
  2. Operator Overloading for DSL Fluency
  3. Building a Config DSL
  4. Simple Expression Parser
← Back to Lua Academy