Subject: [bug#58170] [PATCH 1/1] gnu: home: Add home-batsignal-service-type.

From: "(" <[email-masked]>
Date: Thu, 29 Sep 2022 17:40:20 +0100

* gnu/home/services/pm.scm (home-batsignal-service-type): New
  variable.
(home-batsignal-configuration): New record type.
* doc/guix.texi: Document them.
* gnu/local.mk: Add gnu/home/services/pm.scm.
---
 doc/guix.texi            |  89 ++++++++++++++++++++++--
 gnu/home/services/pm.scm | 145 +++++++++++++++++++++++++++++++++++++++
 gnu/local.mk             |   2 +
 3 files changed, 230 insertions(+), 6 deletions(-)
 create mode 100644 gnu/home/services/pm.scm

diff --git a/doc/guix.texi b/doc/guix.texi
index c534574f81..081a7fb34f 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -107,6 +107,7 @@ Copyright @copyright{} 2022 Karl Hallsby@*
 Copyright @copyright{} 2022 Justin Veilleux@*
 Copyright @copyright{} 2022 Reily Siegel@*
 Copyright @copyright{} 2022 Simon Streit@*
+Copyright @copyright{} 2022 (@*
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -40119,12 +40120,13 @@ services)}.
 
 @menu
 * Essential Home Services::  Environment variables, packages, on-* scripts.
-* Shells: Shells Home Services.          POSIX shells, Bash, Zsh.
-* Mcron: Mcron Home Service.             Scheduled User's Job Execution.
-* Shepherd: Shepherd Home Service.       Managing User's Daemons.
-* SSH: Secure Shell.                     Setting up the secure shell client.
-* Desktop: Desktop Home Services.        Services for graphical environments.
-* Guix: Guix Home Services.              Services for Guix.
+* Shells: Shells Home Services.                        POSIX shells, Bash, Zsh.
+* Mcron: Mcron Home Service.                           Scheduled User's Job Execution.
+* Power Management: Power Management Home Services.    Services for battery power.
+* Shepherd: Shepherd Home Service.                     Managing User's Daemons.
+* SSH: Secure Shell.                                   Setting up the secure shell client.
+* Desktop: Desktop Home Services.                      Services for graphical environments.
+* Guix: Guix Home Services.                            Services for Guix.
 @end menu
 @c In addition to that Home Services can provide
 
@@ -40572,6 +40574,81 @@ specifications,, mcron, GNU@tie{}mcron}).
 @end table
 @end deftp
 
+@node Power Management Home Services
+
+@cindex power management
+The @code{(gnu home services pm)} module provides home services
+pertaining to battery power.
+
+@defvr {Scheme Variable} home-batsignal-service-type
+Service for @code{batsignal}, a program that monitors battery levels
+and warns the user through desktop notifications when their battery
+is getting low.  You can also configure a command to be run when the
+battery level passes a point deemed ``dangerous''.  This service is
+configured with the @code{home-batsignal-configuration} record.
+@end defvr
+
+@deftp {Data Type} home-batsignal-configuration
+Data type representing the configuration for batsignal.
+
+@table @asis
+@item @code{warning-level} (default: @code{15})
+The battery level to send a warning message at.
+
+@item @code{warning-message} (default: @code{#f})
+The message to send as a notification when the battery level reaches
+the @code{warning-level}.  Setting to @code{#f} uses the default
+message.
+
+@item @code{critical-level} (default: @code{5})
+The battery level to send a critical message at.
+
+@item @code{critical-message} (default: @code{#f})
+The message to send as a notification when the battery level reaches
+the @code{critical-level}.  Setting to @code{#f} uses the default
+message.
+
+@item @code{danger-level} (default: @code{2})
+The battery level to run the @code{danger-command} at.
+
+@item @code{danger-command} (default: @code{#f})
+The command to run when the battery level reaches the @code{danger-level}.
+Setting to @code{#f} disables running the command entirely.
+
+@item @code{full-level} (default: @code{#f})
+The battery level to send a full message at.  Setting to @code{#f}
+disables sending the full message entirely.
+
+@item @code{full-message} (default: @code{#f})
+The message to send as a notification when the battery level reaches
+the @code{full-level}.  Setting to @code{#f} uses the default message.
+
+@item @code{batteries} (default: @code{'()})
+The batteries to monitor.  Setting to @code{'()} tries to find batteries
+automatically.
+
+@item @code{poll-delay} (default: @code{60})
+The time in seconds to wait before checking the batteries again.
+
+@item @code{icon} (default: @code{#f})
+A file-like object to use as the icon for battery notifications.  Setting
+to @code{#f} disables notification icons entirely.
+
+@item @code{notifications?} (default: @code{#t})
+Whether to send any notifications.
+
+@item @code{notifications-expire?} (default: @code{#f})
+Whether notifications sent expire after a time.
+
+@item @code{notification-command} (default: @code{#f})
+Command to use to send messages.  Setting to @code{#f} sends a notification
+through @code{libnotify}.
+
+@item @code{ignore-missing?} (default: @code{#f})
+Whether to ignore missing battery errors.
+@end table
+@end deftp
+
 @node Shepherd Home Service
 @subsection Managing User Daemons
 
diff --git a/gnu/home/services/pm.scm b/gnu/home/services/pm.scm
new file mode 100644
index 0000000000..5f09941827
--- /dev/null
+++ b/gnu/home/services/pm.scm
@@ -0,0 +1,145 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2022 ( <[email-masked]>
+;;;
+;;; 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 <http://www.gnu.org/licenses/>.
+
+(define-module (gnu home services pm)
+  #:use-module (guix gexp)
+  #:use-module (guix packages)
+  #:use-module (guix records)
+  #:use-module (gnu home services)
+  #:use-module (gnu home services shepherd)
+  #:use-module (gnu packages monitoring)
+  #:use-module (gnu services shepherd)
+
+  #:export (home-batsignal-configuration
+            home-batsignal-service-type))
+
+;;;
+;;; batsignal
+;;;
+;;; Daemon for running commands and displaying notifications on
+;;; battery events.
+;;;
+
+(define-record-type* <home-batsignal-configuration>
+  home-batsignal-configuration make-home-batsignal-configuration
+  home-batsignal-configuration?
+  (warning-level batsignal-warning-level                    ;integer
+                 (default 15))
+  (warning-message batsignal-warning-message                ;string | #f
+                   (default #f))
+  (critical-level batsignal-critical-level                  ;integer
+                 (default 5))
+  (critical-message batsignal-critical-message              ;string | #f
+                    (default #f))
+  (danger-level batsignal-danger-level                      ;integer
+                (default 2))
+  (danger-command batsignal-danger-command                  ;file-like | string | #f
+                  (default #f))
+  (full-level batsignal-full-level                          ;integer | #f
+              (default #f))
+  (full-message batsignal-full-message                      ;string | #f
+                (default #f))
+  (batteries batsignal-batteries                            ;list of string
+             (default '()))
+  (poll-delay batsignal-poll-delay                          ;integer
+              (default 60))
+  (icon batsignal-icon                                      ;file-like | #f
+        (default #f))
+  (notifications? batsignal-notifications?                  ;boolean
+                  (default #t))
+  (notifications-expire? batsignal-notifications-expire?    ;boolean
+                         (default #f))
+  (notification-command batsignal-notification-command      ;string | #f
+                        (default #f))
+  (ignore-missing? batsignal-ignore-missing?                ;boolean
+                   (default #f)))
+
+(define (home-batsignal-shepherd-services config)
+  (let ((warning-level (batsignal-warning-level config))
+        (warning-message (batsignal-warning-message config))
+        (critical-level (batsignal-critical-level config))
+        (critical-message (batsignal-critical-message config))
+        (danger-level (batsignal-danger-level config))
+        (danger-command (batsignal-danger-command config))
+        (full-level (batsignal-full-level config))
+        (full-message (batsignal-full-message config))
+        (batteries (batsignal-batteries config))
+        (poll-delay (batsignal-poll-delay config))
+        (icon (batsignal-icon config))
+        (notifications? (batsignal-notifications? config))
+        (notifications-expire? (batsignal-notifications-expire? config))
+        (notification-command (batsignal-notification-command config))
+        (ignore-missing? (batsignal-ignore-missing? config)))
+    (list (shepherd-service
+           (provision '(batsignal))
+           (documentation "Run the batsignal battery-watching daemon.")
+           (start #~(make-forkexec-constructor
+                     (append (list #$(file-append batsignal "/bin/batsignal")
+                                   "-w" (number->string #$warning-level)
+                                   "-c" (number->string #$critical-level)
+                                   "-d" (number->string #$danger-level)
+                                   "-m" (number->string #$poll-delay))
+                             (if #$warning-message
+                                 (list "-W" #$warning-message)
+                                 (list))
+                             (if #$critical-message
+                                 (list "-C" #$critical-message)
+                                 (list))
+                             (if #$danger-command
+                                 (list "-D" #$danger-command)
+                                 (list))
+                             (if #$full-level
+                                 (list "-f" (number->string #$full-level))
+                                 (list))
+                             (if #$full-message
+                                 (list "-F" #$full-message)
+                                 (list))
+                             (if (null? (list #$@batteries))
+                                 (list)
+                                 (list "-n" (string-join (list #$@batteries) ",")))
+                             (if #$icon
+                                 (list "-I" #$icon)
+                                 (list))
+                             (if #$notifications?
+                                 (list)
+                                 (list "-N"))
+                             (if #$notifications-expire?
+                                 (list "-e")
+                                 (list))
+                             (if #$notification-command
+                                 (list "-M" #$notification-command)
+                                 (list))
+                             (if #$ignore-missing?
+                                 (list "-i")
+                                 (list)))
+                     #:log-file (string-append
+                                 (or (getenv "XDG_LOG_HOME")
+                                     (format #f "~a/.local/var/log"
+                                               (getenv "HOME")))
+                                 "/batsignal.log")))
+           (stop #~(make-kill-destructor))))))
+
+(define home-batsignal-service-type
+  (service-type
+   (name 'home-batsignal)
+   (extensions
+    (list (service-extension home-shepherd-service-type
+                             home-batsignal-shepherd-services)))
+   (default-value (home-batsignal-configuration))
+   (description
+    "Run batsignal, a battery watching and notification daemon.")))
diff --git a/gnu/local.mk b/gnu/local.mk
index 4e982dc6e3..9ef10fcbfe 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -54,6 +54,7 @@
 # Copyright © 2022 muradm <[email-masked]>
 # Copyright © 2022 Hilton Chain <[email-masked]>
 # Copyright © 2022 Alex Griffin <[email-masked]>
+# Copyright © 2022 ( <[email-masked]>
 #
 # This file is part of GNU Guix.
 #
@@ -89,6 +90,7 @@ GNU_SYSTEM_MODULES =				\
   %D%/home/services/symlink-manager.scm		\
   %D%/home/services/fontutils.scm		\
   %D%/home/services/guix.scm			\
+  %D%/home/services/pm.scm			\
   %D%/home/services/shells.scm			\
   %D%/home/services/shepherd.scm		\
   %D%/home/services/ssh.scm			\
-- 
2.37.3