aboutsummaryrefslogtreecommitdiff
path: root/gnu/home/services/pm.scm
blob: 5f09941827cf628d52a35c9ab4956c788d1b51c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 ( <paren@disroot.org>
;;;
;;; 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.")))