Skip to content

Socket ​

Hazel runs Lua chunks sent to a Unix socket, in the running config. hazel eval, hazel repl or any other program can send them.

sh
hazel eval 'wm.windows:count()'
hazel eval 'local ids = {} for _, w in ipairs(wm.windows:all()) do ids[#ids + 1] = w.app_id end return ids'
echo 'return wm.outputs:get(1).name' | hazel eval

The socket ​

The socket is $XDG_RUNTIME_DIR/hazel-$WAYLAND_DISPLAY.sock, where WAYLAND_DISPLAY is Hazel's own display.

  • Only the same user can connect.
  • When WAYLAND_DISPLAY is a path, only its last part is used.
  • hazel eval and hazel repl find it from their own environment. Run them in a terminal inside Hazel, or set WAYLAND_DISPLAY to Hazel's display.
  • A socket file left behind by a Hazel that is gone is replaced at start. If another Hazel still answers on that path, this one logs a warning and has no socket.
  • The file is removed when Hazel exits.

Sending a chunk ​

One chunk per connection. Write the chunk, shut down the sending side, and read the reply until the connection closes.

  • A chunk is UTF-8 text, at most 4 MiB.
  • The chunk is tried as return <chunk> first, so an expression such as wm.windows:count() returns its value. Otherwise it runs as statements.
  • It has an instruction budget of its own, and shows as socket in tracebacks.
  • Globals it sets stay for later chunks. A reload clears them.
  • print writes to Hazel's log, not to the reply. Return what you want to see.
  • Until a config has loaded, every chunk gets an error back.

The reply ​

The first line is ok or error.

After ok come the returned values, separated by tabs:

  • a string as it is
  • a table without a metatable with its contents: three levels deep, at most 50 entries each, named keys sorted, strings inside quoted, and a table inside itself as { <cycle> }
  • anything else through tostring, such as Rect { x: 0, y: 0, width: 1920, height: 1080 }

After error comes the error message with its traceback.

Commands ​

hazel eval [chunk] ​

Sends chunk, or standard input when there is no argument, and prints the reply without the ok line.

  • It prints nothing when the chunk returns nothing.
  • An error goes to standard error.
  • Exit code 0 when the chunk ran, 1 on a Lua error, 2 when Hazel can't be reached. WAYLAND_DISPLAY or XDG_RUNTIME_DIR not being set counts as that.

hazel repl ​

Reads chunks from standard input, one per line, and prints each reply.

  • A line that leaves a chunk unfinished, such as for i = 1, 3 do, waits for more lines.
  • The > and >> prompts show only when standard input is a terminal.
  • It stops at the end of input, or with exit code 2 when Hazel stops answering.