;;; SPDX-FileCopyrightText: 2024, 2025 Evgeny Pisemsky ;;; ;;; SPDX-License-Identifier: GPL-3.0-or-later (define-module (homeassistant-service)) (use-modules (guix gexp) (guix records) (gnu packages admin) (gnu system shadow) (gnu services) (gnu services shepherd) (homeassistant-package)) (export homeassistant-configuration homeassistant-configuration? homeassistant-configuration-package homeassistant-configuration-yaml-file homeassistant-service-type) (define homeassistant-config-dir "/var/lib/homeassistant") (define-record-type* homeassistant-configuration make-homeassistant-configuration homeassistant-configuration? (package homeassistant-configuration-package (default homeassistant)) (yaml-file homeassistant-configuration-yaml-file (default (plain-file "configuration.yaml" "default_config:\n")))) (define homeassistant-accounts (const (list (user-group (name "homeassistant") (system? #t)) (user-account (name "homeassistant") (group "homeassistant") (system? #t) (home-directory homeassistant-config-dir) (shell (file-append shadow "/sbin/nologin")))))) (define (homeassistant-activation config) (let ((source (homeassistant-configuration-yaml-file config)) (target (string-append homeassistant-config-dir "/configuration.yaml"))) #~(begin (if (file-exists? #$target) (delete-file #$target)) (symlink #$source #$target)))) (define (homeassistant-shepherd-service config) (let ((package (homeassistant-configuration-package config)) (yaml-file (homeassistant-configuration-yaml-file config))) (list (shepherd-service (documentation "Run the Home Assistant platform.") (provision '(homeassistant)) (requirement '(user-processes networking)) (start #~(make-forkexec-constructor (list #$(file-append package "/bin/hass") "--config" #$homeassistant-config-dir "--skip-pip") #:user "homeassistant" #:group "homeassistant")) (stop #~(make-kill-destructor)))))) (define homeassistant-service-type (service-type (description "Run the Home Assistant platform.") (name 'homeassistant) (extensions (list (service-extension account-service-type homeassistant-accounts) (service-extension activation-service-type homeassistant-activation) (service-extension shepherd-root-service-type homeassistant-shepherd-service))) (default-value (homeassistant-configuration))))