homeassistant-service.scm
1 | ;;; SPDX-FileCopyrightText: 2024, 2025 Evgeny Pisemsky <mail@pisemsky.site> |
2 | ;;; |
3 | ;;; SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | (define-module (homeassistant-service)) |
6 | |
7 | (use-modules (guix gexp) |
8 | (guix records) |
9 | (gnu packages admin) |
10 | (gnu system shadow) |
11 | (gnu services) |
12 | (gnu services shepherd) |
13 | (homeassistant-package)) |
14 | |
15 | (export homeassistant-configuration |
16 | homeassistant-configuration? |
17 | homeassistant-configuration-package |
18 | homeassistant-configuration-yaml-file |
19 | homeassistant-service-type) |
20 | |
21 | (define homeassistant-config-dir "/var/lib/homeassistant") |
22 | |
23 | (define-record-type* <homeassistant-configuration> |
24 | homeassistant-configuration |
25 | make-homeassistant-configuration |
26 | homeassistant-configuration? |
27 | (package homeassistant-configuration-package |
28 | (default homeassistant)) |
29 | (yaml-file homeassistant-configuration-yaml-file |
30 | (default (plain-file "configuration.yaml" "default_config:\n")))) |
31 | |
32 | (define homeassistant-accounts |
33 | (const |
34 | (list (user-group |
35 | (name "homeassistant") |
36 | (system? #t)) |
37 | (user-account |
38 | (name "homeassistant") |
39 | (group "homeassistant") |
40 | (system? #t) |
41 | (home-directory homeassistant-config-dir) |
42 | (shell (file-append shadow "/sbin/nologin")))))) |
43 | |
44 | (define (homeassistant-activation config) |
45 | (let ((source (homeassistant-configuration-yaml-file config)) |
46 | (target (string-append homeassistant-config-dir "/configuration.yaml"))) |
47 | #~(begin |
48 | (if (file-exists? #$target) |
49 | (delete-file #$target)) |
50 | (symlink #$source #$target)))) |
51 | |
52 | (define (homeassistant-shepherd-service config) |
53 | (let ((package (homeassistant-configuration-package config)) |
54 | (yaml-file (homeassistant-configuration-yaml-file config))) |
55 | (list (shepherd-service |
56 | (documentation "Run the Home Assistant platform.") |
57 | (provision '(homeassistant)) |
58 | (requirement '(user-processes networking)) |
59 | (start #~(make-forkexec-constructor |
60 | (list #$(file-append package "/bin/hass") |
61 | "--config" #$homeassistant-config-dir |
62 | "--skip-pip") |
63 | #:user "homeassistant" |
64 | #:group "homeassistant")) |
65 | (stop #~(make-kill-destructor)))))) |
66 | |
67 | (define homeassistant-service-type |
68 | (service-type |
69 | (description "Run the Home Assistant platform.") |
70 | (name 'homeassistant) |
71 | (extensions |
72 | (list (service-extension account-service-type homeassistant-accounts) |
73 | (service-extension activation-service-type homeassistant-activation) |
74 | (service-extension shepherd-root-service-type homeassistant-shepherd-service))) |
75 | (default-value (homeassistant-configuration)))) |
76 |