Growing a config
Once main.lua holds a few recipes it gets long. This page covers splitting it into files, what a reload keeps, and how parts of a config talk to each other.
Where the config lives
The config is a directory with a main.lua in it. Hazel looks for it here:
$HAZEL_COMPOSITOR_DIR, when that is set.- Otherwise
$XDG_CONFIG_HOME/hazel, or~/.config/hazelwhenXDG_CONFIG_HOMEisn't set.
Without either, Hazel stops at start with an error naming the path it looked for:
Hazel has no config: /home/you/.config/hazel/main.lua does not exist; create it, or set HAZEL_COMPOSITOR_DIRHAZEL_COMPOSITOR_DIR is taken as it is. Without a main.lua there, Hazel logs an error and runs with no config until one is saved.
Modules
The config directory comes first on package.path, as <dir>/?.lua. require("keys") loads keys.lua next to main.lua, and require("layouts.dwindle") loads layouts/dwindle.lua. A layouts/init.lua is not found this way.
A module is a file that returns a table. This keys.lua holds the bindings from your first config:
local keys = {}
local bindings = {}
function keys.bind(modifiers, key, action)
local wanted = {}
for name in modifiers:gmatch("%a+") do wanted[name] = true end
bindings[#bindings + 1] = { modifiers = wanted, key = Key[key], action = action }
end
local function matches(binding, event)
if event.raw_key ~= binding.key then return false end
for _, name in ipairs({ "super", "shift", "ctrl", "alt" }) do
if (binding.modifiers[name] or false) ~= event.modifiers[name] then return false end
end
return true
end
wm.input:on("key", function(event)
if wm.is_session_locked or event.shortcuts_inhibited then return end
for _, binding in ipairs(bindings) do
if matches(binding, event) then
event:prevent_default()
if event.state == "Pressed" then binding.action() end
return
end
end
end)
return keysand main.lua uses it:
local keys = require("keys")
keys.bind("super", "Return", function() Process.spawn("foot") end)
keys.bind("super shift", "r", function() wm:reload() end)requireruns a file once per load and hands every caller the same table. Two modules that bothrequire("keys")add to the same bindings.- Every module sees the same globals:
wm,Key,Processand the rest. - Image drawables with a relative
pathload it from the config directory too.
For completion and type checking in your editor, write Hazel's type definitions for lua-language-server into the config directory:
hazel --dump-typedefs > ~/.config/hazel/types.luaReloading
Saving any file in the directory, or in a directory under it, reloads the config. So does wm:reload().
The old config gets unload first. The new one starts in a fresh Lua state with a new wm, and these carry over:
- windows, layers and outputs. The new config hears about each one through its usual event (
new_window,new_layer,added), then getsready. wm.store.- processes started without callbacks.
Everything else of the old config is gone: its listeners, timers, drawables and local variables. Processes it started with callbacks get SIGTERM, unless they were started with survives_reload.
A config that fails to load leaves the previous one running. If the first load at start fails, Hazel runs with no config. Saving a fixed file loads it.
wm.is_reload is false for the first load and true after. Check it before starting programs that should run once per session.
Keeping state with wm.store
Locals start over on every reload. wm.store is a table that survives: the Tiling recipe keeps its window order there, so saving the config doesn't reshuffle your windows.
wm.store.reloads = wm.store.reloads and wm.store.reloads + 1 or 0
print("reloads this session:", wm.store.reloads)- Start from what's already there (
wm.store.x = wm.store.x or ...), or each reload resets it. - After a reload,
new_windowfires again for every open window. Don't count windows there. first_contentfires again right after it for windows and layers that have drawn, so what you do there runs again too. A floating window from your first config is centred again on every save.- It holds booleans, numbers, strings, Points, Sizes, Rects and tables of those. A function or a window in it stops the reload, and the error is logged.
- Windows can't be stored, but
window.idcan. It stays the same across reloads.
Your own events
wm:emit(name, ...) fires an event that any part of the config can listen to with wm:on(name, fn). It lets one module react to another without either require-ing the other.
wm:on("layout_changed", function(layout)
print("layout is now", layout)
end)
wm:emit("layout_changed", "master")The recipes use a few: arrange asks Tiling to re-tile, show_workspace and workspace_changed belong to Workspaces. Nothing happens when no one listens, so a recipe can emit arrange whether or not you use tiling.
- Handlers get the arguments, then
wm. - They run before
emitreturns, in the order they were added. - One that raises an error is logged, and the rest still run.