nixos/home-assistant: add customLovelaceModules support

Allows the installation of custom lovelace modules, that can inject
css/js into the frontend and offer a wide variety of widgets.
This commit is contained in:
Martin Weinelt
2022-01-31 00:17:10 +01:00
parent d4914b6d15
commit 9a941c58e5
4 changed files with 65 additions and 2 deletions

View File

@@ -16,7 +16,8 @@ let
cp ${format.generate "configuration.yaml" filteredConfig} $out
sed -i -e "s/'\!\([a-z_]\+\) \(.*\)'/\!\1 \2/;s/^\!\!/\!/;" $out
'';
lovelaceConfig = cfg.lovelaceConfig or {};
lovelaceConfig = if (cfg.lovelaceConfig == null) then {}
else (lib.recursiveUpdate customLovelaceModulesResources cfg.lovelaceConfig);
lovelaceConfigFile = format.generate "ui-lovelace.yaml" lovelaceConfig;
# Components advertised by the home-assistant package
@@ -64,6 +65,20 @@ let
extraComponents = oldArgs.extraComponents or [] ++ extraComponents;
extraPackages = ps: (oldArgs.extraPackages or (_: []) ps) ++ (cfg.extraPackages ps);
}));
# Create a directory that holds all lovelace modules
customLovelaceModulesDir = pkgs.buildEnv {
name = "home-assistant-custom-lovelace-modules";
paths = cfg.customLovelaceModules;
};
# Create parts of the lovelace config that reference lovelave modules as resources
customLovelaceModulesResources = {
lovelace.resources = map (card: {
url = "/local/nixos-lovelace-modules/${card.entrypoint or card.pname}.js?${card.version}";
type = "module";
}) cfg.customLovelaceModules;
};
in {
imports = [
# Migrations in NixOS 22.05
@@ -137,6 +152,26 @@ in {
'';
};
customLovelaceModules = mkOption {
type = types.listOf types.package;
default = [];
example = literalExpression ''
with pkgs.home-assistant-custom-lovelace-modules; [
mini-graph-card
mini-media-player
];
'';
description = lib.mdDoc ''
List of custom lovelace card packages to load as lovelace resources.
Available cards can be found below `pkgs.home-assistant-custom-lovelace-modules`.
::: {.note}
Automatic loading only works with lovelace in `yaml` mode.
:::
'';
};
config = mkOption {
type = types.nullOr (types.submodule {
freeformType = format.type;
@@ -408,9 +443,16 @@ in {
rm -f "${cfg.configDir}/ui-lovelace.yaml"
ln -s /etc/home-assistant/ui-lovelace.yaml "${cfg.configDir}/ui-lovelace.yaml"
'';
copyCustomLovelaceModules = if cfg.customLovelaceModules != [] then ''
mkdir -p "${cfg.configDir}/www"
ln -fns ${customLovelaceModulesDir} "${cfg.configDir}/www/nixos-lovelace-modules"
'' else ''
rm -f "${cfg.configDir}/www/nixos-lovelace-modules"
'';
in
(optionalString (cfg.config != null) copyConfig) +
(optionalString (cfg.lovelaceConfig != null) copyLovelaceConfig)
(optionalString (cfg.lovelaceConfig != null) copyLovelaceConfig) +
copyCustomLovelaceModules
;
environment.PYTHONPATH = package.pythonPath;
serviceConfig = let