2018-01-23 10:51:13 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.home-assistant;
|
|
|
|
|
2018-04-24 00:12:19 +02:00
|
|
|
# cfg.config != null can be assumed here
|
2019-01-24 16:49:06 +01:00
|
|
|
configJSON = pkgs.writeText "configuration.json"
|
2018-04-24 00:12:19 +02:00
|
|
|
(builtins.toJSON (if cfg.applyDefaultConfig then
|
2019-01-24 15:52:05 +01:00
|
|
|
(recursiveUpdate defaultConfig cfg.config) else cfg.config));
|
2019-02-27 22:43:39 +01:00
|
|
|
configFile = pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } ''
|
2019-01-24 16:49:06 +01:00
|
|
|
${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out
|
2020-06-02 01:48:35 +02:00
|
|
|
# Hack to support custom yaml objects,
|
|
|
|
# i.e. secrets: https://www.home-assistant.io/docs/configuration/secrets/
|
|
|
|
sed -i -e "s/'\!\([a-z_]\+\) \(.*\)'/\!\1 \2/;s/^\!\!/\!/;" $out
|
2019-01-24 16:49:06 +01:00
|
|
|
'';
|
2019-01-24 15:52:05 +01:00
|
|
|
|
2019-01-24 16:49:06 +01:00
|
|
|
lovelaceConfigJSON = pkgs.writeText "ui-lovelace.json"
|
2019-01-24 15:52:05 +01:00
|
|
|
(builtins.toJSON cfg.lovelaceConfig);
|
2019-02-27 22:43:39 +01:00
|
|
|
lovelaceConfigFile = pkgs.runCommand "ui-lovelace.yaml" { preferLocalBuild = true; } ''
|
2019-01-24 16:49:06 +01:00
|
|
|
${pkgs.remarshal}/bin/json2yaml -i ${lovelaceConfigJSON} -o $out
|
|
|
|
'';
|
2018-02-01 13:42:07 +01:00
|
|
|
|
2019-02-22 17:28:59 +01:00
|
|
|
availableComponents = cfg.package.availableComponents;
|
2018-02-01 13:42:07 +01:00
|
|
|
|
2019-04-25 15:54:24 +02:00
|
|
|
usedPlatforms = config:
|
|
|
|
if isAttrs config then
|
|
|
|
optional (config ? platform) config.platform
|
|
|
|
++ concatMap usedPlatforms (attrValues config)
|
|
|
|
else if isList config then
|
|
|
|
concatMap usedPlatforms config
|
|
|
|
else [ ];
|
|
|
|
|
|
|
|
# Given a component "platform", looks up whether it is used in the config
|
|
|
|
# as `platform = "platform";`.
|
2018-02-04 01:30:47 +01:00
|
|
|
#
|
2019-04-25 15:54:24 +02:00
|
|
|
# For example, the component mqtt.sensor is used as follows:
|
2018-02-04 01:30:47 +01:00
|
|
|
# config.sensor = [ {
|
2019-04-25 15:54:24 +02:00
|
|
|
# platform = "mqtt";
|
2018-02-04 01:30:47 +01:00
|
|
|
# ...
|
|
|
|
# } ];
|
2019-04-25 15:54:24 +02:00
|
|
|
useComponentPlatform = component: elem component (usedPlatforms cfg.config);
|
2018-02-04 01:30:47 +01:00
|
|
|
|
2018-02-01 13:42:07 +01:00
|
|
|
# Returns whether component is used in config
|
2018-02-04 01:30:47 +01:00
|
|
|
useComponent = component:
|
|
|
|
hasAttrByPath (splitString "." component) cfg.config
|
|
|
|
|| useComponentPlatform component;
|
2018-02-01 13:42:07 +01:00
|
|
|
|
|
|
|
# List of components used in config
|
|
|
|
extraComponents = filter useComponent availableComponents;
|
|
|
|
|
2018-07-20 19:28:29 +02:00
|
|
|
package = if (cfg.autoExtraComponents && cfg.config != null)
|
2018-02-01 13:42:07 +01:00
|
|
|
then (cfg.package.override { inherit extraComponents; })
|
|
|
|
else cfg.package;
|
|
|
|
|
2018-04-21 16:32:09 +02:00
|
|
|
# If you are changing this, please update the description in applyDefaultConfig
|
|
|
|
defaultConfig = {
|
|
|
|
homeassistant.time_zone = config.time.timeZone;
|
2019-02-09 15:24:54 +01:00
|
|
|
http.server_port = cfg.port;
|
2019-01-24 15:52:05 +01:00
|
|
|
} // optionalAttrs (cfg.lovelaceConfig != null) {
|
|
|
|
lovelace.mode = "yaml";
|
2018-04-21 16:32:09 +02:00
|
|
|
};
|
|
|
|
|
2018-01-23 10:51:13 +01:00
|
|
|
in {
|
|
|
|
meta.maintainers = with maintainers; [ dotlambda ];
|
|
|
|
|
|
|
|
options.services.home-assistant = {
|
|
|
|
enable = mkEnableOption "Home Assistant";
|
|
|
|
|
|
|
|
configDir = mkOption {
|
|
|
|
default = "/var/lib/hass";
|
|
|
|
type = types.path;
|
|
|
|
description = "The config directory, where your <filename>configuration.yaml</filename> is located.";
|
|
|
|
};
|
|
|
|
|
2018-04-21 16:32:09 +02:00
|
|
|
port = mkOption {
|
|
|
|
default = 8123;
|
|
|
|
type = types.int;
|
|
|
|
description = "The port on which to listen.";
|
|
|
|
};
|
|
|
|
|
|
|
|
applyDefaultConfig = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Setting this option enables a few configuration options for HA based on NixOS configuration (such as time zone) to avoid having to manually specify configuration we already have.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Currently one side effect of enabling this is that the <literal>http</literal> component will be enabled.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This only takes effect if <literal>config != null</literal> in order to ensure that a manually managed <filename>configuration.yaml</filename> is not overwritten.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-01-23 10:51:13 +01:00
|
|
|
config = mkOption {
|
|
|
|
default = null;
|
2020-01-29 00:09:52 +01:00
|
|
|
# Migrate to new option types later: https://github.com/NixOS/nixpkgs/pull/75584
|
|
|
|
type = with lib.types; let
|
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
|
|
|
(lazyAttrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
]) // {
|
|
|
|
description = "Yaml value";
|
|
|
|
emptyValue.value = {};
|
|
|
|
};
|
|
|
|
in valueType;
|
2018-01-23 10:51:13 +01:00
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
homeassistant = {
|
|
|
|
name = "Home";
|
2019-12-20 09:40:16 +01:00
|
|
|
latitude = "!secret latitude";
|
|
|
|
longitude = "!secret longitude";
|
|
|
|
elevation = "!secret elevation";
|
|
|
|
unit_system = "metric";
|
2018-01-23 10:51:13 +01:00
|
|
|
time_zone = "UTC";
|
|
|
|
};
|
2020-06-02 01:48:35 +02:00
|
|
|
frontend = {
|
|
|
|
themes = "!include_dir_merge_named themes";
|
|
|
|
};
|
2018-01-23 10:51:13 +01:00
|
|
|
http = { };
|
2018-02-01 13:42:07 +01:00
|
|
|
feedreader.urls = [ "https://nixos.org/blogs.xml" ];
|
2018-01-23 10:51:13 +01:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Your <filename>configuration.yaml</filename> as a Nix attribute set.
|
|
|
|
Beware that setting this option will delete your previous <filename>configuration.yaml</filename>.
|
2019-12-20 09:40:16 +01:00
|
|
|
<link xlink:href="https://www.home-assistant.io/docs/configuration/secrets/">Secrets</link>
|
|
|
|
are encoded as strings as shown in the example.
|
2018-01-23 10:51:13 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-02-03 12:45:52 +01:00
|
|
|
configWritable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to make <filename>configuration.yaml</filename> writable.
|
|
|
|
This only has an effect if <option>config</option> is set.
|
|
|
|
This will allow you to edit it from Home Assistant's web interface.
|
|
|
|
However, bear in mind that it will be overwritten at every start of the service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-01-24 15:52:05 +01:00
|
|
|
lovelaceConfig = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with types; nullOr attrs;
|
|
|
|
# from https://www.home-assistant.io/lovelace/yaml-mode/
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
title = "My Awesome Home";
|
|
|
|
views = [ {
|
|
|
|
title = "Example";
|
|
|
|
cards = [ {
|
|
|
|
type = "markdown";
|
|
|
|
title = "Lovelace";
|
|
|
|
content = "Welcome to your **Lovelace UI**.";
|
|
|
|
} ];
|
|
|
|
} ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Your <filename>ui-lovelace.yaml</filename> as a Nix attribute set.
|
|
|
|
Setting this option will automatically add
|
|
|
|
<literal>lovelace.mode = "yaml";</literal> to your <option>config</option>.
|
|
|
|
Beware that setting this option will delete your previous <filename>ui-lovelace.yaml</filename>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-02-03 12:45:52 +01:00
|
|
|
lovelaceConfigWritable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to make <filename>ui-lovelace.yaml</filename> writable.
|
|
|
|
This only has an effect if <option>lovelaceConfig</option> is set.
|
|
|
|
This will allow you to edit it from Home Assistant's web interface.
|
|
|
|
However, bear in mind that it will be overwritten at every start of the service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-01-23 10:51:13 +01:00
|
|
|
package = mkOption {
|
2021-03-06 02:47:39 +01:00
|
|
|
default = pkgs.home-assistant.overrideAttrs (oldAttrs: {
|
|
|
|
doInstallCheck = false;
|
|
|
|
});
|
|
|
|
defaultText = literalExample ''
|
|
|
|
pkgs.home-assistant.overrideAttrs (oldAttrs: {
|
|
|
|
doInstallCheck = false;
|
|
|
|
})
|
|
|
|
'';
|
2018-01-23 10:51:13 +01:00
|
|
|
type = types.package;
|
|
|
|
example = literalExample ''
|
|
|
|
pkgs.home-assistant.override {
|
|
|
|
extraPackages = ps: with ps; [ colorlog ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
2021-03-06 02:47:39 +01:00
|
|
|
Home Assistant package to use. By default the tests are disabled, as they take a considerable amout of time to complete.
|
2018-07-20 19:28:29 +02:00
|
|
|
Override <literal>extraPackages</literal> or <literal>extraComponents</literal> in order to add additional dependencies.
|
|
|
|
If you specify <option>config</option> and do not set <option>autoExtraComponents</option>
|
|
|
|
to <literal>false</literal>, overriding <literal>extraComponents</literal> will have no effect.
|
2018-02-01 13:42:07 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
autoExtraComponents = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
2018-02-03 09:53:54 +01:00
|
|
|
If set to <literal>true</literal>, the components used in <literal>config</literal>
|
2018-02-01 13:42:07 +01:00
|
|
|
are set as the specified package's <literal>extraComponents</literal>.
|
|
|
|
This in turn adds all packaged dependencies to the derivation.
|
|
|
|
You might still see import errors in your log.
|
|
|
|
In this case, you will need to package the necessary dependencies yourself
|
|
|
|
or ask for someone else to package them.
|
|
|
|
If a dependency is packaged but not automatically added to this list,
|
|
|
|
you might need to specify it in <literal>extraPackages</literal>.
|
2018-01-23 10:51:13 +01:00
|
|
|
'';
|
|
|
|
};
|
2018-05-22 03:14:04 +02:00
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "Whether to open the firewall for the specified port.";
|
|
|
|
};
|
2018-01-23 10:51:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2018-05-22 03:14:04 +02:00
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
|
|
|
|
|
2018-01-23 10:51:13 +01:00
|
|
|
systemd.services.home-assistant = {
|
|
|
|
description = "Home Assistant";
|
|
|
|
after = [ "network.target" ];
|
2019-02-03 12:45:52 +01:00
|
|
|
preStart = optionalString (cfg.config != null) (if cfg.configWritable then ''
|
|
|
|
cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml"
|
|
|
|
'' else ''
|
2019-01-24 16:49:06 +01:00
|
|
|
rm -f "${cfg.configDir}/configuration.yaml"
|
|
|
|
ln -s ${configFile} "${cfg.configDir}/configuration.yaml"
|
2019-02-03 12:45:52 +01:00
|
|
|
'') + optionalString (cfg.lovelaceConfig != null) (if cfg.lovelaceConfigWritable then ''
|
|
|
|
cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
|
|
|
|
'' else ''
|
2019-01-24 16:49:06 +01:00
|
|
|
rm -f "${cfg.configDir}/ui-lovelace.yaml"
|
|
|
|
ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
|
2019-02-03 12:45:52 +01:00
|
|
|
'');
|
2018-01-23 10:51:13 +01:00
|
|
|
serviceConfig = {
|
2018-04-21 16:32:09 +02:00
|
|
|
ExecStart = "${package}/bin/hass --config '${cfg.configDir}'";
|
2020-05-31 10:22:22 +02:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2018-01-23 10:51:13 +01:00
|
|
|
User = "hass";
|
|
|
|
Group = "hass";
|
|
|
|
Restart = "on-failure";
|
|
|
|
ProtectSystem = "strict";
|
2020-11-19 13:29:03 +01:00
|
|
|
ReadWritePaths = let
|
|
|
|
cfgPath = [ "config" "homeassistant" "allowlist_external_dirs" ];
|
|
|
|
value = attrByPath cfgPath [] cfg;
|
|
|
|
allowPaths = if isList value then value else singleton value;
|
|
|
|
in [ "${cfg.configDir}" ] ++ allowPaths;
|
2018-11-01 16:39:37 +01:00
|
|
|
KillSignal = "SIGINT";
|
2018-01-23 10:51:13 +01:00
|
|
|
PrivateTmp = true;
|
2018-04-21 16:32:09 +02:00
|
|
|
RemoveIPC = true;
|
2018-04-29 01:06:20 +02:00
|
|
|
AmbientCapabilities = "cap_net_raw,cap_net_admin+eip";
|
2018-01-23 10:51:13 +01:00
|
|
|
};
|
2018-03-18 12:46:36 +01:00
|
|
|
path = [
|
|
|
|
"/run/wrappers" # needed for ping
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.targets.home-assistant = rec {
|
|
|
|
description = "Home Assistant";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "home-assistant.service" ];
|
|
|
|
after = wants;
|
2018-01-23 10:51:13 +01:00
|
|
|
};
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users.hass = {
|
2018-01-23 10:51:13 +01:00
|
|
|
home = cfg.configDir;
|
|
|
|
createHome = true;
|
|
|
|
group = "hass";
|
2019-12-19 12:12:12 +01:00
|
|
|
extraGroups = [ "dialout" ];
|
2018-01-23 10:51:13 +01:00
|
|
|
uid = config.ids.uids.hass;
|
|
|
|
};
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.groups.hass.gid = config.ids.gids.hass;
|
2018-01-23 10:51:13 +01:00
|
|
|
};
|
|
|
|
}
|