1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-08 19:45:42 +03:00

nixos/home-assistant: reload the daemon when configuration changed

Reload the service when configuration changes. This means that we don't
have a potentially slow startup for every small configuration change.
This commit is contained in:
Andreas Rammhold 2021-12-18 23:01:33 +01:00 committed by Martin Weinelt
parent e1e08fe28b
commit cfbcf381c2
No known key found for this signature in database
GPG key ID: 87C1E9888F856759
2 changed files with 48 additions and 3 deletions

View file

@ -369,6 +369,17 @@ in {
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.config.http.server_port ]; networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.config.http.server_port ];
# symlink the configuration to /etc/home-assistant
environment.etc = lib.mkMerge [
(lib.mkIf (cfg.config != null && !cfg.configWritable) {
"home-assistant/configuration.yaml".source = configFile;
})
(lib.mkIf (cfg.lovelaceConfig != null && !cfg.lovelaceConfigWritable) {
"home-assistant/ui-lovelace.yaml".source = lovelaceConfigFile;
})
];
systemd.services.home-assistant = { systemd.services.home-assistant = {
description = "Home Assistant"; description = "Home Assistant";
after = [ after = [
@ -378,18 +389,22 @@ in {
"mysql.service" "mysql.service"
"postgresql.service" "postgresql.service"
]; ];
reloadTriggers = [
configFile
lovelaceConfigFile
];
preStart = let preStart = let
copyConfig = if cfg.configWritable then '' copyConfig = if cfg.configWritable then ''
cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml"
'' else '' '' else ''
rm -f "${cfg.configDir}/configuration.yaml" rm -f "${cfg.configDir}/configuration.yaml"
ln -s ${configFile} "${cfg.configDir}/configuration.yaml" ln -s /etc/home-assistant/configuration.yaml "${cfg.configDir}/configuration.yaml"
''; '';
copyLovelaceConfig = if cfg.lovelaceConfigWritable then '' copyLovelaceConfig = if cfg.lovelaceConfigWritable then ''
cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
'' else '' '' else ''
rm -f "${cfg.configDir}/ui-lovelace.yaml" rm -f "${cfg.configDir}/ui-lovelace.yaml"
ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" ln -s /etc/home-assistant/ui-lovelace.yaml "${cfg.configDir}/ui-lovelace.yaml"
''; '';
in in
(optionalString (cfg.config != null) copyConfig) + (optionalString (cfg.config != null) copyConfig) +

View file

@ -98,9 +98,26 @@ in {
}; };
lovelaceConfigWritable = true; lovelaceConfigWritable = true;
}; };
# Cause a configuration change inside `configuration.yml` and verify that the process is being reloaded.
specialisation.differentName = {
inheritParentConfig = true;
configuration.services.home-assistant.config.homeassistant.name = lib.mkForce "Test Home";
};
# Cause a configuration change that requires a service restart as we added a new runtime dependency
specialisation.newFeature = {
inheritParentConfig = true;
configuration.services.home-assistant.config.device_tracker = [
{ platform = "bluetooth_tracker"; }
];
};
}; };
testScript = '' testScript = { nodes, ... }: let
system = nodes.hass.config.system.build.toplevel;
in
''
import re import re
start_all() start_all()
@ -142,6 +159,19 @@ in {
with subtest("Check extra components are considered in systemd unit hardening"): with subtest("Check extra components are considered in systemd unit hardening"):
hass.succeed("systemctl show -p DeviceAllow home-assistant.service | grep -q char-ttyUSB") hass.succeed("systemctl show -p DeviceAllow home-assistant.service | grep -q char-ttyUSB")
with subtest("Check service reloads when configuration changes"):
# store the old pid of the process
pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
hass.succeed("${system}/specialisation/differentName/bin/switch-to-configuration test")
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
assert pid == new_pid, "The PID of the process should not change between process reloads"
with subtest("check service restarts when package changes"):
pid = new_pid
hass.succeed("${system}/specialisation/newFeature/bin/switch-to-configuration test")
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
assert pid != new_pid, "The PID of the process shoudl change when the HA binary changes"
with subtest("Print log to ease debugging"): with subtest("Print log to ease debugging"):
output_log = hass.succeed("cat ${configDir}/home-assistant.log") output_log = hass.succeed("cat ${configDir}/home-assistant.log")
print("\n### home-assistant.log ###\n") print("\n### home-assistant.log ###\n")