Skip to content

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:

  1. $HAZEL_COMPOSITOR_DIR, when that is set.
  2. Otherwise $XDG_CONFIG_HOME/hazel, or ~/.config/hazel when XDG_CONFIG_HOME isn't set.

Without either, Hazel stops at start with an error naming the path it looked for:

text
Hazel has no config: /home/you/.config/hazel/main.lua does not exist; create it, or set HAZEL_COMPOSITOR_DIR

HAZEL_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:

lua
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 keys

and main.lua uses it:

lua
local keys = require("keys")

keys.bind("super", "Return", function() Process.spawn("foot") end)
keys.bind("super shift", "r", function() wm:reload() end)
  • require runs a file once per load and hands every caller the same table. Two modules that both require("keys") add to the same bindings.
  • Every module sees the same globals: wm, Key, Process and the rest.
  • Image drawables with a relative path load 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:

sh
hazel --dump-typedefs > ~/.config/hazel/types.lua

Reloading ​

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 gets ready.
  • 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.

lua
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_window fires again for every open window. Don't count windows there.
  • first_content fires 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.id can. 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.

lua
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 emit returns, in the order they were added.
  • One that raises an error is logged, and the rest still run.