Event-driven window rules for the niri compositor, with KDL configuration for window placement, sizing, and custom commands.
  • Rust 86.1%
  • Python 6.8%
  • Shell 5.1%
  • Nix 1.5%
  • Dockerfile 0.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Vlad Verestiuc 2d9df286a0
All checks were successful
CI / arch-package (push) Has been skipped
CI / build (push) Successful in 36s
CI / test (push) Successful in 38s
CI / dist-artifacts (push) Has been skipped
CI / release (push) Has been skipped
CI / nix (push) Successful in 2m14s
Replace window fitting with a focused aspect-ratio rule
2026-11-02 18:53:36 +02:00
.direnv Rename the project 2026-11-02 11:15:45 +02:00
.forgejo/workflows Fixes nix 'Install checkout and publishing tools' 2026-11-02 12:21:07 +02:00
ci Add event-driven aspect-ratio window fitting 2026-11-02 15:20:44 +02:00
misc/niri-automate-git Rename the project 2026-11-02 11:15:45 +02:00
src Replace window fitting with a focused aspect-ratio rule 2026-11-02 18:53:36 +02:00
tests Replace window fitting with a focused aspect-ratio rule 2026-11-02 18:53:36 +02:00
.envrc Rename the project 2026-11-02 11:15:45 +02:00
.gitignore Nix build 2026-11-02 12:04:08 +02:00
AGENTS.md Document rules and align tests 2026-02-09 15:35:00 +02:00
build.rs Rename the project 2026-11-02 11:15:45 +02:00
Cargo.lock Rename the project 2026-11-02 11:15:45 +02:00
Cargo.toml Rename the project 2026-11-02 11:15:45 +02:00
config.kdl Add niri-rules config and rule engine 2026-02-04 03:28:07 +02:00
flake.lock Nix build 2026-11-02 12:04:08 +02:00
flake.nix Add event-driven aspect-ratio window fitting 2026-11-02 15:20:44 +02:00
INSTRUCTIONS.md Replace window fitting with a focused aspect-ratio rule 2026-11-02 18:53:36 +02:00
README.md Replace window fitting with a focused aspect-ratio rule 2026-11-02 18:53:36 +02:00
shell.nix Rename the project 2026-11-02 11:15:45 +02:00

niri-automate

niri-automate is a small rule engine for the niri Wayland compositor. It listens to the niri IPC event stream and executes user-defined window rules on specific events.

This project is focused on “window-rules, but with actions”: move/size windows and run arbitrary commands when a rule matches.

What It Can Do

  • Subscribe to niri events and evaluate rules.
  • Match windows by app-id, title, and a set of boolean flags.
  • Execute external commands (command).
  • Place a window before/after another matched window (place-before, place-after).
  • Toggle urgent and floating.
  • Move a window to an output (open-on-output).
  • Set window width/height (fixed pixels or proportion of output size).
  • Maintain a chosen window aspect ratio without changing its height.
  • Close a window (close-window).

Requirements

  • niri compositor running and IPC available.
  • niri on $PATH (this tool shells out to niri msg ...).
  • Rust toolchain only when building with Cargo.

Installation

Nix

With Nix flakes enabled:

nix profile add 'git+https://projects.verestiuc.com/vvlad/niri-automate.git#default'

The package supports Linux on x86-64 and ARM64. Nix builds from source when no matching prebuilt package is available.

For a NixOS flake, add the project as an input:

inputs.niri-automate.url = "git+https://projects.verestiuc.com/vvlad/niri-automate.git";

Then add its package in a module where the input is available:

environment.systemPackages = [
  inputs.niri-automate.packages.${pkgs.stdenv.hostPlatform.system}.default
];

Your flake lock file pins the installed revision. Update that input when you want a newer version.

Build From Source

cargo build --release
./target/release/niri-automate

Running

Run it inside your niri session:

niri-automate --config /path/to/config.kdl

Notes:

  • Default config path is config.kdl (override with --config).
  • Run niri-automate --help to see available CLI options.

Logging

Logging uses tracing.

  • By default logs are printed to stdout.
  • Override log file:
    • --log-file /path/to/file.log
    • NIRI_AUTOMATE_LOG_FILE=/path/to/file.log
  • Control verbosity with RUST_LOG (via tracing-subscriber EnvFilter).

Example:

RUST_LOG=niri_automate=debug ./target/release/niri-automate

Configuration (config.kdl)

The configuration format is KDL (parsed with knus).

At the top level you declare one or more window-rule blocks:

window-rule {
  match app-id="^Alacritty$" event="window-created"
  place-before app-id="^zen$"
  window-width { proportion 0.20; }
}

Rule Evaluation Semantics

  • A window-rule must contain at least one action.
  • match ... entries are OR’ed together:
    • If there are no match entries, the rule matches everything.
    • If there are match entries, at least one must match.
  • exclude ... entries are OR’ed together:
    • If any exclude matches, the whole rule is rejected.

Events

You can match by event string using the event="..." matcher property. Currently supported events:

  • window-created
  • window-changed: initial window snapshots, metadata/layout changes, workspace changes and output/configuration updates
  • window-closed
  • window-focused
  • window-blurred

Matchers

match and exclude nodes accept the same set of properties.

match app-id="^Alacritty$" title="fish" event="window-created" is-floating=false
exclude title="scratchpad"

Supported matcher properties:

  • app-id="REGEX": Rust regex pattern matched against the window app_id
  • title="REGEX": Rust regex pattern matched against the window title
  • event="window-created|window-closed|window-focused|window-blurred"
  • is-active=true|false: true if the window is the active window in its workspace
  • is-focused=true|false
  • is-floating=true|false
  • is-urgent=true|false
  • at-startup=true|false: true during the first ~60s after niri-automate starts
  • is-active-in-column=true|false: currently parsed, but not implemented yet (will never match)
  • is-window-cast-target=true|false: currently parsed, but not implemented yet (will never match)

Actions

Actions are the other statements inside a window-rule block. A rule may contain multiple actions; they run in order.

command

Executes an external command. The first argument is the program; the rest are arguments.

command "notify-send" "niri-automate" "matched!"

Environment variables passed to the command (when available):

Focused Window
  • NIRI_AUTOMATE_FOCUSED_WINDOW_ID
  • NIRI_AUTOMATE_FOCUSED_WINDOW_APP_ID
  • NIRI_AUTOMATE_FOCUSED_WINDOW_TITLE
  • NIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_ID
  • NIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_NAME
  • NIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_OUTPUT
Previous Focused Window
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_ID
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_APP_ID
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_TITLE
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_WORKSPACE_ID
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_WORKSPACE_NAME
  • NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_WORKSPACE_OUTPUT

place-before / place-after

Moves the current window so it ends up immediately before/after a target window. The target is described using matcher properties (same shape as match).

place-before app-id="^zen$"
place-after app-id="^zen$"

Notes:

  • This action relies on the window’s scrolling layout position (pos_in_scrolling_layout).
  • Under the hood it runs niri msg action move-column-left/right on the focused window.

urgent

Sets or unsets urgent state:

urgent true
urgent false

floating

Moves the window to floating or back to tiling:

floating true
floating false

open-on-output

Moves the window to a specific output:

open-on-output "DP-1"

window-width / window-height

Set window size either as fixed pixels or as a proportion of the window’s output size.

window-width  { fixed 480; }
window-height { fixed 320; }
window-width  { proportion 0.333; }

Notes:

  • Proportional sizes require output dimensions from niri msg -j outputs.
  • If the output name can be resolved but the output size isn’t known, the action will log an error and no-op.

aspect-ratio

Keep a window's current height and set its width to a chosen ratio:

window-rule {
  match app-id="^my-game$" event="window-created"
  match app-id="^my-game$" event="window-changed"
  aspect-ratio 16 9
}

The two positive integers are ratio components, not pixel dimensions. Width is rounded to the nearest pixel: a 1333-pixel-high window becomes 2370 pixels wide at 16:9. Other ratios work the same way, for example aspect-ratio 4 3.

This rule does not change height, focus or floating/tiling state, and does not add output margins or force the window to fit the monitor. Use native Niri window rules to choose tiling and default height separately.

The rule handles existing windows when the daemon starts, later resizes, workspace/output moves and configuration changes. It uses window IDs, not focus-and-resize commands. Stale events caused by its own sizing requests do not repeatedly resize a settled window. This is event-driven; no periodic polling or configure-round-trip sleeps are used.

Validate a rules file without connecting to a running compositor:

niri-automate --check-config --config /path/to/config.kdl

close-window

Closes the matched window via niri msg action close-window.

close-window

More KDL Examples

Multiple match And exclude

match entries are OR'ed together, exclude entries are OR'ed together.

window-rule {
  match event="window-created" app-id="^Alacritty$"
  match event="window-created" app-id="^kitty$"
  exclude title="scratchpad"

  window-width { fixed 420; }
}

Chain Multiple Actions

window-rule {
  match event="window-created" app-id="^Alacritty$"
  place-before app-id="^zen$"
  floating true
  urgent true
  window-width { proportion 0.20; }
  command "notify-send" "niri-automate" "placed + sized terminal"
}

Use at-startup

window-rule {
  match event="window-created" at-startup=true app-id="^Alacritty$"
  place-before app-id="^zen$"
}

Move To An Output

window-rule {
  match event="window-created" app-id="^Alacritty$"
  open-on-output "DP-1"
}

Fixed Size With Width + Height

window-rule {
  match event="window-created" app-id="^Alacritty$"
  window-width  { fixed 480; }
  window-height { fixed 320; }
}

Command Using Focus Env Vars

window-rule {
  match event="window-focused"
  command "bash" "-lc" "echo focused=$NIRI_AUTOMATE_FOCUSED_WINDOW_APP_ID prev=$NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_APP_ID"
}

Example: Multiple Rules

window-rule {
  match event="window-focused" app-id="^Alacritty$"
  command "notify-send" "focused alacritty"
}

window-rule {
  match event="window-created" app-id="^Alacritty$"
  place-before app-id="^zen$"
  window-width { proportion 0.20; }
}