From 76e5fddc5097b735b07f49ee1b65667d4de656bb Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 24 Aug 2024 20:36:52 +0200 Subject: [PATCH] feat: add swayidle, use rde sway config serialization --- home/home-configuration.scm | 75 ++++------ home/modules/ruther/home/dwl/scripts.scm | 145 +------------------ home/modules/ruther/home/services/kanshi.scm | 141 ++++++------------ 3 files changed, 70 insertions(+), 291 deletions(-) diff --git a/home/home-configuration.scm b/home/home-configuration.scm index 111d45c..e0f466a 100644 --- a/home/home-configuration.scm +++ b/home/home-configuration.scm @@ -11,6 +11,7 @@ (gnu packages gnupg) (gnu packages emacs) (gnu packages shellutils) + (gnu packages wm) (gnu packages freedesktop) (gnu services) (gnu home services fontutils) @@ -199,7 +200,7 @@ (service home-wayland-display-service-type) (service home-wlr-services-service-type - '(waybar kanshi emacs gammastep + '(waybar kanshi emacs gammastep swayidle blueman-applet network-manager-applet)) (service home-waybar-service-type (home-waybar-configuration @@ -212,54 +213,30 @@ (service home-gammastep-service-type) (service home-kanshi-service-type (home-kanshi-configuration - (config (kanshi-configuration - (profiles - (list - (kanshi-profile - (name "notebook") - (outputs - (list - (kanshi-output - (name "eDP-1") - (enable #t))))) - (kanshi-profile - (name "docked") - (outputs - (list - (kanshi-output - (name "eDP-1") - (enable #f)) - (kanshi-output - (name "DP-9") - (position '(0 . 0)) - (enable #t)) - (kanshi-output - (name "DP-8") - (position '(3840 . 0)) - (enable #t)) - (kanshi-output - (name "DP-7") - (position '(1920 . 0)) - (enable #t))))) - (kanshi-profile - (name "docked-alt") - (outputs - (list - (kanshi-output - (name "eDP-1") - (enable #f)) - (kanshi-output - (name "DP-12") - (position '(0 . 0)) - (enable #t)) - (kanshi-output - (name "DP-11") - (position '(3840 . 0)) - (enable #t)) - (kanshi-output - (name "DP-10") - (position '(1920 . 0)) - (enable #t))))))))))) + (config + '((profile notebook + ((output eDP-1 enable))) + (profile docked + ((output eDP-1 disable) + (output DP-9 enable pos 0,0) + (output DP-8 enable pos 3840,0) + (output DP-7 enable pos 1920,0))) + (profile docked-alt + ((output eDP-1 disable) + (output DP-12 enable pos 0,0) + (output DP-11 enable pos 3840,0) + (output DP-10 enable pos 1920,0))))))) + (service home-swayidle-service-type + (home-swayidle-configuration + (config + `((before-sleep ,#~(string-append "\"" #$swaylock "/bin/swaylock -fF\"")) + (lock ,#~(string-append "\"" #$swaylock "/bin/swaylock -fF\"")) + (timeout 30 + ,#~(string-append "\"" #$mako "/bin/makoctl mode -a idle\"") + resume + ,#~(string-append "\"" #$mako "/bin/makoctl mode -r idle\"")) + (timeout 1800 + ,#~(string-append "\"" #$elogind "/bin/loginctl suspend\"")))))) (service home-xdg-desktop-portal-service-type (xdg-desktop-portal-configuration diff --git a/home/modules/ruther/home/dwl/scripts.scm b/home/modules/ruther/home/dwl/scripts.scm index 3adf80d..c00920c 100644 --- a/home/modules/ruther/home/dwl/scripts.scm +++ b/home/modules/ruther/home/dwl/scripts.scm @@ -20,88 +20,6 @@ start-dwl)) -(define-record-type* - wlr-output make-wlr-output - wlr-output? - (name wlr-output-name) - (position wlr-output-position - (default #f)) - (enable? wlr-output-enable? - (default #t))) - -(define-record-type* - wlr-randr-config make-wlr-randr-config - wlr-randr-config? - (outputs wlr-randr-config-outputs)) - -(define-record-type* - swayidle-timeout make-swayidle-timeout - swayidle-timeout? - (time swayidle-timeout-time) - (command swayidle-timeout-command) - (resume swayidle-timeout-resume - (default #f))) - -(define-record-type* - swayidle-config swaidle-config - swayidle-config? - (wait? swayidle-config-wait? - (default #f)) - (lock swayidle-config-lock) - (before-sleep swayidle-config-before-sleep) - (timeouts swayidle-config-timeouts)) - -(define-public (build-wlr-randr-args config) - (let ((outputs (wlr-randr-config-outputs config))) - (apply append - (map - (lambda (output) - (append - `("--output" - ,(wlr-output-name output) - ,(if (wlr-output-enable? output) - "--on" - "--off") - ) - (if (wlr-output-position output) - `("--pos" - ,(format #f - "~a,~a" - (car (wlr-output-position output)) - (cdr (wlr-output-position output)))) - '()))) - outputs)))) - -(define-public (invoke-wlr-randr config) - #~(apply - invoke - (cons* (string-append #$wlr-randr "/bin/wlr-randr") - '#$(build-wlr-randr-args config)))) - -(define-public (build-swayidle-timeout-args timeout) - (cons* "timeout" - (format #f "~a" (swayidle-timeout-time timeout)) - (swayidle-timeout-command timeout) - (if (swayidle-timeout-resume timeout) - `("resume" - ,(swayidle-timeout-resume timeout)) - '()))) - -(define-public (build-swayidle-args config) - (let* ((mapped `((,swayidle-config-wait? . '("-w")) - (,swayidle-config-before-sleep . ("before-sleep" ,(swayidle-config-before-sleep config))) - (,swayidle-config-lock . ("lock" ,(swayidle-config-lock config))) - (,swayidle-config-timeouts . ,(apply append (map build-swayidle-timeout-args (swayidle-config-timeouts config)))))) - (filtered (filter (lambda (x) (not (nil? ((car x) config)))) - mapped))) - (apply append (map cdr filtered)))) - -(define-public (invoke-swayidle config) - #~(apply - invoke - (cons* (string-append #$swayidle "/bin/swayidle") - '#$(build-swayidle-args config)))) - (define wlopm-all-screens-gexp #~(begin (use-modules @@ -132,56 +50,6 @@ #$(program-file "wlopm-all-screens" wlopm-all-screens-gexp) (string-append #$output "/bin/wlopm-all-screens"))))) -;; Rules - -(define-public preferred-output-config - (wlr-randr-config - (outputs - (list - (wlr-output - (name "DP-9") - (position '(0 . 0)) - (enable? #t)) - (wlr-output - (name "DP-8") - (position '(3840 . 0)) - (enable? #t)) - (wlr-output - (name "DP-7") - (position '(1920 . 0)) - (enable? #t)) - (wlr-output - (name "eDP-1") - (enable? #f)))))) - -(define-public fallback-output-config - (wlr-randr-config - (outputs - (list - (wlr-output - (name "eDP-1") - (enable? #t)))))) - -(define-public idle-rules - (swayidle-config - (wait? #t) - - (lock "swaylock -Ff") - (before-sleep "swaylock -Ff") - - (timeouts - (list - (swayidle-timeout - (time 30) - (command "makoctl mode -a idle") - (resume "makoctl mode -r idle")) - (swayidle-timeout - (time 300) - (command "wlopm-all-screens off") - (resume "wlopm-all-screens on")) - (swayidle-timeout - (time 1800) - (command "loginctl suspend")))))) (define (dwl-startup-cmd-gexp waybar) #~(begin @@ -201,18 +69,7 @@ (string-append #$shepherd "/bin/herd") "start" "wlr-services" - (getenv "WAYLAND_DISPLAY")) - - ;; TODO: how would I go about putting the correct paths inside of idle-rules without PATH env? - (set-path-environment-variable - "PATH" - (map (lambda (x) (string-append x "/bin")) - '#$(list swayidle - (computed-file "wlopm-all-screens" wlopm-all-screens-bin-gexp) - mako - elogind - swaylock)) - #$(invoke-swayidle idle-rules)))) + (getenv "WAYLAND_DISPLAY")))) (define (start-dwl-gexp dwl waybar) #~(begin diff --git a/home/modules/ruther/home/services/kanshi.scm b/home/modules/ruther/home/services/kanshi.scm index b24bb10..13bde87 100644 --- a/home/modules/ruther/home/services/kanshi.scm +++ b/home/modules/ruther/home/services/kanshi.scm @@ -1,6 +1,6 @@ (define-module (ruther home services kanshi) #:use-module (guix gexp) - #:use-module (gnu packages wm) + #:use-module (gnu packages freedesktop) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services shepherd) @@ -9,109 +9,26 @@ #:use-module (gnu home services) #:use-module (gnu home services shepherd) #:use-module (ruther home services wayland) - #:export (kanshi-configuration - kanshi-profile - kanshi-output - - home-kanshi-configuration)) - -(define-maybe string) -(define-maybe cons) -(define-maybe float) -(define-maybe boolean) -(define-maybe list-of-strings) -(define-maybe position) - -(define (position? value) - (and - (pair? value) - (integer? (car value)) - (integer? (cdr value)))) - -(define (serialize-name name value) - (format #f - "~a " - value)) - -(define (serialize-position name value) - (format #f - "~a ~a,~a " - name - (car value) - (cdr value))) - -(define (serialize-enable name value) - (if value "enable " "disable ")) - -(define (serialize-boolean name value) - (format #f - "~a ~a " - name - (if value "yes" "no"))) - -(define (list-of-strings? lst) - (every string? lst)) - -(define (serialize-list-of-strings name value) - (string-join " " - (map serialize-string value))) - -(define-configuration kanshi-output - (name string "Identifier of the output" (serializer serialize-name)) - (mode maybe-string "The mode to use") - (position maybe-position "A cons containing x in car, and y in cdr") - (enable maybe-boolean "Whether to enable the output" (serializer serialize-enable)) - (scale maybe-float "Scale of the output mode") - (adaptive-sync maybe-boolean "Whether to enable adaptive sync for the output")) - -(define (list-of-kanshi-outputs? lst) - (every kanshi-output? lst)) - -(define (serialize-list-of-kanshi-outputs lst) - #~(string-join - (map (lambda (x) (string-append " output " x)) - (list #$@(map - (cut serialize-configuration <> kanshi-output-fields) - lst))) - "\n")) - -(define-configuration/no-serialization kanshi-profile - (name - string - "Name of the profile") - (outputs - list-of-kanshi-outputs - "List of the outputs for this profile") - ;; (commands - ;; maybe-list-of-strings - ;; "List of the commands to execute. These map to `exec`") - ) - -(define (list-of-kanshi-profiles? lst) - (every kanshi-profile? lst)) - -(define-public (serialize-kanshi-profile config) - #~(format #f - "profile ~a {~%~a~%}~%~%" - #$(kanshi-profile-name config) - #$(serialize-list-of-kanshi-outputs (kanshi-profile-outputs config)))) - -(define (serialize-list-of-kanshi-profiles name lst) - #~(string-append #$@(map serialize-kanshi-profile lst))) - -(define-configuration kanshi-configuration - (profiles - list-of-kanshi-profiles - "List of the profiles inside of the kanshi configuration.")) + #:export (home-kanshi-configuration + home-swayidle-configuration)) (define-public (serialize-kanshi-configuration config) - (mixed-text-file + (apply + mixed-text-file "config" - (serialize-configuration config kanshi-configuration-fields))) + ((@@ (rde home services wm) serialize-sway-config) config))) + +(define-public (serialize-swayidle-configuration config) + (apply + mixed-text-file + "config" + ((@@ (rde home services wm) serialize-sway-config) config))) + +(define sway-config? (@ (rde home services wm) sway-config?)) (define-configuration/no-serialization home-kanshi-configuration (kanshi (file-like kanshi) "Kanshi package to use.") - (config kanshi-configuration "Configuration of the kanshi program itself. Goes into .config/kanshi/config")) + (config sway-config "Configuration of the kanshi program itself. Goes into .config/kanshi/config. See `man 5 kanshi`")) (define (home-kanshi-shepherd-services config) (list @@ -136,3 +53,31 @@ home-kanshi-shepherd-services) (service-extension home-wayland-display-service-type (const #f)))))) + +(define-configuration/no-serialization home-swayidle-configuration + (swayidle (file-like swayidle) "Swayidle package to use.") + (config sway-config "Configuration of the swayidle program itself. Goes into .config/swayidle/config. See `man 5 swayidle`")) + +(define (home-swayidle-shepherd-services config) + (list + (shepherd-service + (documentation "Run the swayidle daemon for managing commands executed when idle.") + (provision '(swayidle)) + (requirement '(wayland-display)) + (start #~(make-forkexec-constructor + (list #$(file-append + (home-swayidle-configuration-swayidle config) + "/bin/swayidle") + "-wC" + #$(serialize-swayidle-configuration (home-swayidle-configuration-config config))))) + (stop #~(make-kill-destructor))))) + +(define-public home-swayidle-service-type + (service-type + (name 'home-swayidle) + (description "A service for configuring and running swayidle through Shepherd") + (extensions + (list (service-extension home-shepherd-service-type + home-swayidle-shepherd-services) + (service-extension home-wayland-display-service-type + (const #f)))))) -- 2.48.1