Add usbrelayd service
modules/cogd/services/hardware.scm unknown status 1
| 1 | + | ;;; SPDX-FileCopyrightText: 2025 Evgeny Pisemsky <mail@pisemsky.site> | |
| 2 | + | ;;; | |
| 3 | + | ;;; SPDX-License-Identifier: GPL-3.0-or-later | |
| 4 | + | ||
| 5 | + | (define-module (cogd services hardware)) | |
| 6 | + | ||
| 7 | + | (use-modules (guix records) | |
| 8 | + | (guix gexp) | |
| 9 | + | (guix packages) | |
| 10 | + | (gnu packages admin) | |
| 11 | + | (gnu system shadow) | |
| 12 | + | (gnu services) | |
| 13 | + | (gnu services base) | |
| 14 | + | (gnu services shepherd) | |
| 15 | + | (cogd packages hardware)) | |
| 16 | + | ||
| 17 | + | (export usbrelayd-configuration | |
| 18 | + | usbrelayd-configuration? | |
| 19 | + | usbrelayd-configuration-package | |
| 20 | + | usbrelayd-configuration-mqtt-broker | |
| 21 | + | usbrelayd-configuration-mqtt-client | |
| 22 | + | usbrelayd-service-type) | |
| 23 | + | ||
| 24 | + | (define-record-type* <usbrelayd-configuration> | |
| 25 | + | usbrelayd-configuration | |
| 26 | + | make-usbrelayd-configuration | |
| 27 | + | usbrelayd-configuration? | |
| 28 | + | (package usbrelayd-configuration-package | |
| 29 | + | (default python-usbrelay)) | |
| 30 | + | (mqtt-broker usbrelayd-configuration-mqtt-broker | |
| 31 | + | (default "localhost")) | |
| 32 | + | (mqtt-client usbrelayd-configuration-mqtt-client | |
| 33 | + | (default "usbrelayd"))) | |
| 34 | + | ||
| 35 | + | (define (usbrelayd-config-file config) | |
| 36 | + | (plain-file | |
| 37 | + | "usbrelayd.conf" | |
| 38 | + | (format #f "[MQTT]\nBROKER = ~a\nCLIENTNAME = ~a\n" | |
| 39 | + | (usbrelayd-configuration-mqtt-broker config) | |
| 40 | + | (usbrelayd-configuration-mqtt-client config)))) | |
| 41 | + | ||
| 42 | + | (define (usbrelayd-etc-service config) | |
| 43 | + | `(("usbrelayd.conf" ,(usbrelayd-config-file config)))) | |
| 44 | + | ||
| 45 | + | (define usbrelayd-accounts | |
| 46 | + | (const | |
| 47 | + | (list (user-group | |
| 48 | + | (name "usbrelay") | |
| 49 | + | (system? #t)) | |
| 50 | + | (user-account | |
| 51 | + | (name "usbrelay") | |
| 52 | + | (group "usbrelay") | |
| 53 | + | (system? #t) | |
| 54 | + | (home-directory "/var/empty") | |
| 55 | + | (shell (file-append shadow "/sbin/nologin")))))) | |
| 56 | + | ||
| 57 | + | (define (usbrelayd-shepherd-service config) | |
| 58 | + | (list (shepherd-service | |
| 59 | + | (documentation "Run the USB relay daemon.") | |
| 60 | + | (provision '(usbrelayd)) | |
| 61 | + | (requirement '(mosquitto)) | |
| 62 | + | (start #~(make-forkexec-constructor | |
| 63 | + | (list #$(file-append | |
| 64 | + | (usbrelayd-configuration-package config) | |
| 65 | + | "/sbin/usbrelayd")) | |
| 66 | + | #:user "usbrelay" | |
| 67 | + | #:group "usbrelay")) | |
| 68 | + | (stop #~(make-kill-destructor))))) | |
| 69 | + | ||
| 70 | + | (define (usbrelayd-udev-rules config) | |
| 71 | + | (list (lookup-package-input | |
| 72 | + | (usbrelayd-configuration-package config) | |
| 73 | + | "usbrelay"))) | |
| 74 | + | ||
| 75 | + | (define usbrelayd-service-type | |
| 76 | + | (service-type | |
| 77 | + | (description "Run the USB relay daemon.") | |
| 78 | + | (name 'usbrelayd) | |
| 79 | + | (extensions | |
| 80 | + | (list (service-extension account-service-type usbrelayd-accounts) | |
| 81 | + | (service-extension etc-service-type usbrelayd-etc-service) | |
| 82 | + | (service-extension shepherd-root-service-type usbrelayd-shepherd-service) | |
| 83 | + | (service-extension udev-service-type usbrelayd-udev-rules))) | |
| 84 | + | (default-value (usbrelayd-configuration)))) |