diff --git a/hosts/common/configs/user/gui/astal/default.nix b/hosts/common/configs/user/gui/astal/default.nix index 3a03309..17e725f 100644 --- a/hosts/common/configs/user/gui/astal/default.nix +++ b/hosts/common/configs/user/gui/astal/default.nix @@ -39,5 +39,7 @@ ]; }; }; + + wayland.windowManager.hyprland.onMonitorChange.services = [ "ags.service" ]; }; } diff --git a/hosts/common/configs/user/gui/hyprland/monitors.sh b/hosts/common/configs/user/gui/hyprland/monitors.sh new file mode 100644 index 0000000..5e0b427 --- /dev/null +++ b/hosts/common/configs/user/gui/hyprland/monitors.sh @@ -0,0 +1,8 @@ +handle() { + # shellcheck disable=SC2086 + case $1 in + monitoradded*|monitorremoved*) systemctl --user restart $SERVICES ;; + esac +} + +socat -U - "UNIX-CONNECT:$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" | while read -r line; do handle "$line"; done diff --git a/hosts/common/configs/user/gui/hyprland/options.nix b/hosts/common/configs/user/gui/hyprland/options.nix index 3972d2d..17e0120 100644 --- a/hosts/common/configs/user/gui/hyprland/options.nix +++ b/hosts/common/configs/user/gui/hyprland/options.nix @@ -23,21 +23,45 @@ in default = ""; description = "Extra configuration lines to add to exec"; }; + + onMonitorChange.services = mkOption { + type = listOf str; + default = [ ]; + description = "Services to restart when a monitor is connected or disconnected"; + }; }; config = { - wayland.windowManager.hyprland.settings.exec-once = lib.meta.getExe ( - pkgs.writeShellApplication { - name = "init-hyprland"; - text = cfg.initExtraConfig; - } - ); + wayland.windowManager.hyprland.initExtraConfig = + lib.mkIf (cfg.onMonitorChange.services != [ ]) + "${ + lib.meta.getExe ( + pkgs.writeShellApplication { + name = "hyprland-monitor-change-services"; + runtimeInputs = with pkgs; [ + socat + systemd + ]; + runtimeEnv.SERVICES = lib.strings.concatStringsSep " " cfg.onMonitorChange.services; + text = builtins.readFile ./monitors.sh; + } + ) + } &"; - wayland.windowManager.hyprland.settings.exec = lib.meta.getExe ( - pkgs.writeShellApplication { - name = "reload-hyprland"; - text = cfg.reloadExtraConfig; - } - ); + wayland.windowManager.hyprland.settings = { + exec-once = lib.meta.getExe ( + pkgs.writeShellApplication { + name = "init-hyprland"; + text = cfg.initExtraConfig; + } + ); + + exec = lib.meta.getExe ( + pkgs.writeShellApplication { + name = "reload-hyprland"; + text = cfg.reloadExtraConfig; + } + ); + }; }; } diff --git a/hosts/common/configs/user/gui/swww/default.nix b/hosts/common/configs/user/gui/swww/default.nix index f59047e..399121a 100644 --- a/hosts/common/configs/user/gui/swww/default.nix +++ b/hosts/common/configs/user/gui/swww/default.nix @@ -64,5 +64,7 @@ in }; theme.reloadExtraConfig = "${themeSwww} &"; + + wayland.windowManager.hyprland.onMonitorChange.services = [ "swww.service" ]; }; }