From patchwork Mon Aug 19 15:30:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Arnaud Daby-Seesaram X-Patchwork-Id: 30183 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 6B0F527BBE9; Mon, 19 Aug 2024 16:33:12 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 797E727BBE2 for ; Mon, 19 Aug 2024 16:33:07 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sg4Mr-0008HA-81; Mon, 19 Aug 2024 11:32:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sg4Mn-0008B1-97 for guix-patches@gnu.org; Mon, 19 Aug 2024 11:32:21 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sg4Mn-0002MZ-03 for guix-patches@gnu.org; Mon, 19 Aug 2024 11:32:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=AzUWNs6SpG1b65NDuwV2U0ct9Xuhxc+Z8pBxNzffXVE=; b=phb5jbaNbkP4kJbAT2Y10wKB1C7236hAoVFdtZFMpKw8C5tG+Wc1Z4k5UeiuxhERnmPs7YXIw962sstnp+vELE36I9d/W+EKNoB/c9FLNg7qAG8WYtwObHTp9ogPdlUgvuupZtkdqAuS8ulJwRD7qtM8Mv2J+CUHsqX8TT/HpHjHUDBh8rR/KWfgTJnWC9g3Mbx0IpH7vD11ttV6qoZja0rW9YhXg9xSgOQRttmNtB9nUzFjkx8Hi3XVZK9S88COPptjrJ1/4pcRkX0hzHhawsW5u5NHTq0yqOu0wfZTVIRJAlZQIRGZEBblNTVypaHnpYdj+wDBYGGOhOqJ4eZ/WQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sg4NS-0007vt-Es for guix-patches@gnu.org; Mon, 19 Aug 2024 11:33:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72714] [PATCH] home: services: Add 'home-sway-service-type'. Resent-From: Arnaud Daby-Seesaram Original-Sender: "Debbugs-submit" Resent-CC: , guix-patches@gnu.org Resent-Date: Mon, 19 Aug 2024 15:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 72714 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 72714@debbugs.gnu.org Cc: Arnaud Daby-Seesaram , ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= , Tanguy Le Carrour X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= , Tanguy Le Carrour Received: via spool by submit@debbugs.gnu.org id=B.172408156030457 (code B ref -1); Mon, 19 Aug 2024 15:33:02 +0000 Received: (at submit) by debbugs.gnu.org; 19 Aug 2024 15:32:40 +0000 Received: from localhost ([127.0.0.1]:58974 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sg4N5-0007v9-Fh for submit@debbugs.gnu.org; Mon, 19 Aug 2024 11:32:40 -0400 Received: from lists.gnu.org ([209.51.188.17]:42746) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sg4N3-0007uy-3l for submit@debbugs.gnu.org; Mon, 19 Aug 2024 11:32:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sg4MM-00083x-VV for guix-patches@gnu.org; Mon, 19 Aug 2024 11:31:54 -0400 Received: from nanein.fr ([185.230.78.41]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sg4MH-0002Iv-KF for guix-patches@gnu.org; Mon, 19 Aug 2024 11:31:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nanein.fr; s=mail; t=1724081496; bh=0+mky3sZaS67yqtdrFFTKDWy+Jq5m9ridGbgyoMqM6o=; h=From:To:Cc:Subject:Date:From; b=mUDLqPmpagqoMW6OpPXXN0nQ80uyfbXK0HkhMqL69DOifkaLfj+juEi+cUet9qcEH mXi5yxH2tcEo9z2/v+XQvQgV6WSXqx9/C2+yw8kXrik/ZLgz4Nab136aDYU977IY2j S9DZhPhTW/9eW9Cfe6RmbWe3TRenb/0R2xDHD+J3ahglt7uuSxPl37/9wcBbbHfeGC MwcQmM4HiuQ+Z5yk1ItACdxL8yLMppgH/zS5n8uJkN7oEyYwPyib7cDTiabf9UoXci shEgEKiM1M/5fa5H5a6TYBIKP0PzQAYbKyxzzR1u5uyTPT/eqp/z9s/oobKgSjJSi7 qAkNKOONoAeN0eNrfIdzWrx05DKYFb4cyu0DBikq1Njly2efNIO6lx1P5JxFjhIzNj MMpFVg8vg+WlSHRw4W+WE1aoPnaISGVbbm1Pj/+IR35d7lN1zkXxU/uSQW2in8DKoH PqIofkShYEyh3p+GuXJhoyDRvmKdKgfu0ul6WSu9vTbEkKJssPylMkr8mTHy6Wbdo8 qcdkqURgOAl1ajhwkRlB458J+utJY4M07YI2B1JQ8Cgsb/f4V81KvzbdBn7TKtB2Pj 5wjRVEuL91KAnSKW2DMsKZPSQfIk6Y3wWGEVljd7xUDyMN9Dx1W/21HHVOivxeEOj2 YDIIcFzQQSnI4WUmww6oIw/8= Received: from localhost.localdomain (unknown [IPv6:2a01:e34:ec01:e860:f052:4634:b50a:d5f5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by nanein.fr (Postfix) with ESMTPSA id E32EC140266; Mon, 19 Aug 2024 17:31:36 +0200 (CEST) Date: Mon, 19 Aug 2024 17:30:42 +0200 Message-ID: <1e82e473639f21a2950a0827f156437ef1bc9c48.1724081442.git.ds-ac@nanein.fr> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Received-SPF: pass client-ip=185.230.78.41; envelope-from=ds-ac@nanein.fr; helo=nanein.fr X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Arnaud Daby-Seesaram X-ACL-Warn: , Arnaud Daby-Seesaram via Guix-patches X-Patchwork-Original-From: Arnaud Daby-Seesaram via Guix-patches via From: Arnaud Daby-Seesaram Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/home/services/sway.scm (home-sway-service-type): New variable. (sway-configuration): New configuration record. (sway-bar): New configuration record. (sway-output): New configuration record. (sway-input): New configuration record. (point): New configuration record. (sway-color): New configuration record. (sway-border-color): New configuration record. (flatten): New procedure. Change-Id: Ia29a4173ce3a887d5fac3f57ba3819a52f9624d3 --- This patch provides an *experimental* home service to configure sway. It defines a configuration record that contains fields for common options of sway (an extra-contents field that can be used for additional settings). The service also adds packages to the user profile (notably sway). Feedback is welcome ! Please note that (1) I only use Guile for Guix, so my code might be clumsy and non-idiomatic¹ and (2) the patch is experimental, and will need polishing before being merged. For example, I am not sure when I should allow G-expressions in the configuration. ¹: feel free to share your opinions on how to improve the code. I have not written any documentation at the time (even documentation strings in the configuration records are not well defined). I am waiting for the code structure and configuration records to be stable-ish to take the time to write a documentation. gnu/home/services/sway.scm | 622 +++++++++++++++++++++++++++++++++++++ 1 file changed, 622 insertions(+) create mode 100644 gnu/home/services/sway.scm base-commit: d3832de763777759eb1f56e20731cb39da4c7b5b diff --git a/gnu/home/services/sway.scm b/gnu/home/services/sway.scm new file mode 100644 index 0000000000..3f978d0bf0 --- /dev/null +++ b/gnu/home/services/sway.scm @@ -0,0 +1,622 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2024 Arnaud Daby-Seesaram +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (gnu home services sway) + #:use-module (guix monads) + #:use-module (guix modules) + #:use-module (guix gexp) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (ice-9 popen) + #:use-module (ice-9 format) + #:use-module (ice-9 match) + #:use-module (guix packages) + #:use-module (gnu services configuration) + #:use-module (gnu home services) + #:use-module (gnu packages freedesktop) + #:use-module (gnu packages xdisorg) + #:use-module (gnu packages image) + #:use-module (gnu packages gnome) + #:use-module (gnu packages wm) + #:use-module (gnu packages emacs) + #:use-module (gnu packages linux) + #:use-module (gnu packages base) + #:use-module (gnu packages suckless) + #:use-module (gnu packages glib) + #:export (;; Accessors and predicates do not need to be exported. + sway-configuration + sway-bar + sway-output + sway-input + point + sway-color + sway-border-color + home-sway-service-type)) + +;; Helper function. +(define (flatten l) + (let loop ((lst l) (acc '())) + (match lst + (() acc) + ((head . tail) + (loop tail (append acc head)))))) + + +;;; +;;; Default settings. +;;; + +(define %sway-default-variables + `((mod . "Mod4") + (left . "h") + (down . "j") + (up . "k") + (right . "l") + (term . ,#~(string-append #$st "/bin/st")) + (menu . ,#~(string-append #$dmenu "/bin/dmenu_run")))) + +(define %sway-default-gestures + `((swipe:3:right . "workspace next_on_output") + (swipe:3:left . "workspace prev_on_output") + (swipe:3:down . "move to scratchpad") + (swipe:3:up . "scratchpad show") + (pinch:2:clockwise + . "move container to workspace prev_on_output") + (pinch:2:counterclockwise + . "move container to workspace next_on_output") + (swipe:4:left . "exec alacritty") + (swipe:4:right . "exec qutebrowser") + (swipe:4:down . "exec st") + (swipe:4:up + . ,#~(string-append + "exec " #$emacs "/bin/emacsclient -a '" + #$libnotify "/bin/notify-send Oups \"emacs not loaded :/\"' -c")) + (pinch:inward+right . "resize shrink width") + (pinch:inward+left . "resize grow width") + (pinch:inward+down . "resize shrink height") + (pinch:inward+up . "resize grow height") + (pinch:outward+up . "move up") + (pinch:outward+down . "move down") + (pinch:outward+left . "move left") + (pinch:outward+right . "move right"))) + +(define %sway-default-keybindings + `(($mod+Shift+e + . ,#~(string-append + "exec " #$emacs + "/bin/emacsclient -a '" + #$libnotify "/bin/notify-send Oups \"emacs not loaded :/\"' -c")) + ($mod+Return . "exec $term") + ($mod+Shift+c . "kill") + ($mod+p . "exec $menu") + ($mod+Shift+r . "reload") + ($mod+Shift+q + . ,#~(string-append + "exec " #$sway "/bin/swaynag -t warning " + "-m 'You pressed the exit shortcut. Do you really want to exit sway?" + " This will end your Wayland session.'" + " -B 'Yes, exit sway' 'swaymsg exit'")) + ($mod+$left . "focus left") + ($mod+$right . "focus right") + ($mod+$up . "focus up") + ($mod+$down . "focus down") + ($mod+Shift+$left . "move left") + ($mod+Shift+$right . "move right") + ($mod+Shift+$up . "move up") + ($mod+Shift+$down . "move down") + ($mod+ampersand . "workspace number 1") + ($mod+eacute . "workspace number 2") + ($mod+quotedbl . "workspace number 3") + ($mod+apostrophe . "workspace number 4") + ($mod+parenleft . "workspace number 5") + ($mod+minus . "workspace number 6") + ($mod+egrave . "workspace number 7") + ($mod+underscore . "workspace number 8") + ($mod+ccedilla . "workspace number 9") + ($mod+agrave . "workspace number 10") + ($mod+Shift+ampersand . "move container to workspace number 1") + ($mod+Shift+eacute . "move container to workspace number 2") + ($mod+Shift+quotedbl . "move container to workspace number 3") + ($mod+Shift+apostrophe . "move container to workspace number 4") + ($mod+Shift+parenleft . "move container to workspace number 5") + ($mod+Shift+minus . "move container to workspace number 6") + ($mod+Shift+egrave . "move container to workspace number 7") + ($mod+Shift+underscore . "move container to workspace number 8") + ($mod+Shift+ccedilla . "move container to workspace number 9") + ($mod+Shift+agrave . "move container to workspace number 10") + ($mod+b . "splith") + ($mod+v . "splitv") + ($mod+s . "layout stacking") + ($mod+w . "layout tabbed") + ($mod+e . "layout toggle split") + ($mod+f . "fullscreen") + ($mod+Shift+space . "floating toggle") + ($mod+space . "focus mode_toggle") + ($mod+z . "focus parent") + ($mod+Shift+o . "move scratchpad") + ($mod+o . "scratchpad show") + ($mod+r . "mode \"resize\"") + ($mod+Shift+n . "bar mode toggle") + ($mod+Shift+b . "border toggle") + ($mod+tab . "workspace back_and_forth"))) + + +;;; +;;; Definition of configurations. +;;; + +(define (list-of-string-or-gexp? lst) + (every (lambda (elt) + (or (string? elt) + (gexp? elt))) + lst)) + +(define (list-of-packages? lst) + (every package? lst)) + +(define (bar-position? p) + (member p '(top bottom))) + +(define (hidden-state? st) + (member st '(hide show))) + +(define (string-or-symbol? s) + (or (string? s) + (symbol? s))) + +(define (strings? lst) + (every string? lst)) + +(define-maybe string (no-serialization)) +(define-maybe strings (no-serialization)) + +(define-configuration/no-serialization sway-input + (identifier + (string-or-symbol '*) + "Identifier of the input.") + (xkb-layout + maybe-string + "Keyboard layout.") + (xkb-model + maybe-string + "Keyboard model.") + (xkb-options + maybe-strings + "Keyboard options.") + (xkb-variant + maybe-string + "Keyboard layout variant.") + (extra-content + (list-of-string-or-gexp '()) + "Lines to add at the end of the configuration file.")) + +(define (sway-inputs? lst) + (every sway-input? lst)) + +(define-configuration/no-serialization sway-border-color + (border + string + "Border color.") + (background + string + "Background color.") + (text + string + "Text color.")) + +(define-maybe sway-border-color (no-serialization)) + +(define-configuration/no-serialization sway-color + (background + maybe-string + "Background color of the bar.") + (statusline + maybe-string + "Text color of the separator.") + (focused-background + maybe-string + "Background color of the bar on the currently focused monitor.") + (focused-statusline + maybe-string + "Text color of the statusline on the currently focused monitor.") + (focused-workspace + maybe-sway-border-color + "...") + (active-workspace + maybe-sway-border-color + "...") + (inactive-workspace + maybe-sway-border-color + "...") + (urgent-workspace + maybe-sway-border-color + "...") + (binding-mode + maybe-sway-border-color + "...")) + +(define-maybe sway-color (no-serialization)) + +(define-configuration/no-serialization sway-bar + (identifier + (symbol 'bar0) + "Identifier of the bar.") + (position + (bar-position 'top) + "Position of the bar.") + (hidden-state + (hidden-state 'hide) + "Hidden state.") + (binding-mode-indicator + (boolean #t) + "Binding indicator.") + (colors + maybe-sway-color + "Color palette of the bar.") + (status-command + (file-like + (program-file + "sway-bar-status" + (with-imported-modules + (source-module-closure + '((ice-9 textual-ports) (ice-9 regex) (ice-9 popen) (ice-9 format) + (srfi srfi-19))) + #~(begin + (use-modules (ice-9 textual-ports) + (ice-9 format) + (ice-9 popen) + (ice-9 regex) + (srfi srfi-19)) + (let loop () + (let* ((date (date->string (current-date) "~a ~D ~H:~M:~S")) + (batline (let* ((p (open-pipe* + OPEN_READ + #$(file-append acpi "/bin/acpi") "-b")) + (bat (get-line p))) + (close-pipe p) + bat)) + (bat (match:substring (string-match "[0-9]+%" batline)))) + (format #t "~a - ~a~%~!" bat date) + (sleep 1) + (loop))))))) + "Status command. It must be file-like.")) + +(define-configuration/no-serialization point + (x integer "X coordinate.") + (y integer "Y coordinate.")) + +(define-maybe point (no-serialization)) +(define-maybe file-like (no-serialization)) + +(define-configuration/no-serialization sway-output + (identifier + (string-or-symbol '*) + "Identifier of the output.") + (resolution + maybe-string + "Mode of the monitor.") + (position + maybe-point + "Position of the monitor.") + (bg + maybe-file-like + "Background image.") + (extra-content + (list-of-string-or-gexp '()) + "Extra lines.")) + +(define (sway-outputs? lst) + (every sway-output? lst)) + +(define-configuration/no-serialization sway-configuration + (keybindings + (alist %sway-default-keybindings) + "Keybindings.") + (gestures + (alist %sway-default-gestures) + "Gestures.") + (packages + (list-of-packages + (list sway swaylock waybar swaybg slurp grim dmenu bemenu + dbus xdg-desktop-portal-wlr xdg-desktop-portal)) + "List of packages to add to the profile.") + (variables + (alist %sway-default-variables) + "Variables declared at the beginning of the file.") + (inputs + (sway-inputs (list (sway-input))) + "Inputs.") + (outputs + (sway-outputs '()) + "Outputs.") + (bar + (sway-bar (sway-bar)) + "Bar configuration.") + (execs + (list-of-string-or-gexp '()) + "Programs to execute at startup time.") + (extra-content + (list-of-string-or-gexp '()) + "Lines to add at the end of the configuration file.")) + + +;;; +;;; Serialization functions. +;;; + +(define (serialize-keybinding var) + (let ((name (symbol->string (car var))) + (value (cdr var))) + #~(string-append "bindsym " #$name " " #$value))) + +(define (serialize-gesture var) + (let ((name (symbol->string (car var))) + (value (cdr var))) + #~(string-append "bindgesture " #$name " " #$value))) + +(define (serialize-variable var) + (let ((name (symbol->string (car var))) + (value (cdr var))) + #~(string-append "set $" #$name " " #$value))) + +(define (serialize-exec exe) + #~(string-append "exec " #$exe)) + +(define (serialize-output out) + (let* ((pre-ident (sway-output-identifier out)) + (ident (if (symbol? pre-ident) + (symbol->string pre-ident) + (string-append "\"" pre-ident "\""))) + (bg (sway-output-bg out)) + (resolution (sway-output-resolution out)) + (position (sway-output-position out)) + (extra-content (sway-output-extra-content out))) + (append + (filter + (lambda (elt) (not (eq? elt %unset-value))) + (list + ;; Beginning of the block. + #~(string-append "output " #$ident " {") + ;; Optional elements. + (if (eq? %unset-value bg) + %unset-value + #~(string-append " bg " #$bg " fill")) + (if (eq? %unset-value resolution) + %unset-value + (string-append " resolution " resolution)) + (if (eq? %unset-value position) + %unset-value + (string-append " position " (number->string (point-x position)) + " " (number->string (point-y position)))))) + extra-content + ;; End of the block. + '("}")))) + +(define-inlinable (add-line-if prefix value) + (if (eq? %unset-value value) + %unset-value + (string-append prefix " " value))) + +(define (serialize-input input) + (let* ((pre-ident (sway-input-identifier input)) + (ident (if (symbol? pre-ident) + (symbol->string pre-ident) + (string-append "\"" pre-ident "\""))) + (xkb-layout (sway-input-xkb-layout input)) + (xkb-model (sway-input-xkb-model input)) + (xkb-variant (sway-input-xkb-variant input)) + (xkb-options (sway-input-xkb-options input))) + (append + (filter + (lambda (elt) (not (eq? elt %unset-value))) + (list + (string-append "input " ident " {") + ;; Optional. + (add-line-if " xkb_layout" xkb-layout) + (add-line-if " xkb_model" xkb-model) + (add-line-if " xkb_variant" xkb-variant) + (if (eq? %unset-value xkb-options) + %unset-value + (string-concatenate (cons " xkb_options " xkb-options))))) + (map (lambda (s) + (string-append (string-pad "" 4) s)) + (sway-input-extra-content input)) + '("}")))) + +(define (serialize-colors colors) + (define (add-border-color-if name val) + (if (eq? %unset-value val) + %unset-value + (string-append + name + " " (sway-border-color-border val) + " " (sway-border-color-background val) + " " (sway-border-color-text val)))) + (let ((background (sway-color-background colors)) + (statusline (sway-color-statusline colors)) + (focused-background (sway-color-focused-background colors)) + (focused-statusline (sway-color-focused-statusline colors)) + (focused-workspace (sway-color-focused-workspace colors)) + (active-workspace (sway-color-active-workspace colors)) + (inactive-workspace (sway-color-inactive-workspace colors)) + (urgent-workspace (sway-color-urgent-workspace colors)) + (binding-mode (sway-color-binding-mode colors))) + (filter + (lambda (elt) (not (eq? elt %unset-value))) + (list + (add-line-if "background" background) + (add-line-if "statusline" statusline) + (add-line-if "focused_background" focused-background) + (add-line-if "focused_statusline" focused-statusline) + (add-border-color-if "focused_workspace" focused-workspace) + (add-border-color-if "active_workspace" active-workspace) + (add-border-color-if "inactive_workspace" inactive-workspace) + (add-border-color-if "urgent_workspace" urgent-workspace) + (add-border-color-if "binding_mode" binding-mode))))) + +(define (make-sway-config conf) + (let* ((extra (sway-configuration-extra-content conf)) + (bar (sway-configuration-bar conf))) + + (with-imported-modules + (source-module-closure + '((ice-9 popen) (ice-9 match) (ice-9 format) (guix monads))) + (computed-file + "sway-config" + #~(begin + (use-modules (ice-9 format) (ice-9 popen) (ice-9 match) + (guix monads)) + ;; Helper functions to pretty-print the configuration file. + (define (line s) + (lambda (port) + (lambda (i) + (format port "~a~a~%" (string-pad "" i) s) + (values port i)))) + (define (lines lst) + (lambda (port) + (lambda (i) + (let loop ((l lst)) + (match l + (() #t) + ((head . tail) + (format port "~a~a~%" (string-pad "" i) + (if (list? head) + (string-concatenate head) + head)) + (loop tail)))) + (values port i)))) + (define-syntax line* + (syntax-rules () + ((line* elt ...) + (lines (list elt ...))))) + (define-syntax line2 + (syntax-rules () + ((line2 a b) + (line (string-append a b))))) + (define (indent k) + (lambda (port) + (lambda (i) + (values port (+ i k))))) + (define (begin-block name) + (lambda (port) + (lambda (i) + (format port "~a~a {~%" (string-pad "" i) name) + (values port (+ i 4))))) + (define (end-block) + (lambda (port) + (lambda (i) + (let ((i (- i 4))) + (format port "~a}~%" (string-pad "" i)) + (values port i))))) + + (let* ((file #$output) + (port (open-output-file #$output))) + ;; The previous iteration of this work computed the content of the + ;; configuration file as a string. The state monad was used to + ;; keep track of the state during the computation. + ;; However, this first approach was too expensive. Now that I use + ;; `computed-file', the "computed value" is the constant port to + ;; which I write. Thus, using the state monad is overkill here. + ;; It can be replaced by the identity monad, where the value being + ;; passed around is the pair of `port' and the indentation level. + (run-with-state + (with-monad + %state-monad + (>>= (return port) + ;; Header of the configuration file. + (line* + "#####################################" + "### Auto-generated configuration. ###" + "#####################################" + "# DO NOT EDIT MANUALLY." "") + (line* + "# Variables." + "# ==========" + #$@(map serialize-variable + (sway-configuration-variables conf)) "") + (line* + "# Outputs." + "# ========" + #$@(flatten + (map serialize-output + (sway-configuration-outputs conf))) "") + (line* + "# Inputs." + "# =======" + #$@(flatten + (map serialize-input + (sway-configuration-inputs conf))) "") + (line* "# Bar configuration." + "# ==================") + (begin-block + (string-append + "bar " #$(symbol->string + (sway-bar-identifier bar)))) + (line2 "position " #$(symbol->string + (sway-bar-position bar))) + (line2 "hidden_state " + #$(symbol->string + (sway-bar-hidden-state bar))) + (line2 "status_command " + #$(sway-bar-status-command bar)) + (line2 "binding_mode_indicator " + #$(if (sway-bar-binding-mode-indicator bar) + "true" "false")) + (begin-block "colors") + (line* + #$@(serialize-colors (sway-bar-colors bar))) + (end-block) ;; colors + (end-block) ;; bar + (line* + "" "# Extra configuration content." + "# ============================" + #$@extra "") + (line* + "# Keybindings." + "# ============" + #$@(map serialize-keybinding + (sway-configuration-keybindings conf)) + "") + (line* + "# Gestures." + "# =========" + #$@(map serialize-gesture + (sway-configuration-gestures conf)) "") + (line* + "# Programs to execute." + "# ====================" + #$@(map serialize-exec + (sway-configuration-execs conf))))) + 0))))))) + +(define (sway-configuration->file sway-conf) + `((".config/sway/config" + ,(make-sway-config + sway-conf)))) + +(define home-sway-service-type + (service-type + (name 'home-sway-config) + (extensions + (list (service-extension home-files-service-type + sway-configuration->file) + (service-extension home-profile-service-type + sway-configuration-packages))) + (description "Configure Sway by providing a file +@file{~/.config/sway/config}.") + (default-value (sway-configuration))))