{ config, lib, pkgs, ... }: let cfg = config.programs.music-light; toggleScript = pkgs.writeShellScriptBin "toggle-music-light" '' if systemctl --user is-active --quiet music-light.service; then exec systemctl --user stop music-light.service else exec systemctl --user start music-light.service fi ''; startScript = pkgs.writeShellScriptBin "start-music-light" '' exec systemctl --user start music-light.service ''; stopScript = pkgs.writeShellScriptBin "stop-music-light" '' exec systemctl --user stop music-light.service ''; statusScript = pkgs.writeShellScriptBin "music-light-status" '' if systemctl --user is-active --quiet music-light.service; then echo on else echo off fi ''; in { options.programs.music-light = { enable = lib.mkEnableOption "music-light integration managed by Home Manager"; scriptDir = lib.mkOption { type = lib.types.str; default = "${config.home.homeDirectory}/music-light"; example = "/home/vini/music-light"; description = "Directory that contains `luz-musica.py`."; }; pythonPath = lib.mkOption { type = lib.types.str; default = "/usr/bin/python3"; example = "/usr/bin/python3"; description = "Path to the host Python interpreter used by the service."; }; playerctlPath = lib.mkOption { type = lib.types.str; default = "/usr/bin/playerctl"; example = "/usr/bin/playerctl"; description = "Path to the host `playerctl` binary used by the service."; }; enableDesktopEntry = lib.mkOption { type = lib.types.bool; default = true; description = "Whether to create a desktop launcher that toggles the service."; }; autostart = lib.mkOption { type = lib.types.bool; default = false; description = "Whether to start `music-light.service` automatically at login."; }; }; config = lib.mkIf cfg.enable { home.packages = [ toggleScript startScript stopScript statusScript ]; xdg.desktopEntries = lib.mkIf cfg.enableDesktopEntry { music-light-toggle = { name = "Music Light Toggle"; comment = "Start or stop the music-light sync service"; exec = "${toggleScript}/bin/toggle-music-light"; terminal = false; categories = [ "Utility" ]; }; }; systemd.user.services.music-light = { Unit = { Description = "Sync Home Assistant light color with the active media artwork"; After = [ "graphical-session.target" "network-online.target" ]; PartOf = [ "graphical-session.target" ]; }; Service = { Type = "simple"; WorkingDirectory = cfg.scriptDir; ExecStart = "${cfg.pythonPath} ${cfg.scriptDir}/luz-musica.py"; Restart = "on-failure"; RestartSec = 3; Environment = [ "PATH=/usr/local/bin:/usr/bin:/bin" "PLAYERCTL_BIN=${cfg.playerctlPath}" ]; }; Install.WantedBy = lib.mkIf cfg.autostart [ "default.target" ]; }; }; }