2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.services.home-assistant;
|
2022-02-20 05:27:41 +00:00
|
|
|
|
format = pkgs.formats.yaml {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Render config attribute sets to YAML
|
|
|
|
|
# Values that are null will be filtered from the output, so this is one way to have optional
|
|
|
|
|
# options shown in settings.
|
|
|
|
|
# We post-process the result to add support for YAML functions, like secrets or includes, see e.g.
|
|
|
|
|
# https://www.home-assistant.io/docs/configuration/secrets/
|
|
|
|
|
filteredConfig = lib.converge (lib.filterAttrsRecursive (_: v: ! elem v [ null ])) cfg.config or {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
configFile = pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
cp ${format.generate "configuration.yaml" filteredConfig} $out
|
2020-06-18 07:06:33 +00:00
|
|
|
|
sed -i -e "s/'\!\([a-z_]\+\) \(.*\)'/\!\1 \2/;s/^\!\!/\!/;" $out
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
2022-02-20 05:27:41 +00:00
|
|
|
|
lovelaceConfig = cfg.lovelaceConfig or {};
|
|
|
|
|
lovelaceConfigFile = format.generate "ui-lovelace.yaml" lovelaceConfig;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Components advertised by the home-assistant package
|
2020-04-24 23:36:52 +00:00
|
|
|
|
availableComponents = cfg.package.availableComponents;
|
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Components that were added by overriding the package
|
2021-12-06 16:07:01 +00:00
|
|
|
|
explicitComponents = cfg.package.extraComponents;
|
2022-02-20 05:27:41 +00:00
|
|
|
|
useExplicitComponent = component: elem component explicitComponents;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
# Given a component "platform", looks up whether it is used in the config
|
|
|
|
|
# as `platform = "platform";`.
|
|
|
|
|
#
|
|
|
|
|
# For example, the component mqtt.sensor is used as follows:
|
|
|
|
|
# config.sensor = [ {
|
|
|
|
|
# platform = "mqtt";
|
|
|
|
|
# ...
|
|
|
|
|
# } ];
|
2022-02-20 05:27:41 +00: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 [ ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
useComponentPlatform = component: elem component (usedPlatforms cfg.config);
|
2021-12-06 16:07:01 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Returns whether component is used in config, explicitly passed into package or
|
|
|
|
|
# configured in the module.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
useComponent = component:
|
|
|
|
|
hasAttrByPath (splitString "." component) cfg.config
|
2021-12-06 16:07:01 +00:00
|
|
|
|
|| useComponentPlatform component
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|| useExplicitComponent component
|
|
|
|
|
|| builtins.elem component cfg.extraComponents;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Final list of components passed into the package to include required dependencies
|
2020-04-24 23:36:52 +00:00
|
|
|
|
extraComponents = filter useComponent availableComponents;
|
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
package = (cfg.package.override (oldArgs: {
|
|
|
|
|
# Respect overrides that already exist in the passed package and
|
|
|
|
|
# concat it with values passed via the module.
|
|
|
|
|
extraComponents = oldArgs.extraComponents or [] ++ extraComponents;
|
|
|
|
|
extraPackages = ps: (oldArgs.extraPackages or (_: []) ps) ++ (cfg.extraPackages ps);
|
|
|
|
|
}));
|
|
|
|
|
in {
|
|
|
|
|
imports = [
|
|
|
|
|
# Migrations in NixOS 22.05
|
|
|
|
|
(mkRemovedOptionModule [ "services" "home-assistant" "applyDefaultConfig" ] "The default config was migrated into services.home-assistant.config")
|
|
|
|
|
(mkRemovedOptionModule [ "services" "home-assistant" "autoExtraComponents" ] "Components are now parsed from services.home-assistant.config unconditionally")
|
|
|
|
|
(mkRenamedOptionModule [ "services" "home-assistant" "port" ] [ "services" "home-assistant" "config" "http" "server_port" ])
|
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
meta = {
|
|
|
|
|
buildDocsInSandbox = false;
|
|
|
|
|
maintainers = teams.home-assistant.members;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.services.home-assistant = {
|
2021-06-28 23:13:55 +00:00
|
|
|
|
# Running home-assistant on NixOS is considered an installation method that is unsupported by the upstream project.
|
|
|
|
|
# https://github.com/home-assistant/architecture/blob/master/adr/0012-define-supported-installation-method.md#decision
|
2022-09-09 14:08:57 +00:00
|
|
|
|
enable = mkEnableOption (lib.mdDoc "Home Assistant. Please note that this installation method is unsupported upstream");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
configDir = mkOption {
|
|
|
|
|
default = "/var/lib/hass";
|
|
|
|
|
type = types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc "The config directory, where your {file}`configuration.yaml` is located.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
extraComponents = mkOption {
|
|
|
|
|
type = types.listOf (types.enum availableComponents);
|
|
|
|
|
default = [
|
|
|
|
|
# List of components required to complete the onboarding
|
|
|
|
|
"default_config"
|
|
|
|
|
"met"
|
|
|
|
|
"esphome"
|
2022-08-12 12:06:08 +00:00
|
|
|
|
] ++ optionals pkgs.stdenv.hostPlatform.isAarch [
|
2022-02-20 05:27:41 +00:00
|
|
|
|
# Use the platform as an indicator that we might be running on a RaspberryPi and include
|
|
|
|
|
# relevant components
|
|
|
|
|
"rpi_power"
|
|
|
|
|
];
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
[
|
|
|
|
|
"analytics"
|
|
|
|
|
"default_config"
|
|
|
|
|
"esphome"
|
|
|
|
|
"my"
|
|
|
|
|
"shopping_list"
|
|
|
|
|
"wled"
|
|
|
|
|
]
|
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
List of [components](https://www.home-assistant.io/integrations/) that have their dependencies included in the package.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
The component name can be found in the URL, for example `https://www.home-assistant.io/integrations/ffmpeg/` would map to `ffmpeg`.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
extraPackages = mkOption {
|
|
|
|
|
type = types.functionTo (types.listOf types.package);
|
|
|
|
|
default = _: [];
|
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
|
python3Packages: with python3Packages; [];
|
|
|
|
|
'';
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
python3Packages: with python3Packages; [
|
|
|
|
|
# postgresql support
|
|
|
|
|
psycopg2
|
|
|
|
|
];
|
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
List of packages to add to propagatedBuildInputs.
|
|
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
|
A popular example is `python3Packages.psycopg2`
|
2022-02-20 05:27:41 +00:00
|
|
|
|
for PostgreSQL support in the recorder component.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
2022-03-05 16:20:37 +00:00
|
|
|
|
type = types.nullOr (types.submodule {
|
2022-02-20 05:27:41 +00:00
|
|
|
|
freeformType = format.type;
|
|
|
|
|
options = {
|
|
|
|
|
# This is a partial selection of the most common options, so new users can quickly
|
|
|
|
|
# pick up how to match home-assistants config structure to ours. It also lets us preset
|
|
|
|
|
# config values intelligently.
|
|
|
|
|
|
|
|
|
|
homeassistant = {
|
|
|
|
|
# https://www.home-assistant.io/docs/configuration/basic/
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
example = "Home";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
Name of the location where Home Assistant is running.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
latitude = mkOption {
|
|
|
|
|
type = types.nullOr (types.either types.float types.str);
|
|
|
|
|
default = null;
|
|
|
|
|
example = 52.3;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
Latitude of your location required to calculate the time the sun rises and sets.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
longitude = mkOption {
|
|
|
|
|
type = types.nullOr (types.either types.float types.str);
|
|
|
|
|
default = null;
|
|
|
|
|
example = 4.9;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
Longitude of your location required to calculate the time the sun rises and sets.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unit_system = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ "metric" "imperial" ]);
|
|
|
|
|
default = null;
|
|
|
|
|
example = "metric";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
The unit system to use. This also sets temperature_unit, Celsius for Metric and Fahrenheit for Imperial.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
temperature_unit = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ "C" "F" ]);
|
|
|
|
|
default = null;
|
|
|
|
|
example = "C";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Override temperature unit set by unit_system. `C` for Celsius, `F` for Fahrenheit.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
time_zone = mkOption {
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = config.time.timeZone or null;
|
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
|
config.time.timeZone or null
|
|
|
|
|
'';
|
|
|
|
|
example = "Europe/Amsterdam";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Pick your time zone from the column TZ of Wikipedia’s [list of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
http = {
|
|
|
|
|
# https://www.home-assistant.io/integrations/http/
|
|
|
|
|
server_host = mkOption {
|
|
|
|
|
type = types.either types.str (types.listOf types.str);
|
|
|
|
|
default = [
|
|
|
|
|
"0.0.0.0"
|
|
|
|
|
"::"
|
|
|
|
|
];
|
|
|
|
|
example = "::1";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
Only listen to incoming requests on specific IP/host. The default listed assumes support for IPv4 and IPv6.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server_port = mkOption {
|
|
|
|
|
default = 8123;
|
|
|
|
|
type = types.port;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
The port on which to listen.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
lovelace = {
|
|
|
|
|
# https://www.home-assistant.io/lovelace/dashboards/
|
|
|
|
|
mode = mkOption {
|
|
|
|
|
type = types.enum [ "yaml" "storage" ];
|
|
|
|
|
default = if cfg.lovelaceConfig != null
|
|
|
|
|
then "yaml"
|
|
|
|
|
else "storage";
|
|
|
|
|
defaultText = literalExpression ''
|
|
|
|
|
if cfg.lovelaceConfig != null
|
|
|
|
|
then "yaml"
|
|
|
|
|
else "storage";
|
|
|
|
|
'';
|
|
|
|
|
example = "yaml";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
In what mode should the main Lovelace panel be, `yaml` or `storage` (UI managed).
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
2022-02-20 05:27:41 +00:00
|
|
|
|
};
|
2022-03-05 16:20:37 +00:00
|
|
|
|
});
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
|
|
|
|
homeassistant = {
|
|
|
|
|
name = "Home";
|
|
|
|
|
latitude = "!secret latitude";
|
|
|
|
|
longitude = "!secret longitude";
|
|
|
|
|
elevation = "!secret elevation";
|
|
|
|
|
unit_system = "metric";
|
|
|
|
|
time_zone = "UTC";
|
|
|
|
|
};
|
2020-06-18 07:06:33 +00:00
|
|
|
|
frontend = {
|
|
|
|
|
themes = "!include_dir_merge_named themes";
|
|
|
|
|
};
|
2022-02-20 05:27:41 +00:00
|
|
|
|
http = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
feedreader.urls = [ "https://nixos.org/blogs.xml" ];
|
|
|
|
|
}
|
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Your {file}`configuration.yaml` as a Nix attribute set.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
YAML functions like [secrets](https://www.home-assistant.io/docs/configuration/secrets/)
|
2022-02-20 05:27:41 +00:00
|
|
|
|
can be passed as a string and will be unquoted automatically.
|
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
Unless this option is explicitly set to `null`
|
|
|
|
|
we assume your {file}`configuration.yaml` is
|
2022-02-20 05:27:41 +00:00
|
|
|
|
managed through this module and thereby overwritten on startup.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configWritable = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Whether to make {file}`configuration.yaml` writable.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
This will allow you to edit it from Home Assistant's web interface.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
This only has an effect if {option}`config` is set.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
However, bear in mind that it will be overwritten at every start of the service.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
lovelaceConfig = mkOption {
|
|
|
|
|
default = null;
|
2022-02-20 05:27:41 +00:00
|
|
|
|
type = types.nullOr format.type;
|
|
|
|
|
# from https://www.home-assistant.io/lovelace/dashboards/
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
|
|
|
|
title = "My Awesome Home";
|
|
|
|
|
views = [ {
|
|
|
|
|
title = "Example";
|
|
|
|
|
cards = [ {
|
|
|
|
|
type = "markdown";
|
|
|
|
|
title = "Lovelace";
|
|
|
|
|
content = "Welcome to your **Lovelace UI**.";
|
|
|
|
|
} ];
|
|
|
|
|
} ];
|
|
|
|
|
}
|
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Your {file}`ui-lovelace.yaml` as a Nix attribute set.
|
|
|
|
|
Setting this option will automatically set `lovelace.mode` to `yaml`.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
Beware that setting this option will delete your previous {file}`ui-lovelace.yaml`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
lovelaceConfigWritable = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Whether to make {file}`ui-lovelace.yaml` writable.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
This will allow you to edit it from Home Assistant's web interface.
|
2022-02-20 05:27:41 +00:00
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
|
This only has an effect if {option}`lovelaceConfig` is set.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
However, bear in mind that it will be overwritten at every start of the service.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
package = mkOption {
|
2021-04-17 00:35:05 +00:00
|
|
|
|
default = pkgs.home-assistant.overrideAttrs (oldAttrs: {
|
|
|
|
|
doInstallCheck = false;
|
|
|
|
|
});
|
2021-10-06 13:57:05 +00:00
|
|
|
|
defaultText = literalExpression ''
|
2021-04-17 00:35:05 +00:00
|
|
|
|
pkgs.home-assistant.overrideAttrs (oldAttrs: {
|
|
|
|
|
doInstallCheck = false;
|
|
|
|
|
})
|
2021-03-09 03:18:52 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
type = types.package;
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
pkgs.home-assistant.override {
|
2022-02-20 05:27:41 +00:00
|
|
|
|
extraPackages = python3Packages: with python3Packages; [
|
|
|
|
|
psycopg2
|
|
|
|
|
];
|
|
|
|
|
extraComponents = [
|
|
|
|
|
"default_config"
|
|
|
|
|
"esphome"
|
|
|
|
|
"met"
|
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-02-20 05:27:41 +00:00
|
|
|
|
The Home Assistant package to use.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc "Whether to open the firewall for the specified port.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-05-18 14:49:53 +00:00
|
|
|
|
assertions = [
|
|
|
|
|
{
|
|
|
|
|
assertion = cfg.openFirewall -> !isNull cfg.config;
|
|
|
|
|
message = "openFirewall can only be used with a declarative config";
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.config.http.server_port ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-06-26 10:26:21 +00:00
|
|
|
|
# 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;
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
systemd.services.home-assistant = {
|
|
|
|
|
description = "Home Assistant";
|
2022-02-20 05:27:41 +00:00
|
|
|
|
after = [
|
|
|
|
|
"network-online.target"
|
|
|
|
|
|
|
|
|
|
# prevent races with database creation
|
|
|
|
|
"mysql.service"
|
|
|
|
|
"postgresql.service"
|
|
|
|
|
];
|
2022-07-14 12:49:19 +00:00
|
|
|
|
reloadTriggers = lib.optional (cfg.config != null) configFile
|
|
|
|
|
++ lib.optional (cfg.lovelaceConfig != null) lovelaceConfigFile;
|
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
|
preStart = let
|
|
|
|
|
copyConfig = if cfg.configWritable then ''
|
|
|
|
|
cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml"
|
|
|
|
|
'' else ''
|
|
|
|
|
rm -f "${cfg.configDir}/configuration.yaml"
|
2022-06-26 10:26:21 +00:00
|
|
|
|
ln -s /etc/home-assistant/configuration.yaml "${cfg.configDir}/configuration.yaml"
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
|
|
|
|
copyLovelaceConfig = if cfg.lovelaceConfigWritable then ''
|
|
|
|
|
cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
|
|
|
|
|
'' else ''
|
|
|
|
|
rm -f "${cfg.configDir}/ui-lovelace.yaml"
|
2022-06-26 10:26:21 +00:00
|
|
|
|
ln -s /etc/home-assistant/ui-lovelace.yaml "${cfg.configDir}/ui-lovelace.yaml"
|
2022-02-20 05:27:41 +00:00
|
|
|
|
'';
|
|
|
|
|
in
|
|
|
|
|
(optionalString (cfg.config != null) copyConfig) +
|
|
|
|
|
(optionalString (cfg.lovelaceConfig != null) copyLovelaceConfig)
|
|
|
|
|
;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
serviceConfig = let
|
|
|
|
|
# List of capabilities to equip home-assistant with, depending on configured components
|
2022-08-12 12:06:08 +00:00
|
|
|
|
capabilities = lib.unique ([
|
2021-05-03 20:48:10 +00:00
|
|
|
|
# Empty string first, so we will never accidentally have an empty capability bounding set
|
|
|
|
|
# https://github.com/NixOS/nixpkgs/issues/120617#issuecomment-830685115
|
|
|
|
|
""
|
2022-10-06 18:32:54 +00:00
|
|
|
|
] ++ lib.optionals (builtins.any useComponent componentsUsingBluetooth) [
|
2022-08-12 12:06:08 +00:00
|
|
|
|
# Required for interaction with hci devices and bluetooth sockets, identified by bluetooth-adapters dependency
|
2021-05-03 20:48:10 +00:00
|
|
|
|
# https://www.home-assistant.io/integrations/bluetooth_le_tracker/#rootless-setup-on-core-installs
|
|
|
|
|
"CAP_NET_ADMIN"
|
|
|
|
|
"CAP_NET_RAW"
|
|
|
|
|
] ++ lib.optionals (useComponent "emulated_hue") [
|
|
|
|
|
# Alexa looks for the service on port 80
|
|
|
|
|
# https://www.home-assistant.io/integrations/emulated_hue
|
|
|
|
|
"CAP_NET_BIND_SERVICE"
|
|
|
|
|
] ++ lib.optionals (useComponent "nmap_tracker") [
|
|
|
|
|
# https://www.home-assistant.io/integrations/nmap_tracker#linux-capabilities
|
|
|
|
|
"CAP_NET_ADMIN"
|
|
|
|
|
"CAP_NET_BIND_SERVICE"
|
|
|
|
|
"CAP_NET_RAW"
|
2022-08-12 12:06:08 +00:00
|
|
|
|
]);
|
2021-06-28 23:13:55 +00:00
|
|
|
|
componentsUsingBluetooth = [
|
|
|
|
|
# Components that require the AF_BLUETOOTH address family
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"august"
|
2022-11-04 12:27:35 +00:00
|
|
|
|
"august_ble"
|
|
|
|
|
"airthings_ble"
|
2022-12-17 10:02:37 +00:00
|
|
|
|
"aranet"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"bluemaestro"
|
|
|
|
|
"bluetooth"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
"bluetooth_le_tracker"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"bluetooth_tracker"
|
|
|
|
|
"bthome"
|
|
|
|
|
"default_config"
|
|
|
|
|
"eq3btsmart"
|
|
|
|
|
"esphome"
|
|
|
|
|
"fjaraskupan"
|
|
|
|
|
"govee_ble"
|
|
|
|
|
"homekit_controller"
|
|
|
|
|
"inkbird"
|
2022-11-04 12:27:35 +00:00
|
|
|
|
"keymitt_ble"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"led_ble"
|
|
|
|
|
"melnor"
|
|
|
|
|
"moat"
|
2022-11-04 12:27:35 +00:00
|
|
|
|
"oralb"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"qingping"
|
2022-12-17 10:02:37 +00:00
|
|
|
|
"ruuvitag_ble"
|
|
|
|
|
"sensirion_ble"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"sensorpro"
|
|
|
|
|
"sensorpush"
|
2022-12-17 10:02:37 +00:00
|
|
|
|
"shelly"
|
2022-11-04 12:27:35 +00:00
|
|
|
|
"snooz"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"switchbot"
|
|
|
|
|
"thermobeacon"
|
|
|
|
|
"thermopro"
|
2022-11-04 12:27:35 +00:00
|
|
|
|
"tilt_ble"
|
2022-10-06 18:32:54 +00:00
|
|
|
|
"xiaomi_ble"
|
|
|
|
|
"yalexs_ble"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
];
|
2022-01-26 04:04:25 +00:00
|
|
|
|
componentsUsingPing = [
|
|
|
|
|
# Components that require the capset syscall for the ping wrapper
|
|
|
|
|
"ping"
|
|
|
|
|
"wake_on_lan"
|
|
|
|
|
];
|
2021-06-28 23:13:55 +00:00
|
|
|
|
componentsUsingSerialDevices = [
|
|
|
|
|
# Components that require access to serial devices (/dev/tty*)
|
|
|
|
|
# List generated from home-assistant documentation:
|
|
|
|
|
# git clone https://github.com/home-assistant/home-assistant.io/
|
|
|
|
|
# cd source/_integrations
|
|
|
|
|
# rg "/dev/tty" -l | cut -d'/' -f3 | cut -d'.' -f1 | sort
|
|
|
|
|
# And then extended by references found in the source code, these
|
|
|
|
|
# mostly the ones using config flows already.
|
|
|
|
|
"acer_projector"
|
|
|
|
|
"alarmdecoder"
|
|
|
|
|
"blackbird"
|
2021-08-25 08:27:29 +00:00
|
|
|
|
"deconz"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
"dsmr"
|
|
|
|
|
"edl21"
|
|
|
|
|
"elkm1"
|
|
|
|
|
"elv"
|
|
|
|
|
"enocean"
|
|
|
|
|
"firmata"
|
|
|
|
|
"flexit"
|
|
|
|
|
"gpsd"
|
|
|
|
|
"insteon"
|
|
|
|
|
"kwb"
|
|
|
|
|
"lacrosse"
|
|
|
|
|
"modbus"
|
|
|
|
|
"modem_callerid"
|
|
|
|
|
"mysensors"
|
|
|
|
|
"nad"
|
|
|
|
|
"numato"
|
|
|
|
|
"rflink"
|
|
|
|
|
"rfxtrx"
|
|
|
|
|
"scsgate"
|
|
|
|
|
"serial"
|
|
|
|
|
"serial_pm"
|
|
|
|
|
"sms"
|
|
|
|
|
"upb"
|
2021-09-18 10:52:07 +00:00
|
|
|
|
"usb"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
"velbus"
|
|
|
|
|
"w800rf32"
|
|
|
|
|
"zha"
|
2021-07-24 12:14:16 +00:00
|
|
|
|
"zwave"
|
2021-09-18 10:52:07 +00:00
|
|
|
|
"zwave_js"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
];
|
2021-05-03 20:48:10 +00:00
|
|
|
|
in {
|
2022-02-10 20:34:41 +00:00
|
|
|
|
ExecStart = "${package}/bin/hass --config '${cfg.configDir}'";
|
2020-06-15 15:56:04 +00:00
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
User = "hass";
|
|
|
|
|
Group = "hass";
|
|
|
|
|
Restart = "on-failure";
|
2021-05-03 20:48:10 +00:00
|
|
|
|
RestartForceExitStatus = "100";
|
|
|
|
|
SuccessExitStatus = "100";
|
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
|
|
|
|
|
|
# Hardening
|
|
|
|
|
AmbientCapabilities = capabilities;
|
|
|
|
|
CapabilityBoundingSet = capabilities;
|
2021-06-28 23:13:55 +00:00
|
|
|
|
DeviceAllow = (optionals (any useComponent componentsUsingSerialDevices) [
|
2021-05-03 20:48:10 +00:00
|
|
|
|
"char-ttyACM rw"
|
|
|
|
|
"char-ttyAMA rw"
|
|
|
|
|
"char-ttyUSB rw"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
]);
|
2021-05-03 20:48:10 +00:00
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
|
LockPersonality = true;
|
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
|
PrivateTmp = true;
|
|
|
|
|
PrivateUsers = false; # prevents gaining capabilities in the host namespace
|
|
|
|
|
ProtectClock = true;
|
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
|
ProtectHome = true;
|
|
|
|
|
ProtectHostname = true;
|
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
|
ProtectProc = "invisible";
|
2021-05-20 23:08:51 +00:00
|
|
|
|
ProcSubset = "all";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
ProtectSystem = "strict";
|
2021-05-03 20:48:10 +00:00
|
|
|
|
RemoveIPC = true;
|
2020-11-30 08:33:03 +00:00
|
|
|
|
ReadWritePaths = let
|
2021-05-03 20:48:10 +00:00
|
|
|
|
# Allow rw access to explicitly configured paths
|
2020-11-30 08:33:03 +00:00
|
|
|
|
cfgPath = [ "config" "homeassistant" "allowlist_external_dirs" ];
|
|
|
|
|
value = attrByPath cfgPath [] cfg;
|
|
|
|
|
allowPaths = if isList value then value else singleton value;
|
|
|
|
|
in [ "${cfg.configDir}" ] ++ allowPaths;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
RestrictAddressFamilies = [
|
|
|
|
|
"AF_INET"
|
|
|
|
|
"AF_INET6"
|
2021-05-20 23:08:51 +00:00
|
|
|
|
"AF_NETLINK"
|
|
|
|
|
"AF_UNIX"
|
2021-06-28 23:13:55 +00:00
|
|
|
|
] ++ optionals (any useComponent componentsUsingBluetooth) [
|
2021-05-03 20:48:10 +00:00
|
|
|
|
"AF_BLUETOOTH"
|
|
|
|
|
];
|
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
|
RestrictRealtime = true;
|
|
|
|
|
RestrictSUIDSGID = true;
|
2021-06-28 23:13:55 +00:00
|
|
|
|
SupplementaryGroups = optionals (any useComponent componentsUsingSerialDevices) [
|
|
|
|
|
"dialout"
|
|
|
|
|
];
|
2021-05-03 20:48:10 +00:00
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
|
SystemCallFilter = [
|
|
|
|
|
"@system-service"
|
|
|
|
|
"~@privileged"
|
2022-01-26 04:04:25 +00:00
|
|
|
|
] ++ optionals (any useComponent componentsUsingPing) [
|
|
|
|
|
"capset"
|
2021-05-03 20:48:10 +00:00
|
|
|
|
];
|
|
|
|
|
UMask = "0077";
|
2020-04-24 23:36:52 +00: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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.users.hass = {
|
|
|
|
|
home = cfg.configDir;
|
|
|
|
|
createHome = true;
|
|
|
|
|
group = "hass";
|
|
|
|
|
uid = config.ids.uids.hass;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.groups.hass.gid = config.ids.gids.hass;
|
|
|
|
|
};
|
|
|
|
|
}
|