moonarch/defaults/bin/moonarch-waybar
nevaforget 2363e76b4a
All checks were successful
Update PKGBUILD version / update-pkgver (push) Successful in 4s
feat: add moonarch-waybar config merger wrapper
Waybar's include directive cannot merge arrays, making per-machine
module customization impossible without duplicating the entire config.

moonarch-waybar merges an optional ~/.config/waybar/userconfig with
the system config, supporting prepend/append on module arrays and
object merge for module definitions. Generates user style.css with
@import of system styles on first run.

System waybar config converted from JSONC to valid JSON for jq
compatibility. Niri startup and hotkey updated to use the wrapper.
2026-04-09 17:04:24 +02:00

60 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# ABOUTME: Wrapper that merges system waybar config with per-machine userconfig.
# ABOUTME: Handles array prepend/append that waybar's native include cannot do.
SYSTEM_CONFIG="/etc/xdg/waybar/config"
SYSTEM_STYLE="/etc/xdg/waybar/style.css"
USER_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/waybar"
USERCONFIG="$USER_DIR/userconfig"
OUTPUT="$USER_DIR/config"
USER_STYLE="$USER_DIR/style.css"
merge_config() {
mkdir -p "$USER_DIR"
if ! jq -s '
.[0] as $sys | .[1] as $user |
(($user.prepend // {}) | to_entries) as $prepends |
(($user.append // {}) | to_entries) as $appends |
$sys |
reduce $prepends[] as $p (.;
.[$p.key] = ($p.value + (.[$p.key] // []))
) |
reduce $appends[] as $a (.;
.[$a.key] = ((.[$a.key] // []) + $a.value)
) |
($user | del(.prepend) | del(.append)) as $extras |
. * $extras
' "$SYSTEM_CONFIG" "$USERCONFIG" > "${OUTPUT}.tmp" 2>&1; then
local err
err=$(cat "${OUTPUT}.tmp")
rm -f "${OUTPUT}.tmp"
logger -t moonarch-waybar "Config merge failed: $err"
notify-send -u critical "moonarch-waybar" "Config merge failed — using system config.\n$err"
return 1
fi
mv "${OUTPUT}.tmp" "$OUTPUT"
}
bootstrap_style() {
if [[ ! -f "$USER_STYLE" ]]; then
mkdir -p "$USER_DIR"
cat > "$USER_STYLE" << 'CSS'
/* Generated by moonarch-waybar — add custom styles below */
@import url("/etc/xdg/waybar/style.css");
CSS
fi
}
if [[ -f "$USERCONFIG" ]]; then
if [[ ! -f "$OUTPUT" ]] ||
[[ "$USERCONFIG" -nt "$OUTPUT" ]] ||
[[ "$SYSTEM_CONFIG" -nt "$OUTPUT" ]]; then
merge_config
fi
bootstrap_style
fi
exec waybar "$@"