guix_mirror_bot pushed a commit to branch master
in repository guix.
commit 0c4386825ae4decd2329073f6e224ac326c79838
Author: Amy Pillow <[email protected]>
AuthorDate: Mon Mar 30 18:06:31 2026 +0200
gnu: Add system76-power-service-type.
* gnu/services/desktop.scm (system76-service-type): New variable.
* doc/guix.texi (Desktop Services): Define system76-power service and
configuration.
The System76 Power service runs a daemon to manage graphics and power
options
as well as battery charge thresholds.
Change-Id: Icd7a45d2c14afb10eace78f51b5b9bbcf25d1127
Signed-off-by: Ludovic Courtès <[email protected]>
Merges: #9914
---
doc/guix.texi | 57 +++++++++++++++++++++
gnu/services/desktop.scm | 125 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 182 insertions(+)
diff --git a/doc/guix.texi b/doc/guix.texi
index cf7516d7f54..739641fc139 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -28210,6 +28210,63 @@ for their own passwords when performing administrative
actions instead of
@code{root}'s, similar to the behaviour used by @code{sudo}.
@end defvar
+@defvar system76-power-service-type
+Service that runs the @code{system76-power} daemon to manage graphics
+and power options as well as battery charge thresholds. Its value is a
+@code{<system76-power-configuration>} object.
+@end defvar
+
+The following example sets the battery charge thresholds to 70% and 90%,
+and can be added to the list of services in your
+@code{operating-system}.
+
+@lisp
+(service system76-power-service-type
+ (system76-power-configuration
+ (charge-thresholds '(70 . 90))))
+@end lisp
+
+@deftp {Data Type} system76-power-configuration
+Available @code{system76-power-configuration} fields are:
+
+@table @asis
+@item @code{package} (default: @code{system76-power}) (type: package)
+The package to use for @code{system76-power}.
+
+@item @code{profile-on-ac} (default: @code{performance}) (type:
system76-power-profile)
+The power profile to use when plugged in. Possible values are
+@code{battery}, @code{balanced}, or @code{performance}.
+
+@item @code{profile-on-battery} (default: @code{battery}) (type:
system76-power-profile)
+The power profile to use on battery. Possible values are
+@code{battery}, @code{balanced}, or @code{performance}.
+
+@item @code{charge-thresholds} (default: @code{balanced}) (type:
system76-power-charge-thresholds)
+Set thresholds for battery charging. This is either a profile name
+(@code{full-charge}, @code{balanced},@code{max-lifespan}) or a pair of
+start and stop percentages for when to charge.
+
+Possible profiles are:
+
+@table @asis
+@item @code{full-charge} (start: 90) (end: 100)
+Battery is charged to its full capacity for the longest possible use on
+battery power. Charging resumes when the battery falls below 90% charge.
+
+@item @code{balanced} (start: 86) (end: 90)
+Use this threshold when you unplug frequently but don't need the full
+battery capacity. Charging stops when the battery reaches 90% capacity
+and resumes when the battery falls below 86%.
+
+@item @code{max-lifespan} (start: 50) (end: 60)
+Use this threshold if you rarely use the system on battery for extended
+periods. Charging stops when the battery reaches 60% capacity and
+resumes when the battery falls below 50%.
+
+@end table
+@end table
+@end deftp
+
@defvar upower-service-type
Service that runs @uref{https://upower.freedesktop.org/, @command{upowerd}}, a
system-wide monitor for power consumption and battery levels, with the given
diff --git a/gnu/services/desktop.scm b/gnu/services/desktop.scm
index fecb16e2ad2..d0b5e1dc345 100644
--- a/gnu/services/desktop.scm
+++ b/gnu/services/desktop.scm
@@ -78,6 +78,7 @@
#:use-module (gnu packages pulseaudio)
#:use-module (gnu packages xfce)
#:use-module (gnu packages xdisorg)
+ #:use-module (gnu packages rust-apps)
#:use-module (gnu packages scanner)
#:use-module (gnu packages suckless)
#:use-module (gnu packages sugar)
@@ -122,6 +123,14 @@
upower-service-type
+ system76-power-configuration
+ system76-power-configuration?
+ system76-power-configuration-package
+ system76-power-configuration-profile-on-ac
+ system76-power-configuration-profile-on-battery
+ system76-power-configuration-charge-thresholds
+ system76-power-service-type
+
udisks-configuration
udisks-configuration?
udisks-service ; deprecated
@@ -2653,6 +2662,122 @@ hardware files."
plasma-package))))))
+;;;
+;;; System76 power service.
+;;;
+
+(define (system76-power-profile? profile)
+ (and (memq profile '(battery balanced performance))
+ #t))
+
+(define (system76-power-charge-thresholds? profile)
+ (match profile
+ ((or 'full-charge 'balanced 'max-lifespan) #t)
+ (((? number?) . (? number?)) #t)
+ (_ #f)))
+
+(define-configuration/no-serialization system76-power-configuration
+ (package
+ (package system76-power)
+ "The package to use for @code{system76-power}.")
+ (profile-on-ac
+ (system76-power-profile 'performance)
+ "The power profile to use when plugged in.
+
+Possible values are @code{battery}, @code{balanced}, or @code{performance}.")
+ (profile-on-battery
+ (system76-power-profile 'battery)
+ "The power profile to use on battery.
+
+Possible values are @code{battery}, @code{balanced}, or @code{performance}.")
+ (charge-thresholds
+ (system76-power-charge-thresholds 'balanced)
+ "Set thresholds for battery charging. This is either a profile
+name (@code{full-charge}, @code{balanced},@code{max-lifespan}) or a pair of
+start and stop percentages."))
+
+(define system76-power-supply-changed
+ (match-record-lambda <system76-power-configuration>
+ (package profile-on-ac profile-on-battery)
+ (program-file
+ "system76-power-supply-changed"
+ (with-imported-modules '((guix build utils))
+ #~(begin
+ (use-modules (ice-9 popen)
+ (ice-9 textual-ports)
+ (guix build utils))
+ (let ((on-line? (string-contains
+ (get-string-all
+ (open-pipe*
+ OPEN_READ
+ #$(file-append acpi "/bin/acpi") "-a"))
+ "on-line")))
+ (invoke #$(file-append package "/bin/system76-power")
+ "profile"
+ (symbol->string
+ (if on-line?
+ '#$profile-on-ac
+ '#$profile-on-battery)))))))))
+
+(define (system76-power-udev-rules config)
+ (list
+ (file->udev-rule
+ "99-system76-power.rules"
+ (computed-file
+ "99-system76-power.rules"
+ #~(with-output-to-file #$output
+ (lambda _
+ (format #t "\
+ACTION==\"change\", SUBSYSTEM==\"power_supply\",\
+KERNEL!=\"hidpp_battery*\", RUN+=~s"
+ #$(system76-power-supply-changed config))))))))
+
+(define (system76-power-shepherd-services config)
+ (match-record config <system76-power-configuration>
+ (package charge-thresholds)
+ (list
+ (shepherd-service
+ (provision '(system76-power))
+ (requirement '(dbus-system))
+ (documentation "System76 Power Daemon")
+ (start #~(make-forkexec-constructor
+ (list #$(file-append package "/bin/system76-power")
+ "daemon")))
+ (stop #~(make-kill-destructor)))
+ (shepherd-service
+ (provision '(system76-power-defaults))
+ (requirement '(system76-power))
+ (documentation "Load System76 Power defaults")
+ (start #~(lambda _
+ (invoke #$(system76-power-supply-changed config))
+ (invoke #$(file-append package "/bin/system76-power")
+ "charge-thresholds"
+ #$@(match charge-thresholds
+ ((start . end)
+ (list (number->string start) (number->string
end)))
+ ('full-charge '("--profile" "full_charge"))
+ ('balanced '("--profile" "balanced"))
+ ('max-lifespan '("--profile"
"max_lifespan"))))))
+ (one-shot? #t)))))
+
+(define system76-power-service-type
+ (let ((pkgs (compose list system76-power-configuration-package)))
+ (service-type
+ (name 'system76-power)
+ (description "Run the @code{system76-power} daemon to manage graphics
+and power options as well as battery charge thresholds.")
+ (extensions
+ (list
+ (service-extension dbus-root-service-type pkgs)
+ (service-extension polkit-service-type pkgs)
+ (service-extension profile-service-type pkgs)
+ (service-extension shepherd-root-service-type
+ system76-power-shepherd-services)
+ (service-extension udev-service-type
+ system76-power-udev-rules)))
+ (default-value (system76-power-configuration)))))
+
+
;;;
;;; inputattach-service-type
;;;