From 42c09098485931abe5051021fb6986fe6f8fa072 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Fri, 28 Jan 2022 22:54:19 +0100 Subject: nixos/home-assistant: move module into home-automation category Putting so many things into misc is not great, so let's open up a new category called home-automation here and now. --- .../services/home-automation/home-assistant.nix | 416 +++++++++++++++++++++ 1 file changed, 416 insertions(+) create mode 100644 nixos/modules/services/home-automation/home-assistant.nix (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix new file mode 100644 index 00000000000..fc8ce08b2e1 --- /dev/null +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -0,0 +1,416 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.home-assistant; + + # cfg.config != null can be assumed here + configJSON = pkgs.writeText "configuration.json" + (builtins.toJSON (if cfg.applyDefaultConfig then + (recursiveUpdate defaultConfig cfg.config) else cfg.config)); + configFile = pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } '' + ${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out + # 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 + ''; + + lovelaceConfigJSON = pkgs.writeText "ui-lovelace.json" + (builtins.toJSON cfg.lovelaceConfig); + lovelaceConfigFile = pkgs.runCommand "ui-lovelace.yaml" { preferLocalBuild = true; } '' + ${pkgs.remarshal}/bin/json2yaml -i ${lovelaceConfigJSON} -o $out + ''; + + availableComponents = cfg.package.availableComponents; + + explicitComponents = cfg.package.extraComponents; + + 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";`. + # + # For example, the component mqtt.sensor is used as follows: + # config.sensor = [ { + # platform = "mqtt"; + # ... + # } ]; + useComponentPlatform = component: elem component (usedPlatforms cfg.config); + + useExplicitComponent = component: elem component explicitComponents; + + # Returns whether component is used in config or explicitly passed into package + useComponent = component: + hasAttrByPath (splitString "." component) cfg.config + || useComponentPlatform component + || useExplicitComponent component; + + # List of components used in config + extraComponents = filter useComponent availableComponents; + + package = if (cfg.autoExtraComponents && cfg.config != null) + then (cfg.package.override { inherit extraComponents; }) + else cfg.package; + + # If you are changing this, please update the description in applyDefaultConfig + defaultConfig = { + homeassistant.time_zone = config.time.timeZone; + http.server_port = cfg.port; + } // optionalAttrs (cfg.lovelaceConfig != null) { + lovelace.mode = "yaml"; + }; + +in { + meta.maintainers = teams.home-assistant.members; + + options.services.home-assistant = { + # 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 + enable = mkEnableOption "Home Assistant. Please note that this installation method is unsupported upstream"; + + configDir = mkOption { + default = "/var/lib/hass"; + type = types.path; + description = "The config directory, where your configuration.yaml is located."; + }; + + port = mkOption { + default = 8123; + type = types.port; + 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. + + + Currently one side effect of enabling this is that the http component will be enabled. + + + This only takes effect if config != null in order to ensure that a manually managed configuration.yaml is not overwritten. + ''; + }; + + config = mkOption { + default = null; + # 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; + example = literalExpression '' + { + homeassistant = { + name = "Home"; + latitude = "!secret latitude"; + longitude = "!secret longitude"; + elevation = "!secret elevation"; + unit_system = "metric"; + time_zone = "UTC"; + }; + frontend = { + themes = "!include_dir_merge_named themes"; + }; + http = { }; + feedreader.urls = [ "https://nixos.org/blogs.xml" ]; + } + ''; + description = '' + Your configuration.yaml as a Nix attribute set. + Beware that setting this option will delete your previous configuration.yaml. + Secrets + are encoded as strings as shown in the example. + ''; + }; + + configWritable = mkOption { + default = false; + type = types.bool; + description = '' + Whether to make configuration.yaml writable. + This only has an effect if 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. + ''; + }; + + lovelaceConfig = mkOption { + default = null; + type = with types; nullOr attrs; + # from https://www.home-assistant.io/lovelace/yaml-mode/ + example = literalExpression '' + { + title = "My Awesome Home"; + views = [ { + title = "Example"; + cards = [ { + type = "markdown"; + title = "Lovelace"; + content = "Welcome to your **Lovelace UI**."; + } ]; + } ]; + } + ''; + description = '' + Your ui-lovelace.yaml as a Nix attribute set. + Setting this option will automatically add + lovelace.mode = "yaml"; to your . + Beware that setting this option will delete your previous ui-lovelace.yaml + ''; + }; + + lovelaceConfigWritable = mkOption { + default = false; + type = types.bool; + description = '' + Whether to make ui-lovelace.yaml writable. + This only has an effect if 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. + ''; + }; + + package = mkOption { + default = pkgs.home-assistant.overrideAttrs (oldAttrs: { + doInstallCheck = false; + }); + defaultText = literalExpression '' + pkgs.home-assistant.overrideAttrs (oldAttrs: { + doInstallCheck = false; + }) + ''; + type = types.package; + example = literalExpression '' + pkgs.home-assistant.override { + extraPackages = ps: with ps; [ colorlog ]; + } + ''; + description = '' + Home Assistant package to use. By default the tests are disabled, as they take a considerable amout of time to complete. + Override extraPackages or extraComponents in order to add additional dependencies. + If you specify and do not set + to false, overriding extraComponents will have no effect. + Avoid home-assistant.overridePythonAttrs if you use autoExtraComponents. + ''; + }; + + autoExtraComponents = mkOption { + default = true; + type = types.bool; + description = '' + If set to true, the components used in config + are set as the specified package's extraComponents. + 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 extraPackages. + ''; + }; + + openFirewall = mkOption { + default = false; + type = types.bool; + description = "Whether to open the firewall for the specified port."; + }; + }; + + config = mkIf cfg.enable { + networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ]; + + systemd.services.home-assistant = { + description = "Home Assistant"; + after = [ "network.target" ]; + preStart = optionalString (cfg.config != null) (if cfg.configWritable then '' + cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" + '' else '' + rm -f "${cfg.configDir}/configuration.yaml" + ln -s ${configFile} "${cfg.configDir}/configuration.yaml" + '') + optionalString (cfg.lovelaceConfig != null) (if cfg.lovelaceConfigWritable then '' + cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" + '' else '' + rm -f "${cfg.configDir}/ui-lovelace.yaml" + ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" + ''); + serviceConfig = let + # List of capabilities to equip home-assistant with, depending on configured components + capabilities = [ + # Empty string first, so we will never accidentally have an empty capability bounding set + # https://github.com/NixOS/nixpkgs/issues/120617#issuecomment-830685115 + "" + ] ++ (unique (optionals (useComponent "bluetooth_tracker" || useComponent "bluetooth_le_tracker") [ + # Required for interaction with hci devices and bluetooth sockets + # 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" + ])); + componentsUsingBluetooth = [ + # Components that require the AF_BLUETOOTH address family + "bluetooth_tracker" + "bluetooth_le_tracker" + ]; + componentsUsingPing = [ + # Components that require the capset syscall for the ping wrapper + "ping" + "wake_on_lan" + ]; + 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" + "arduino" + "blackbird" + "deconz" + "dsmr" + "edl21" + "elkm1" + "elv" + "enocean" + "firmata" + "flexit" + "gpsd" + "insteon" + "kwb" + "lacrosse" + "mhz19" + "modbus" + "modem_callerid" + "mysensors" + "nad" + "numato" + "rflink" + "rfxtrx" + "scsgate" + "serial" + "serial_pm" + "sms" + "upb" + "usb" + "velbus" + "w800rf32" + "xbee" + "zha" + "zwave" + "zwave_js" + ]; + in { + ExecStart = "${package}/bin/hass --config '${cfg.configDir}'"; + ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID"; + User = "hass"; + Group = "hass"; + Restart = "on-failure"; + RestartForceExitStatus = "100"; + SuccessExitStatus = "100"; + KillSignal = "SIGINT"; + + # Hardening + AmbientCapabilities = capabilities; + CapabilityBoundingSet = capabilities; + DeviceAllow = (optionals (any useComponent componentsUsingSerialDevices) [ + "char-ttyACM rw" + "char-ttyAMA rw" + "char-ttyUSB rw" + ]); + 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"; + ProcSubset = "all"; + ProtectSystem = "strict"; + RemoveIPC = true; + ReadWritePaths = let + # Allow rw access to explicitly configured paths + cfgPath = [ "config" "homeassistant" "allowlist_external_dirs" ]; + value = attrByPath cfgPath [] cfg; + allowPaths = if isList value then value else singleton value; + in [ "${cfg.configDir}" ] ++ allowPaths; + RestrictAddressFamilies = [ + "AF_INET" + "AF_INET6" + "AF_NETLINK" + "AF_UNIX" + ] ++ optionals (any useComponent componentsUsingBluetooth) [ + "AF_BLUETOOTH" + ]; + RestrictNamespaces = true; + RestrictRealtime = true; + RestrictSUIDSGID = true; + SupplementaryGroups = optionals (any useComponent componentsUsingSerialDevices) [ + "dialout" + ]; + SystemCallArchitectures = "native"; + SystemCallFilter = [ + "@system-service" + "~@privileged" + ] ++ optionals (any useComponent componentsUsingPing) [ + "capset" + ]; + UMask = "0077"; + }; + 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; + }; +} -- cgit 1.4.1 From 5aabba490e4a6001b9bda3f485210cdbec7e2d28 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Fri, 28 Jan 2022 22:55:27 +0100 Subject: nixos/home-assistant: update default package example The given example is now closer to a sane default people will want to start with. It also displays the existance of extraComponents, a feature that will receive more usage with home-assistant warning about components that have completely migrated away from YAML configuration. --- nixos/modules/services/home-automation/home-assistant.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index fc8ce08b2e1..79368416d71 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -201,7 +201,14 @@ in { type = types.package; example = literalExpression '' pkgs.home-assistant.override { - extraPackages = ps: with ps; [ colorlog ]; + extraPackages = python3Packages: with python3Packages; [ + psycopg2 + ]; + extraComponents = [ + "default_config" + "esphome" + "met" + ]; } ''; description = '' -- cgit 1.4.1 From 9896247fb6d2c646f19fcd90ade45d6abd89930b Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sat, 29 Jan 2022 16:36:42 +0100 Subject: nixos/home-assistant: Wait for network-online.target If people take the time to setup network-online.target correctly we should wait for it. If they don't it's basically the same as network.target anyway, so no harm done. Over time I've seen multiple integrations that have dealt badly with missing network connectivity at startup, this should alleviate further pains. --- nixos/modules/services/home-automation/home-assistant.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 79368416d71..db6fc39c306 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -247,7 +247,7 @@ in { systemd.services.home-assistant = { description = "Home Assistant"; - after = [ "network.target" ]; + after = [ "network-online.target" ]; preStart = optionalString (cfg.config != null) (if cfg.configWritable then '' cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" '' else '' -- cgit 1.4.1 From 59a367bcabae16ec0c18df29fda4b09dfa36ba53 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 30 Jan 2022 02:41:15 +0100 Subject: nixos/home-assistant: convert to rfc42 style settings After this change users with non-declarative configs need to set `services.home-assistant.config` to an `null`, or their `configuration.yaml` will be overwritten. The reason for this is that with rfc42 style defaults the config attribute set will never be empty by default. --- .../from_md/release-notes/rl-2205.section.xml | 17 ++ nixos/doc/manual/release-notes/rl-2205.section.md | 9 + .../services/home-automation/home-assistant.nix | 261 +++++++++++++-------- 3 files changed, 191 insertions(+), 96 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml index 4e64a02de81..4a6b539bcd0 100644 --- a/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml +++ b/nixos/doc/manual/from_md/release-notes/rl-2205.section.xml @@ -269,6 +269,23 @@ (ghc.withPackages.override { useLLVM = true; }) (p: []). + + + The home-assistant module now requires + users that don’t want their configuration to be managed + declaratively to set + services.home-assistant.config = null;. + This is required due to the way default settings are handled + with the new settings style. + + + Additionally the default list of + extraComponents now includes the minimal + dependencies to successfully complete the + onboarding + procedure. + + pkgs.emacsPackages.orgPackages is removed diff --git a/nixos/doc/manual/release-notes/rl-2205.section.md b/nixos/doc/manual/release-notes/rl-2205.section.md index 10349f96d4a..e06e7e385d1 100644 --- a/nixos/doc/manual/release-notes/rl-2205.section.md +++ b/nixos/doc/manual/release-notes/rl-2205.section.md @@ -91,6 +91,15 @@ In addition to numerous new and upgraded packages, this release has the followin `useLLVM`. So instead of `(ghc.withPackages (p: [])).override { withLLVM = true; }`, one needs to use `(ghc.withPackages.override { useLLVM = true; }) (p: [])`. +- The `home-assistant` module now requires users that don't want their + configuration to be managed declaratively to set + `services.home-assistant.config = null;`. This is required + due to the way default settings are handled with the new settings style. + + Additionally the default list of `extraComponents` now includes the minimal + dependencies to successfully complete the [onboarding](https://www.home-assistant.io/getting-started/onboarding/) + procedure. + - `pkgs.emacsPackages.orgPackages` is removed because org elpa is deprecated. The packages in the top level of `pkgs.emacsPackages`, such as org and org-contrib, refer to the ones in `pkgs.emacsPackages.elpaPackages` and diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index db6fc39c306..3c1b5b199d4 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -4,35 +4,27 @@ with lib; let cfg = config.services.home-assistant; + format = pkgs.formats.yaml {}; - # cfg.config != null can be assumed here - configJSON = pkgs.writeText "configuration.json" - (builtins.toJSON (if cfg.applyDefaultConfig then - (recursiveUpdate defaultConfig cfg.config) else cfg.config)); + # 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 {}; configFile = pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } '' - ${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out - # Hack to support custom yaml objects, - # i.e. secrets: https://www.home-assistant.io/docs/configuration/secrets/ + cp ${format.generate "configuration.yaml" filteredConfig} $out sed -i -e "s/'\!\([a-z_]\+\) \(.*\)'/\!\1 \2/;s/^\!\!/\!/;" $out ''; + lovelaceConfig = cfg.lovelaceConfig or {}; + lovelaceConfigFile = format.generate "ui-lovelace.yaml" lovelaceConfig; - lovelaceConfigJSON = pkgs.writeText "ui-lovelace.json" - (builtins.toJSON cfg.lovelaceConfig); - lovelaceConfigFile = pkgs.runCommand "ui-lovelace.yaml" { preferLocalBuild = true; } '' - ${pkgs.remarshal}/bin/json2yaml -i ${lovelaceConfigJSON} -o $out - ''; - + # Components advertised by the home-assistant package availableComponents = cfg.package.availableComponents; + # Components that were added by overriding the package explicitComponents = cfg.package.extraComponents; - - usedPlatforms = config: - if isAttrs config then - optional (config ? platform) config.platform - ++ concatMap usedPlatforms (attrValues config) - else if isList config then - concatMap usedPlatforms config - else [ ]; + useExplicitComponent = component: elem component explicitComponents; # Given a component "platform", looks up whether it is used in the config # as `platform = "platform";`. @@ -42,33 +34,42 @@ let # platform = "mqtt"; # ... # } ]; - useComponentPlatform = component: elem component (usedPlatforms cfg.config); + usedPlatforms = config: + if isAttrs config then + optional (config ? platform) config.platform + ++ concatMap usedPlatforms (attrValues config) + else if isList config then + concatMap usedPlatforms config + else [ ]; - useExplicitComponent = component: elem component explicitComponents; + useComponentPlatform = component: elem component (usedPlatforms cfg.config); - # Returns whether component is used in config or explicitly passed into package + # Returns whether component is used in config, explicitly passed into package or + # configured in the module. useComponent = component: hasAttrByPath (splitString "." component) cfg.config || useComponentPlatform component || useExplicitComponent component; - # List of components used in config + # Final list of components passed into the package to include required dependencies extraComponents = filter useComponent availableComponents; - package = if (cfg.autoExtraComponents && cfg.config != null) - then (cfg.package.override { inherit extraComponents; }) - else cfg.package; - - # If you are changing this, please update the description in applyDefaultConfig - defaultConfig = { - homeassistant.time_zone = config.time.timeZone; - http.server_port = cfg.port; - } // optionalAttrs (cfg.lovelaceConfig != null) { - lovelace.mode = "yaml"; - }; + package = (cfg.package.override { + inherit extraComponents; + }); in { - meta.maintainers = teams.home-assistant.members; + 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" ]) + ]; + + meta = { + buildDocsInSandbox = false; + maintainers = teams.home-assistant.members; + }; options.services.home-assistant = { # Running home-assistant on NixOS is considered an installation method that is unsupported by the upstream project. @@ -81,42 +82,117 @@ in { description = "The config directory, where your configuration.yaml is located."; }; - port = mkOption { - default = 8123; - type = types.port; - description = "The port on which to listen."; - }; + config = mkOption { + type = types.submodule { + 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. - 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. - - - Currently one side effect of enabling this is that the http component will be enabled. - - - This only takes effect if config != null in order to ensure that a manually managed configuration.yaml is not overwritten. - ''; - }; + homeassistant = { + # https://www.home-assistant.io/docs/configuration/basic/ + name = mkOption { + type = types.nullOr types.str; + default = null; + example = "Home"; + description = '' + Name of the location where Home Assistant is running. + ''; + }; - config = mkOption { - default = null; - # 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 = {}; + latitude = mkOption { + type = types.nullOr (types.either types.float types.str); + default = null; + example = 52.3; + description = '' + 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; + description = '' + 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"; + description = '' + 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"; + description = '' + Override temperature unit set by unit_system. C for Celsius, F for Fahrenheit. + ''; + }; + + time_zone = mkOption { + type = types.nullOr types.str; + default = config.time.timeZone or null; + defaultText = literalExpression '' + config.time.timeZone or null + ''; + example = "Europe/Amsterdam"; + description = '' + Pick your time zone from the column TZ of Wikipedia’s list of tz database time zones. + ''; + }; }; - in valueType; + + 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"; + description = '' + 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; + description = '' + 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"; + description = '' + In what mode should the main Lovelace panel be, yaml or storage (UI managed). + ''; + }; + }; + }; + }; example = literalExpression '' { homeassistant = { @@ -130,15 +206,19 @@ in { frontend = { themes = "!include_dir_merge_named themes"; }; - http = { }; + http = {}; feedreader.urls = [ "https://nixos.org/blogs.xml" ]; } ''; description = '' Your configuration.yaml as a Nix attribute set. - Beware that setting this option will delete your previous configuration.yaml. - Secrets - are encoded as strings as shown in the example. + + YAML functions like secrets + can be passed as a string and will be unquoted automatically. + + Unless this option is explicitly set to null + we assume your configuration.yaml is + managed through this module and thereby overwritten on startup. ''; }; @@ -147,16 +227,18 @@ in { type = types.bool; description = '' Whether to make configuration.yaml writable. - This only has an effect if is set. + This will allow you to edit it from Home Assistant's web interface. + + This only has an effect if is set. However, bear in mind that it will be overwritten at every start of the service. ''; }; lovelaceConfig = mkOption { default = null; - type = with types; nullOr attrs; - # from https://www.home-assistant.io/lovelace/yaml-mode/ + type = types.nullOr format.type; + # from https://www.home-assistant.io/lovelace/dashboards/ example = literalExpression '' { title = "My Awesome Home"; @@ -172,8 +254,8 @@ in { ''; description = '' Your ui-lovelace.yaml as a Nix attribute set. - Setting this option will automatically add - lovelace.mode = "yaml"; to your . + Setting this option will automatically set lovelace.mode to yaml. + Beware that setting this option will delete your previous ui-lovelace.yaml ''; }; @@ -183,8 +265,10 @@ in { type = types.bool; description = '' Whether to make ui-lovelace.yaml writable. - This only has an effect if is set. + This will allow you to edit it from Home Assistant's web interface. + + This only has an effect if is set. However, bear in mind that it will be overwritten at every start of the service. ''; }; @@ -212,7 +296,7 @@ in { } ''; description = '' - Home Assistant package to use. By default the tests are disabled, as they take a considerable amout of time to complete. + The Home Assistant package to use. Override extraPackages or extraComponents in order to add additional dependencies. If you specify and do not set to false, overriding extraComponents will have no effect. @@ -220,21 +304,6 @@ in { ''; }; - autoExtraComponents = mkOption { - default = true; - type = types.bool; - description = '' - If set to true, the components used in config - are set as the specified package's extraComponents. - 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 extraPackages. - ''; - }; - openFirewall = mkOption { default = false; type = types.bool; -- cgit 1.4.1 From 4a0b964b34c5ace3071a2fa3a0160ef179314893 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 30 Jan 2022 02:35:34 +0100 Subject: nixos/home-assistant: add extraComponents option It simply should not be required to override the package for such a common use case, especially since the module usually adds another override on top to inherit extraComponents. --- .../services/home-automation/home-assistant.nix | 36 +++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 3c1b5b199d4..59a88689969 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -49,14 +49,17 @@ let useComponent = component: hasAttrByPath (splitString "." component) cfg.config || useComponentPlatform component - || useExplicitComponent component; + || useExplicitComponent component + || builtins.elem component cfg.extraComponents; # Final list of components passed into the package to include required dependencies extraComponents = filter useComponent availableComponents; - package = (cfg.package.override { - inherit extraComponents; - }); + 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 ++ extraComponents; + })); in { imports = [ @@ -82,6 +85,31 @@ in { description = "The config directory, where your configuration.yaml is located."; }; + extraComponents = mkOption { + type = types.listOf (types.enum availableComponents); + default = [ + # List of components required to complete the onboarding + "default_config" + "met" + "esphome" + ]; + example = literalExpression '' + [ + "analytics" + "default_config" + "esphome" + "my" + "shopping_list" + "wled" + ] + ''; + description = '' + List of components that have their dependencies included in the package. + + The component name can be found in the URL, for example https://www.home-assistant.io/integrations/ffmpeg/ would map to ffmpeg. + ''; + }; + config = mkOption { type = types.submodule { freeformType = format.type; -- cgit 1.4.1 From 13faa004b65c15a5a687db38943eb34347e20479 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 30 Jan 2022 03:46:41 +0100 Subject: nixos/home-assistant: add extraPackages option --- .../services/home-automation/home-assistant.nix | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 59a88689969..eb70cf4c9e2 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -59,8 +59,8 @@ let # Respect overrides that already exist in the passed package and # concat it with values passed via the module. extraComponents = oldArgs.extraComponents ++ extraComponents; + extraPackages = ps: (oldArgs.extraPackages ps) ++ (cfg.extraPackages ps); })); - in { imports = [ # Migrations in NixOS 22.05 @@ -110,6 +110,26 @@ in { ''; }; + extraPackages = mkOption { + type = types.functionTo (types.listOf types.package); + default = _: []; + defaultText = literalExpression '' + python3Packages: with python3Packages; []; + ''; + example = literalExpression '' + python3Packages: with python3Packages; [ + # postgresql support + psycopg2 + ]; + ''; + description = '' + List of packages to add to propagatedBuildInputs. + + A popular example is python3Packages.psycopg2 + for PostgreSQL support in the recorder component. + ''; + }; + config = mkOption { type = types.submodule { freeformType = format.type; -- cgit 1.4.1 From 918100f48f76152fbfcf20689af33288067d1122 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 1 Feb 2022 21:25:12 +0100 Subject: nixos/home-assistant: Wait for {mysql,postgresql}.service Database provisioning was shown to be racy since adding a recorder test using PostgreSQL. There is no harm in waiting for these services, because if they're not available they will be ignored. --- nixos/modules/services/home-automation/home-assistant.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index eb70cf4c9e2..15aea7de902 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -364,7 +364,13 @@ in { systemd.services.home-assistant = { description = "Home Assistant"; - after = [ "network-online.target" ]; + after = [ + "network-online.target" + + # prevent races with database creation + "mysql.service" + "postgresql.service" + ]; preStart = optionalString (cfg.config != null) (if cfg.configWritable then '' cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" '' else '' -- cgit 1.4.1 From 2f644fd3e6e0e553b2a215b832128dcd2332f61c Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 30 Jan 2022 18:07:25 +0100 Subject: nixos/home-assistant: add rpi_power component by default on arm The rpi_power integration is part of the onboarding flow on Raspberry Pi SBCs. --- nixos/modules/services/home-automation/home-assistant.nix | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 15aea7de902..802a98b7c7d 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -92,6 +92,10 @@ in { "default_config" "met" "esphome" + ] ++ optionals (pkgs.stdenv.hostPlatform.isAarch32 || pkgs.stdenv.hostPlatform.isAarch64) [ + # Use the platform as an indicator that we might be running on a RaspberryPi and include + # relevant components + "rpi_power" ]; example = literalExpression '' [ -- cgit 1.4.1 From c1d2042219f20d169c415096aa1228162433ba77 Mon Sep 17 00:00:00 2001 From: piegames Date: Sat, 12 Feb 2022 19:47:47 +0100 Subject: home-assistant: clean up preStart Co-Authored-By: Martin Weinelt --- .../services/home-automation/home-assistant.nix | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 802a98b7c7d..bdd5e82bd26 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -375,17 +375,23 @@ in { "mysql.service" "postgresql.service" ]; - preStart = optionalString (cfg.config != null) (if cfg.configWritable then '' - cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" - '' else '' - rm -f "${cfg.configDir}/configuration.yaml" - ln -s ${configFile} "${cfg.configDir}/configuration.yaml" - '') + optionalString (cfg.lovelaceConfig != null) (if cfg.lovelaceConfigWritable then '' - cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" - '' else '' - rm -f "${cfg.configDir}/ui-lovelace.yaml" - ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" - ''); + preStart = let + copyConfig = if cfg.configWritable then '' + cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml" + '' else '' + rm -f "${cfg.configDir}/configuration.yaml" + ln -s ${configFile} "${cfg.configDir}/configuration.yaml" + ''; + copyLovelaceConfig = if cfg.lovelaceConfigWritable then '' + cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" + '' else '' + rm -f "${cfg.configDir}/ui-lovelace.yaml" + ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml" + ''; + in + (optionalString (cfg.config != null) copyConfig) + + (optionalString (cfg.lovelaceConfig != null) copyLovelaceConfig) + ; serviceConfig = let # List of capabilities to equip home-assistant with, depending on configured components capabilities = [ -- cgit 1.4.1 From 047429df52f6c7204206adaf95c0f8373e2df1d8 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Thu, 17 Feb 2022 00:45:38 +0100 Subject: nixos/home-assistant: fix package override The attributes can be missing on the package, since they're optional, so catch that by adding empty defaults. --- nixos/modules/services/home-automation/home-assistant.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index bdd5e82bd26..f4197650ab2 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -58,8 +58,8 @@ let 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 ++ extraComponents; - extraPackages = ps: (oldArgs.extraPackages ps) ++ (cfg.extraPackages ps); + extraComponents = oldArgs.extraComponents or [] ++ extraComponents; + extraPackages = ps: (oldArgs.extraPackages or (_: []) ps) ++ (cfg.extraPackages ps); })); in { imports = [ -- cgit 1.4.1 From 1090fcb7c937e6659f93ffbd7794e9f57de71e42 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 22 Feb 2022 12:04:04 +0100 Subject: nixos/home-assistant: allow null config value While the documentation said to set this to null, in case an imperative config was supposed to be used, this was not possible with the typing in place. --- nixos/modules/services/home-automation/home-assistant.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index f4197650ab2..5a40baf7fb1 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -135,7 +135,7 @@ in { }; config = mkOption { - type = types.submodule { + type = types.nullOr (types.submodule { freeformType = format.type; options = { # This is a partial selection of the most common options, so new users can quickly @@ -244,7 +244,7 @@ in { }; }; }; - }; + }); example = literalExpression '' { homeassistant = { -- cgit 1.4.1 From 0dd8ef5ef7bab2214b3ad2b34ba8466c37e9e078 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Tue, 22 Feb 2022 12:07:02 +0100 Subject: nixos/home-assistant: update package option description Overriding can now happen using module options, which is preferred because it is more discoverable and doesn't require knowledge of overrides in the first place. --- nixos/modules/services/home-automation/home-assistant.nix | 4 ---- 1 file changed, 4 deletions(-) (limited to 'nixos/modules/services/home-automation/home-assistant.nix') diff --git a/nixos/modules/services/home-automation/home-assistant.nix b/nixos/modules/services/home-automation/home-assistant.nix index 5a40baf7fb1..6022227f6ea 100644 --- a/nixos/modules/services/home-automation/home-assistant.nix +++ b/nixos/modules/services/home-automation/home-assistant.nix @@ -349,10 +349,6 @@ in { ''; description = '' The Home Assistant package to use. - Override extraPackages or extraComponents in order to add additional dependencies. - If you specify and do not set - to false, overriding extraComponents will have no effect. - Avoid home-assistant.overridePythonAttrs if you use autoExtraComponents. ''; }; -- cgit 1.4.1