- Rust 86.1%
- Python 6.8%
- Shell 5.1%
- Nix 1.5%
- Dockerfile 0.5%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .direnv | ||
| .forgejo/workflows | ||
| ci | ||
| misc/niri-automate-git | ||
| src | ||
| tests | ||
| .envrc | ||
| .gitignore | ||
| AGENTS.md | ||
| build.rs | ||
| Cargo.lock | ||
| Cargo.toml | ||
| config.kdl | ||
| flake.lock | ||
| flake.nix | ||
| INSTRUCTIONS.md | ||
| README.md | ||
| shell.nix | ||
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
urgentandfloating. - 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.
nirion$PATH(this tool shells out toniri 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 --helpto see available CLI options.
Logging
Logging uses tracing.
- By default logs are printed to stdout.
- Override log file:
--log-file /path/to/file.logNIRI_AUTOMATE_LOG_FILE=/path/to/file.log
- Control verbosity with
RUST_LOG(viatracing-subscriberEnvFilter).
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-rulemust contain at least one action. match ...entries are OR’ed together:- If there are no
matchentries, the rule matches everything. - If there are
matchentries, at least one must match.
- If there are no
exclude ...entries are OR’ed together:- If any
excludematches, the whole rule is rejected.
- If any
Events
You can match by event string using the event="..." matcher property.
Currently supported events:
window-createdwindow-changed: initial window snapshots, metadata/layout changes, workspace changes and output/configuration updateswindow-closedwindow-focusedwindow-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": Rustregexpattern matched against the windowapp_idtitle="REGEX": Rustregexpattern matched against the window titleevent="window-created|window-closed|window-focused|window-blurred"is-active=true|false: true if the window is the active window in its workspaceis-focused=true|falseis-floating=true|falseis-urgent=true|falseat-startup=true|false: true during the first ~60s afterniri-automatestartsis-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_IDNIRI_AUTOMATE_FOCUSED_WINDOW_APP_IDNIRI_AUTOMATE_FOCUSED_WINDOW_TITLENIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_IDNIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_NAMENIRI_AUTOMATE_FOCUSED_WINDOW_WORKSPACE_OUTPUT
Previous Focused Window
NIRI_AUTOMATE_PREV_FOCUSED_WINDOW_IDNIRI_AUTOMATE_PREV_FOCUSED_WINDOW_APP_IDNIRI_AUTOMATE_PREV_FOCUSED_WINDOW_TITLENIRI_AUTOMATE_PREV_FOCUSED_WINDOW_WORKSPACE_IDNIRI_AUTOMATE_PREV_FOCUSED_WINDOW_WORKSPACE_NAMENIRI_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/righton 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; }
}