desktop.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu home services desktop)
  19. #:use-module (gnu home services)
  20. #:use-module (gnu home services shepherd)
  21. #:use-module (gnu services configuration)
  22. #:autoload (gnu packages xdisorg) (redshift)
  23. #:use-module (guix records)
  24. #:use-module (guix gexp)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (ice-9 match)
  27. #:export (home-redshift-configuration
  28. home-redshift-configuration?
  29. home-redshift-service-type))
  30. ;;;
  31. ;;; Redshift.
  32. ;;;
  33. (define (serialize-integer field value)
  34. (string-append (match field
  35. ('daytime-temperature "temp-day")
  36. ('nighttime-temperature "temp-night")
  37. ('daytime-brightness "brightness-day")
  38. ('nighttime-brightness "brightness-night")
  39. ('latitude "lat")
  40. ('longitude "lon")
  41. (_ (symbol->string field)))
  42. "=" (number->string value) "\n"))
  43. (define (serialize-symbol field value)
  44. (string-append (symbol->string field)
  45. "=" (symbol->string value) "\n"))
  46. (define (serialize-string field value)
  47. (string-append (symbol->string field)
  48. "=" value "\n"))
  49. (define serialize-inexact-number serialize-integer)
  50. (define (inexact-number? n)
  51. (and (number? n) (inexact? n)))
  52. (define-maybe inexact-number)
  53. (define-maybe string)
  54. (define (serialize-raw-configuration-string field value)
  55. value)
  56. (define raw-configuration-string? string?)
  57. (define-configuration home-redshift-configuration
  58. (redshift
  59. (file-like redshift)
  60. "Redshift package to use.")
  61. (location-provider
  62. (symbol 'geoclue2)
  63. "Geolocation provider---@code{'manual} or @code{'geoclue2}.
  64. In the former case, you must also specify the @code{latitude} and
  65. @code{longitude} fields so Redshift can determine daytime at your place. In
  66. the latter case, the Geoclue system service must be running; it will be
  67. queried for location information.")
  68. (adjustment-method
  69. (symbol 'randr)
  70. "Color adjustment method.")
  71. ;; Default values from redshift(1).
  72. (daytime-temperature
  73. (integer 6500)
  74. "Daytime color temperature (kelvins).")
  75. (nighttime-temperature
  76. (integer 4500)
  77. "Nighttime color temperature (kelvins).")
  78. (daytime-brightness
  79. maybe-inexact-number
  80. "Daytime screen brightness, between 0.1 and 1.0.")
  81. (nighttime-brightness
  82. maybe-inexact-number
  83. "Nighttime screen brightness, between 0.1 and 1.0.")
  84. (latitude
  85. maybe-inexact-number
  86. "Latitude, when @code{location-provider} is @code{'manual}.")
  87. (longitude
  88. maybe-inexact-number
  89. "Longitude, when @code{location-provider} is @code{'manual}.")
  90. (dawn-time
  91. maybe-string
  92. "Custom time for the transition from night to day in the
  93. morning---@code{\"HH:MM\"} format. When specified, solar elevation is not
  94. used to determine the daytime/nighttime period.")
  95. (dusk-time
  96. maybe-string
  97. "Likewise, custom time for the transition from day to night in the
  98. evening.")
  99. (extra-content
  100. (raw-configuration-string "")
  101. "Extra content appended as-is to the Redshift configuration file. Run
  102. @command{man redshift} for more information about the configuration file
  103. format."))
  104. (define (serialize-redshift-configuration config)
  105. (define location-fields
  106. '(latitude longitude))
  107. (define (location-field? field)
  108. (memq (configuration-field-name field) location-fields))
  109. (define (secondary-field? field)
  110. (or (location-field? field)
  111. (memq (configuration-field-name field)
  112. '(redshift extra-content))))
  113. #~(string-append
  114. "[redshift]\n"
  115. #$(serialize-configuration config
  116. (remove secondary-field?
  117. home-redshift-configuration-fields))
  118. #$(home-redshift-configuration-extra-content config)
  119. "\n[manual]\n"
  120. #$(serialize-configuration config
  121. (filter location-field?
  122. home-redshift-configuration-fields))))
  123. (define (redshift-shepherd-service config)
  124. (define config-file
  125. (computed-file "redshift.conf"
  126. #~(call-with-output-file #$output
  127. (lambda (port)
  128. (display #$(serialize-redshift-configuration config)
  129. port)))))
  130. (list (shepherd-service
  131. (documentation "Redshift program.")
  132. (provision '(redshift))
  133. ;; FIXME: This fails to start if Home is first activated from a
  134. ;; non-X11 session.
  135. (start #~(make-forkexec-constructor
  136. (list #$(file-append redshift "/bin/redshift")
  137. "-c" #$config-file)))
  138. (stop #~(make-kill-destructor)))))
  139. (define home-redshift-service-type
  140. (service-type
  141. (name 'home-redshift)
  142. (extensions (list (service-extension home-shepherd-service-type
  143. redshift-shepherd-service)))
  144. (default-value (home-redshift-configuration))
  145. (description
  146. "Run Redshift, a program that adjusts the color temperature of display
  147. according to time of day.")))