nixos/home-assistant: replace global with lib

This commit is contained in:
Martin Weinelt 2024-08-30 01:46:36 +02:00
commit 00b9d1f754
No known key found for this signature in database
GPG key ID: 87C1E9888F856759

View file

@ -1,8 +1,37 @@
{ config, lib, pkgs, ... }:
with lib;
let
inherit (lib)
any
attrByPath
attrValues
concatMap
converge
elem
escapeShellArg
escapeShellArgs
filter
filterAttrsRecursive
hasAttrByPath
isAttrs
isDerivation
isList
literalExpression
mkEnableOption
mkIf
mkMerge
mkOption
mkRemovedOptionModule
mkRenamedOptionModule
optionals
optionalString
recursiveUpdate
singleton
splitString
types
unique
;
cfg = config.services.home-assistant;
format = pkgs.formats.yaml {};
@ -17,7 +46,7 @@ let
# Filter null values from the configuration, so that we can still advertise
# optional options in the config attribute.
filteredConfig = lib.converge (lib.filterAttrsRecursive (_: v: ! elem v [ null ])) (lib.recursiveUpdate customLovelaceModulesResources (cfg.config or {}));
filteredConfig = converge (filterAttrsRecursive (_: v: ! elem v [ null ])) (recursiveUpdate customLovelaceModulesResources (cfg.config or {}));
configFile = renderYAMLFile "configuration.yaml" filteredConfig;
lovelaceConfigFile = renderYAMLFile "ui-lovelace.yaml" cfg.lovelaceConfig;
@ -42,7 +71,7 @@ let
if isDerivation config then
[ ]
else if isAttrs config then
optional (config ? platform) config.platform
optionals (config ? platform) [ config.platform ]
++ concatMap usedPlatforms (attrValues config)
else if isList config then
concatMap usedPlatforms config
@ -67,7 +96,7 @@ let
extraComponents = oldArgs.extraComponents or [] ++ extraComponents;
extraPackages = ps: (oldArgs.extraPackages or (_: []) ps)
++ (cfg.extraPackages ps)
++ (lib.concatMap (component: component.propagatedBuildInputs or []) cfg.customComponents);
++ (concatMap (component: component.propagatedBuildInputs or []) cfg.customComponents);
}));
# Create a directory that holds all lovelace modules
@ -93,7 +122,7 @@ in {
meta = {
buildDocsInSandbox = false;
maintainers = teams.home-assistant.members;
maintainers = lib.teams.home-assistant.members;
};
options.services.home-assistant = {
@ -471,12 +500,12 @@ in {
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) {
environment.etc = mkMerge [
(mkIf (cfg.config != null && !cfg.configWritable) {
"home-assistant/configuration.yaml".source = configFile;
})
(lib.mkIf (cfg.lovelaceConfig != null && !cfg.lovelaceConfigWritable) {
(mkIf (cfg.lovelaceConfig != null && !cfg.lovelaceConfigWritable) {
"home-assistant/ui-lovelace.yaml".source = lovelaceConfigFile;
})
];
@ -491,8 +520,8 @@ in {
"mysql.service"
"postgresql.service"
];
reloadTriggers = lib.optional (cfg.config != null) configFile
++ lib.optional (cfg.lovelaceConfig != null) lovelaceConfigFile;
reloadTriggers = optionals (cfg.config != null) [ configFile ]
++ optionals (cfg.lovelaceConfig != null) [ lovelaceConfigFile ];
preStart = let
copyConfig = if cfg.configWritable then ''
@ -541,20 +570,20 @@ in {
environment.PYTHONPATH = package.pythonPath;
serviceConfig = let
# List of capabilities to equip home-assistant with, depending on configured components
capabilities = lib.unique ([
capabilities = unique ([
# Empty string first, so we will never accidentally have an empty capability bounding set
# https://github.com/NixOS/nixpkgs/issues/120617#issuecomment-830685115
""
] ++ lib.optionals (builtins.any useComponent componentsUsingBluetooth) [
] ++ optionals (any useComponent componentsUsingBluetooth) [
# Required for interaction with hci devices and bluetooth sockets, identified by bluetooth-adapters dependency
# 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") [
] ++ 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") [
] ++ optionals (useComponent "nmap_tracker") [
# https://www.home-assistant.io/integrations/nmap_tracker#linux-capabilities
"CAP_NET_ADMIN"
"CAP_NET_BIND_SERVICE"