From patchwork Wed Aug 16 17:43:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 52950 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 8985327BBEA; Wed, 16 Aug 2023 18:45:32 +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=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham 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 9DCE827BBE2 for ; Wed, 16 Aug 2023 18:45:29 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qWKZv-00046J-Du; Wed, 16 Aug 2023 13:45:08 -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 1qWKZq-000464-Bd for guix-patches@gnu.org; Wed, 16 Aug 2023 13:45:02 -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 1qWKZq-0005Cf-0M for guix-patches@gnu.org; Wed, 16 Aug 2023 13:45:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qWKZp-0000Vt-Rd for guix-patches@gnu.org; Wed, 16 Aug 2023 13:45:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65343] [PATCH] home: services: Add 'x11-display' service. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: , guix-patches@gnu.org Resent-Date: Wed, 16 Aug 2023 17:45:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65343 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65343@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= , ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by submit@debbugs.gnu.org id=B.16922078541893 (code B ref -1); Wed, 16 Aug 2023 17:45:01 +0000 Received: (at submit) by debbugs.gnu.org; 16 Aug 2023 17:44:14 +0000 Received: from localhost ([127.0.0.1]:42035 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qWKZ3-0000UT-Ua for submit@debbugs.gnu.org; Wed, 16 Aug 2023 13:44:14 -0400 Received: from lists.gnu.org ([2001:470:142::17]:38834) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qWKZ0-0000UD-Q9 for submit@debbugs.gnu.org; Wed, 16 Aug 2023 13:44:12 -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 1qWKYv-0003uk-Ee for guix-patches@gnu.org; Wed, 16 Aug 2023 13:44:05 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qWKYu-00056e-Cl; Wed, 16 Aug 2023 13:44:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=EDkNBdMmWiQJ2cFWH+ok30iDoKP/xvyrFsRvLoBCUf8=; b=nscRo4agFlDI/8 bMFuRI1iucfgBKrdulY17LWl27M5J94ktWE6r40ahQ9rwBH2zYCn2DGfLCyWtu5/k3S954GfBInEZ p41VzpVXxr7sHRllEHwg2GDS8BHzsw4FxApTzg3WpbBNR7yOFkV++/hJGwqfdZGgj+xMwWkWxWacQ E/zsolwf5lStwv594syVOW0XjPWMBVRF4tMiLujUswIfV8Hn7M/8ttxxTYZFR2rvhrB01DpTaSs3V DJqzHrEMeqzFCK3D599nSwA7yrE4jXR8o/C3Aq6Sg34Xq/z3eKR4x9op/FViPSQZ2DhSFotnQC58v Kb0K/ncb58WzDpRrOsdg==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Wed, 16 Aug 2023 19:43:51 +0200 Message-ID: X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 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: , 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/desktop.scm (x11-shepherd-service): New procedure. (home-x11-service-type): New variable. (redshift-shepherd-service): Add 'requirement' field. (home-redshift-service-type): Extend 'home-x11-service-type'. * doc/guix.texi (Desktop Home Services): Document it. --- doc/guix.texi | 22 ++++++++++ gnu/home/services/desktop.scm | 82 ++++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 5 deletions(-) Hello Guix! This is an attempt to fix a longstanding issue with Home services that depend on X11: how can we make sure that (1) they are not started when X is not running, and (2) they get the correct ‘DISPLAY’ variable. It’s a bit of a hack (the idea came up during a discussion on IRC a few days ago), but it does the job. I guess it could be extended to Wayland as well, but I’m not familiar with it. Thoughts? Ludo’. base-commit: 880ada0bdb9e694573ec42200d48658b27744b9b diff --git a/doc/guix.texi b/doc/guix.texi index 22590b4f9c..a99ef8e5e8 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -44067,6 +44067,28 @@ Desktop Home Services may find useful on ``desktop'' systems running a graphical user environment such as Xorg. +@cindex X Window, for Guix Home services +@cindex X11, in Guix Home +@defvar home-x11-service-type +This is the service type representing the X Window graphical display +server (also referred to as ``X11''). + +X Window is necessarily started by a system service; on Guix System, +starting it is the responsibility of @code{gdm-service-type} and similar +services (@pxref{X Window}). At the level of Guix Home, as an +unprivileged user, we cannot start X Window; all we can do is check +whether it is running. This is what this service does. + +As a user, you probably don't need to worry or explicitly instantiate +@code{home-x11-service-type}. Services that require an X Window +graphical display, such as @code{home-redshift-service-type} below, +instantiate it and depend on its corresponding @code{x11-display} +Shepherd service (@pxref{Shepherd Home Service}). When X Window is +running, the @code{x11-display} Shepherd service starts and sets the +@code{DISPLAY} environment variable of the @command{shepherd} process; +otherwise, it fails to start. +@end defvar + @defvar home-redshift-service-type This is the service type for @uref{https://github.com/jonls/redshift, Redshift}, a program that adjusts the display color temperature diff --git a/gnu/home/services/desktop.scm b/gnu/home/services/desktop.scm index 626918fd9e..b293031fd1 100644 --- a/gnu/home/services/desktop.scm +++ b/gnu/home/services/desktop.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2022 Ludovic Courtès +;;; Copyright © 2022-2023 Ludovic Courtès ;;; Copyright © 2022 ( ;;; Copyright © 2023 conses ;;; Copyright © 2023 Janneke Nieuwenhuizen @@ -30,7 +30,9 @@ (define-module (gnu home services desktop) #:use-module (guix gexp) #:use-module (srfi srfi-1) #:use-module (ice-9 match) - #:export (home-redshift-configuration + #:export (home-x11-service-type + + home-redshift-configuration home-redshift-configuration? home-redshift-service-type @@ -43,6 +45,69 @@ (define-module (gnu home services desktop) home-xmodmap-configuration home-xmodmap-service-type)) + +;;; +;;; Waiting for X11. +;;; + +(define (x11-shepherd-service delay) + (list (shepherd-service + (provision '(x11-display)) + (modules '((ice-9 ftw) + (ice-9 match) + (srfi srfi-1))) + (start #~(lambda () + (define x11-directory + "/tmp/.X11-unix") + + ;; Wait for an accessible socket to show up in + ;; X11-DIRECTORY, up to DELAY seconds. + (let loop ((attempts #$delay)) + (define socket + (find (match-lambda + ((or "." "..") #f) + (name + (let ((name (in-vicinity x11-directory + name))) + (access? name O_RDWR)))) + (or (scandir x11-directory) '()))) + + (if (and socket (string-prefix? "X" socket)) + (let ((display (string-append + ":" (string-drop socket 1)))) + (format #t "X11 display server found at ~s.~%" + display) + (setenv "DISPLAY" display) + display) + (if (zero? attempts) + (begin + (display + "X11 display server did not show up; \ +giving up.\n" + (current-error-port)) + #f) + (begin + (sleep 1) + (loop (- attempts 1)))))))) + (stop #~(lambda (_) + (unsetenv "DISPLAY") + #f)) + (respawn? #f)))) + +(define home-x11-service-type + (service-type + (name 'home-x11-display) + (extensions (list (service-extension home-shepherd-service-type + x11-shepherd-service))) + (default-value 5) + (description + "Create a @code{x11-display} Shepherd service that waits for the X +Window (or ``X11'') graphical display server to be up and running, up to a +configurable delay, and sets the @code{DISPLAY} environment variable of +@command{shepherd} itself accordingly. If no accessible X11 server shows up +during that time, the @code{x11-display} service is marked as failing to +start."))) + ;;; ;;; Redshift. @@ -169,8 +234,11 @@ (define (redshift-shepherd-service config) (list (shepherd-service (documentation "Redshift program.") (provision '(redshift)) - ;; FIXME: This fails to start if Home is first activated from a - ;; non-X11 session. + + ;; Depend on 'x11-display', which sets 'DISPLAY' if an X11 server is + ;; available, and fails to start otherwise. + (requirement '(x11-display)) + (start #~(make-forkexec-constructor (list #$(file-append redshift "/bin/redshift") "-c" #$config-file))) @@ -181,7 +249,11 @@ (define home-redshift-service-type (service-type (name 'home-redshift) (extensions (list (service-extension home-shepherd-service-type - redshift-shepherd-service))) + redshift-shepherd-service) + ;; Ensure 'home-x11-service-type' is instantiated so we + ;; can depend on the Shepherd 'x11-display' service. + (service-extension home-x11-service-type + (const #t)))) (default-value (home-redshift-configuration)) (description "Run Redshift, a program that adjusts the color temperature of display