diff --git a/devices/homie/modules/home-assistant/default.nix b/devices/homie/modules/home-assistant/default.nix index f264c9d8..28ad43b9 100644 --- a/devices/homie/modules/home-assistant/default.nix +++ b/devices/homie/modules/home-assistant/default.nix @@ -1,4 +1,4 @@ -{pkgs, ...}: { +{pkgs, self, ...}: { imports = [ ./assist.nix ./bluetooth.nix @@ -7,9 +7,9 @@ ./netdaemon ./spotify.nix ./timer.nix - ]; - # TODO: nix: add HA options for custom_sentences and stuff + self.nixosModules.ha-plus + ]; services.home-assistant = { enable = true; diff --git a/devices/homie/modules/home-assistant/frontend.nix b/devices/homie/modules/home-assistant/frontend.nix index 4ec95279..523628d9 100644 --- a/devices/homie/modules/home-assistant/frontend.nix +++ b/devices/homie/modules/home-assistant/frontend.nix @@ -3,31 +3,18 @@ dracul-ha-src, material-rounded-theme-src, material-symbols-src, - lib, pkgs, ... }: let - inherit (lib) concatStringsSep getExe; inherit (pkgs.writers) writeYAML; - - themes = [ - "${caule-themes-src}/themes/caule-themes-pack-1.yaml" - "${dracul-ha-src}/themes/dracul-ha.yaml" - "${material-rounded-theme-src}/themes/material_rounded.yaml" - ]; in { - systemd.services.home-assistant.preStart = let - WorkingDirectory = "/var/lib/hass"; - in - getExe (pkgs.writeShellApplication { - name = "ha-themes"; - text = '' - mkdir -p ${WorkingDirectory}/themes - cp -f ${concatStringsSep " " themes} ${WorkingDirectory}/themes - ''; - }); - services.home-assistant = { + configFiles = { + "themes/caule.yaml".source = "${caule-themes-src}/themes/caule-themes-pack-1.yaml"; + "themes/dracul-ha.yaml".source = "${dracul-ha-src}/themes/dracul-ha.yaml"; + "themes/material_rounded.yaml".source = "${material-rounded-theme-src}/themes/material_rounded.yaml"; + }; + customLovelaceModules = builtins.attrValues { inherit (pkgs.home-assistant-custom-lovelace-modules) diff --git a/devices/homie/modules/home-assistant/spotify.nix b/devices/homie/modules/home-assistant/spotify.nix index f6e08562..0e0a8f9b 100644 --- a/devices/homie/modules/home-assistant/spotify.nix +++ b/devices/homie/modules/home-assistant/spotify.nix @@ -1,27 +1,9 @@ { config, - lib, pkgs, self, ... -}: let - inherit (lib) getExe; - inherit (pkgs.writers) writeYAML; -in { - systemd.services.home-assistant.preStart = let - WorkingDirectory = "/var/lib/hass"; - creds = config.sops.secrets.spotifyd.path; - spotify = writeYAML "assist_spotify.yaml" (import ./spotify-sentences.nix); - in - getExe (pkgs.writeShellApplication { - name = "spotify-files"; - text = '' - mkdir -p ${WorkingDirectory}/custom_sentences/en - cp -f ${spotify} ${WorkingDirectory}/custom_sentences/en/assist_spotify.yaml - cp -f ${creds} ${WorkingDirectory}/.storage/SpotifyWebApiPython_librespot_credentials.json - ''; - }); - +}: { services.home-assistant = { customComponents = builtins.attrValues { inherit @@ -34,6 +16,12 @@ in { "spotify" ]; + customSentences."assist_spotify" = import ./spotify-sentences.nix; + + configFiles. + ".storage/SpotifyWebApiPython_librespot_credentials.json" + .source = config.sops.secrets.spotifyd.path; + config.intent_script = { PlayAlbum = { async_action = "false"; diff --git a/devices/homie/modules/home-assistant/timer.nix b/devices/homie/modules/home-assistant/timer.nix index e92a918a..f04efeeb 100644 --- a/devices/homie/modules/home-assistant/timer.nix +++ b/devices/homie/modules/home-assistant/timer.nix @@ -1,11 +1,6 @@ # From https://github.com/don86nl/ha_intents/blob/main/config/packages/assist_timers.yaml -{ - lib, - pkgs, - ... -}: let - inherit (lib) concatStrings concatStringsSep getExe; - inherit (pkgs.writers) writeYAML; +{lib, ...}: let + inherit (lib) concatStrings concatStringsSep; mkTimer = id: { "assist_timer${toString id}" = { @@ -43,20 +38,9 @@ timer_media_location = "/path/to/file.mp3"; }; in { - systemd.services.home-assistant.preStart = let - WorkingDirectory = "/var/lib/hass"; - - timer = writeYAML "assist_timers.yaml" (import ./timer-sentences.nix); - in - getExe (pkgs.writeShellApplication { - name = "timer-files"; - text = '' - mkdir -p ${WorkingDirectory}/custom_sentences/en - cp -f ${timer} ${WorkingDirectory}/custom_sentences/en/assist_timers.yaml - ''; - }); - services.home-assistant = { + customSentences."assist_timers" = import ./timer-sentences.nix; + config = { homeassistant.customize."script.assist_timerstart" = {inherit settings;}; diff --git a/nixosModules/default.nix b/nixosModules/default.nix index fa7254b1..d12d9d80 100644 --- a/nixosModules/default.nix +++ b/nixosModules/default.nix @@ -4,6 +4,7 @@ self: { desktop = import ./desktop self; docker = import ./docker self.inputs.khepri; esphome-plus = import ./esphome-plus; + ha-plus = import ./ha-plus; kmscon = import ./kmscon; nvidia = import ./nvidia; plymouth = import ./plymouth; diff --git a/nixosModules/ha-plus/default.nix b/nixosModules/ha-plus/default.nix new file mode 100644 index 00000000..0f8b0a9d --- /dev/null +++ b/nixosModules/ha-plus/default.nix @@ -0,0 +1,104 @@ +{ + config, + lib, + pkgs, + ... +}: let + inherit (lib) any attrValues concatMapStringsSep getExe mapAttrs' mkDefault mkDerivedConfig mkIf mkOption nameValuePair replaceStrings types; + + cfg = config.services.home-assistant; + format = pkgs.formats.yaml {}; + configFilesList = attrValues cfg.configFiles; +in { + options.services.home-assistant = { + configFiles = mkOption { + default = {}; + description = '' + Set of files that have to be linked in the configuration directory. + ''; + + type = types.attrsOf (types.submodule ( + { + name, + config, + options, + ... + }: { + options = { + enable = mkOption { + type = types.bool; + default = true; + description = '' + Whether this file should be generated. This + option allows specific files to be disabled. + ''; + }; + + target = mkOption { + type = types.str; + description = '' + Name of symlink (relative to config directory). + Defaults to the attribute name. + ''; + }; + + text = mkOption { + default = null; + type = types.nullOr types.lines; + description = "Text of the file."; + }; + + source = mkOption { + type = types.path; + description = "Path of the source file."; + }; + }; + + config = { + target = mkDefault name; + source = mkIf (config.text != null) ( + let + name' = "haConf-" + replaceStrings ["/"] ["-"] name; + in + mkDerivedConfig options.text (pkgs.writeText name') + ); + }; + } + )); + }; + + customSentences = mkOption { + type = types.attrsOf (types.submodule { + freeformType = format.type; + options.language = mkOption { + type = types.str; + }; + }); + }; + }; + + config = mkIf cfg.enable { + systemd.services.home-assistant = + mkIf ( + cfg.configFiles != {} && any (c: c.enable) configFilesList + ) { + preStart = let + inherit (cfg) configDir; + mkLink = configFile: '' + mkdir -p ${configDir}/${dirOf configFile.target} + cp -rf ${configFile.source} ${configDir}/${configFile.target} + ''; + in + getExe (pkgs.writeShellApplication { + name = "home-assistant-pre-start"; + text = concatMapStringsSep "\n" mkLink configFilesList; + }); + }; + + services.home-assistant.configFiles = mapAttrs' (n: v: + nameValuePair "custom_sentences/${v.language}/${n}.yaml" { + source = format.generate n v; + }) + cfg.customSentences; + }; +}