Add mosquitto service
modules/cogd/services/messaging.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 messaging)) | |
| 6 | + | ||
| 7 | + | (use-modules (guix records) | |
| 8 | + | (guix gexp) | |
| 9 | + | (gnu packages messaging) | |
| 10 | + | (gnu packages admin) | |
| 11 | + | (gnu system shadow) | |
| 12 | + | (gnu services) | |
| 13 | + | (gnu services shepherd)) | |
| 14 | + | ||
| 15 | + | (export mosquitto-configuration | |
| 16 | + | mosquitto-configuration? | |
| 17 | + | mosquitto-configuration-package | |
| 18 | + | mosquitto-configuration-config-file | |
| 19 | + | mosquitto-configuration-user | |
| 20 | + | mosquitto-configuration-group | |
| 21 | + | mosquitto-service-type) | |
| 22 | + | ||
| 23 | + | (define-record-type* <mosquitto-configuration> | |
| 24 | + | mosquitto-configuration | |
| 25 | + | make-mosquitto-configuration | |
| 26 | + | mosquitto-configuration? | |
| 27 | + | (package mosquitto-configuration-package | |
| 28 | + | (default mosquitto)) | |
| 29 | + | (config-file mosquitto-configuration-config-file | |
| 30 | + | (default #f)) | |
| 31 | + | (user mosquitto-configuration-user | |
| 32 | + | (default "mosquitto")) | |
| 33 | + | (group mosquitto-configuration-group | |
| 34 | + | (default "mosquitto"))) | |
| 35 | + | ||
| 36 | + | (define (mosquitto-accounts config) | |
| 37 | + | (match-record config <mosquitto-configuration> | |
| 38 | + | (user group) | |
| 39 | + | (filter identity | |
| 40 | + | (list | |
| 41 | + | (and (equal? group "mosquitto") | |
| 42 | + | (user-group | |
| 43 | + | (name "mosquitto") | |
| 44 | + | (system? #t))) | |
| 45 | + | (and (equal? user "mosquitto") | |
| 46 | + | (user-account | |
| 47 | + | (name "mosquitto") | |
| 48 | + | (group group) | |
| 49 | + | (system? #t) | |
| 50 | + | (comment "bzzz") | |
| 51 | + | (home-directory "/var/empty") | |
| 52 | + | (shell (file-append shadow "/sbin/nologin")))))))) | |
| 53 | + | ||
| 54 | + | (define (mosquitto-shepherd-service config) | |
| 55 | + | (match-record config <mosquitto-configuration> | |
| 56 | + | (package config-file user group) | |
| 57 | + | (list (shepherd-service | |
| 58 | + | (documentation "Run the mosquitto MQTT broker.") | |
| 59 | + | (provision '(mosquitto)) | |
| 60 | + | (requirement '(networking syslogd user-processes)) | |
| 61 | + | (start #~(make-forkexec-constructor | |
| 62 | + | (list #$(file-append package "/sbin/mosquitto") | |
| 63 | + | #$@(if config-file | |
| 64 | + | (list "-c" config-file) | |
| 65 | + | '())) | |
| 66 | + | #:user #$user | |
| 67 | + | #:group #$group)) | |
| 68 | + | (stop #~(make-kill-destructor)))))) | |
| 69 | + | ||
| 70 | + | (define mosquitto-service-type | |
| 71 | + | (service-type | |
| 72 | + | (description "Run the mosquitto MQTT broker.") | |
| 73 | + | (name 'mosquitto) | |
| 74 | + | (extensions | |
| 75 | + | (list (service-extension account-service-type mosquitto-accounts) | |
| 76 | + | (service-extension shepherd-root-service-type mosquitto-shepherd-service))) | |
| 77 | + | (default-value (mosquitto-configuration)))) |