From 1dbb3fd2f79c804859e6b65fd42f164f7527ff39 Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Mon, 8 Sep 2025 14:58:26 -0700 Subject: [PATCH] radio-active: add module to create config files Configuration entry similar to; ```nix programs.radio-active.enable = true; ``` By default `ffplay` is used for recording/playback, but that can be changed by applying either of the following; ```nix programs.radio-active.settings.AppConfig.player = "vlc"; programs.radio-active.settings.AppConfig.player = "mpv"; ``` All other configuration options documented by; https://github.com/deep5050/radio-active?tab=readme-ov-file#default-configs maybe applied under the `AppConfig` attribute set. Finally, the `aliases` attribute set allows for defining key/value pares that will generate a `~/.radio-active-alias` of bookmarked stations, for example something like; ```nix programs.radio-active.aliases = { "Deep House Lounge" = "http://198.15.94.34:8006/stream"; }; ``` ... will result in; ``` Deep House Lounge==http://198.15.94.34:8006/stream ``` WARN: Darwin hosts may report issues about `pkgs.vlc` Co-authored-by: Robert Helgesson --- .../misc/news/2025/09/2025-09-28_06-57-30.nix | 12 ++ modules/programs/radio-active.nix | 148 ++++++++++++++++++ .../programs/radio-active/AppConfig.nix | 32 ++++ .../programs/radio-active/AppConfig_all.nix | 32 ++++ .../radio-active/AppConfig_player_mpv.nix | 16 ++ .../radio-active/AppConfig_player_vlc.nix | 16 ++ .../modules/programs/radio-active/aliases.nix | 17 ++ .../modules/programs/radio-active/default.nix | 11 ++ 8 files changed, 284 insertions(+) create mode 100644 modules/misc/news/2025/09/2025-09-28_06-57-30.nix create mode 100644 modules/programs/radio-active.nix create mode 100644 tests/modules/programs/radio-active/AppConfig.nix create mode 100644 tests/modules/programs/radio-active/AppConfig_all.nix create mode 100644 tests/modules/programs/radio-active/AppConfig_player_mpv.nix create mode 100644 tests/modules/programs/radio-active/AppConfig_player_vlc.nix create mode 100644 tests/modules/programs/radio-active/aliases.nix create mode 100644 tests/modules/programs/radio-active/default.nix diff --git a/modules/misc/news/2025/09/2025-09-28_06-57-30.nix b/modules/misc/news/2025/09/2025-09-28_06-57-30.nix new file mode 100644 index 000000000..2810186d3 --- /dev/null +++ b/modules/misc/news/2025/09/2025-09-28_06-57-30.nix @@ -0,0 +1,12 @@ +{ + time = "2025-09-28T06:57:30+00:00"; + condition = true; + message = '' + A new module is available: 'programs.radio-active' + + `radio-active` is a TUI which enables playing of radio station streams. + + This module allows for defining configuration file as well as favorites + list via Nix attribute sets. + ''; +} diff --git a/modules/programs/radio-active.nix b/modules/programs/radio-active.nix new file mode 100644 index 000000000..455f5ced6 --- /dev/null +++ b/modules/programs/radio-active.nix @@ -0,0 +1,148 @@ +{ + config, + lib, + pkgs, + ... +}: +let + inherit (lib) + mapAttrsToList + mkEnableOption + mkIf + mkOption + mkPackageOption + ; + + inherit (lib.attrsets) + attrByPath + ; + + inherit (lib.types) + nonEmptyStr + attrsOf + oneOf + ; + + inherit (lib.types.numbers) + nonnegative + ; + + inherit (pkgs.formats) + ini + ; + + iniFormat = ini { }; + + cfg = config.programs.radio-active; +in +{ + meta.maintainers = [ + lib.maintainers.S0AndS0 + ]; + + options.programs.radio-active = { + enable = mkEnableOption "Enable installing radio-active and writing configuration file"; + + package = mkPackageOption pkgs "radio-active" { + nullable = true; + }; + + settings = mkOption { + type = attrsOf ( + attrsOf (oneOf [ + nonEmptyStr + nonnegative + ]) + ); + default = { }; + example = { + loglevel = "debug"; + limit = 41; + sort = "votes"; + filter = "none"; + volume = 68; + filepath = "/home/{user}/recordings/radioactive/"; + filetype = "mp3"; + player = "ffplay"; + }; + description = '' + Declare-able configurations for radio-active written to + {file}`$XDG_CONFIG_HOME/radio-active/configs.ini`. + ''; + }; + + aliases = mkOption { + type = attrsOf nonEmptyStr; + default = { }; + example = { + "Deep House Lounge" = "http://198.15.94.34:8006/stream"; + }; + description = '' + Key/value pairs where the key is name of radio station and value is URL. + ''; + }; + }; + + config = + let + player = attrByPath [ "settings" "AppConfig" "player" ] "ffplay" cfg; + + knownPlayers = [ + "ffplay" + "mpv" + "vlc" + ]; + in + mkIf cfg.enable { + warnings = lib.optional (builtins.elem player knownPlayers == false) '' + Unknown player defined in `config.programs.radio-active.AppConfig.player` + ''; + + ## TODO: test that dependency `postPatch` modifications works at runtime + home.packages = + let + radio-active = + if player == "mpv" then + pkgs.radio-active.overrideAttrs ( + finalAttrs: previousAttrs: { + postPatch = '' + ${previousAttrs.postPatch} + + substituteInPlace radioactive/mpv.py \ + --replace-fail 'self.exe_path = which(self.program_name)' \ + 'self.exe_path = "${lib.getExe pkgs.mpv}"' + ''; + } + ) + else if player == "vlc" then + pkgs.radio-active.overrideAttrs ( + finalAttrs: previousAttrs: { + postPatch = '' + ${previousAttrs.postPatch} + + substituteInPlace radioactive/vlc.py \ + --replace-fail 'self.exe_path = which(self.program_name)' \ + 'self.exe_path = "${lib.getExe pkgs.vlc}"' + ''; + } + ) + else + pkgs.radio-active; + in + mkIf (cfg.package != null) [ + radio-active + ]; + + xdg.configFile."radio-active/configs.ini" = + lib.mkIf (cfg.settings != { } && cfg.settings.AppConfig != { }) + { + source = iniFormat.generate "radio-active-config" cfg.settings; + }; + + home.file.".radio-active-alias" = mkIf (cfg.aliases != { }) { + text = '' + ${builtins.concatStringsSep "\n" (mapAttrsToList (name: value: "${name}==${value}") cfg.aliases)} + ''; + }; + }; +} diff --git a/tests/modules/programs/radio-active/AppConfig.nix b/tests/modules/programs/radio-active/AppConfig.nix new file mode 100644 index 000000000..8c322dc9b --- /dev/null +++ b/tests/modules/programs/radio-active/AppConfig.nix @@ -0,0 +1,32 @@ +{ + programs.radio-active = { + enable = true; + + settings.AppConfig = { + filepath = "/mnt/{user}/recordings/radioactive/"; + filetype = "auto"; + filter = "name=shows"; + limit = 41; + loglevel = "debug"; + player = "ffplay"; + sort = "random"; + volume = 68; + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/radio-active/configs.ini + assertFileContent home-files/.config/radio-active/configs.ini \ + ${builtins.toFile "expected.radio-active_configs.ini" '' + [AppConfig] + filepath=/mnt/{user}/recordings/radioactive/ + filetype=auto + filter=name=shows + limit=41 + loglevel=debug + player=ffplay + sort=random + volume=68 + ''} + ''; +} diff --git a/tests/modules/programs/radio-active/AppConfig_all.nix b/tests/modules/programs/radio-active/AppConfig_all.nix new file mode 100644 index 000000000..f6dd26a97 --- /dev/null +++ b/tests/modules/programs/radio-active/AppConfig_all.nix @@ -0,0 +1,32 @@ +{ + programs.radio-active = { + enable = true; + + settings.AppConfig = { + filepath = "/home/{user}/recordings/radioactive/"; + filetype = "mp3"; + filter = "none"; + limit = 41; + loglevel = "debug"; + player = "ffplay"; + sort = "votes"; + volume = 68; + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/radio-active/configs.ini + assertFileContent home-files/.config/radio-active/configs.ini \ + ${builtins.toFile "expected.all.radio-active_configs.ini" '' + [AppConfig] + filepath=/home/{user}/recordings/radioactive/ + filetype=mp3 + filter=none + limit=41 + loglevel=debug + player=ffplay + sort=votes + volume=68 + ''} + ''; +} diff --git a/tests/modules/programs/radio-active/AppConfig_player_mpv.nix b/tests/modules/programs/radio-active/AppConfig_player_mpv.nix new file mode 100644 index 000000000..4074d6e4f --- /dev/null +++ b/tests/modules/programs/radio-active/AppConfig_player_mpv.nix @@ -0,0 +1,16 @@ +{ + programs.radio-active = { + enable = true; + + settings.AppConfig.player = "mpv"; + }; + + nmt.script = '' + assertFileExists home-files/.config/radio-active/configs.ini + assertFileContent home-files/.config/radio-active/configs.ini \ + ${builtins.toFile "expected.player_mpv.radio-active_configs.ini" '' + [AppConfig] + player=mpv + ''} + ''; +} diff --git a/tests/modules/programs/radio-active/AppConfig_player_vlc.nix b/tests/modules/programs/radio-active/AppConfig_player_vlc.nix new file mode 100644 index 000000000..0774d66b9 --- /dev/null +++ b/tests/modules/programs/radio-active/AppConfig_player_vlc.nix @@ -0,0 +1,16 @@ +{ + programs.radio-active = { + enable = true; + + settings.AppConfig.player = "vlc"; + }; + + nmt.script = '' + assertFileExists home-files/.config/radio-active/configs.ini + assertFileContent home-files/.config/radio-active/configs.ini \ + ${builtins.toFile "expected.player_mpv.radio-active_configs.ini" '' + [AppConfig] + player=vlc + ''} + ''; +} diff --git a/tests/modules/programs/radio-active/aliases.nix b/tests/modules/programs/radio-active/aliases.nix new file mode 100644 index 000000000..a81db935a --- /dev/null +++ b/tests/modules/programs/radio-active/aliases.nix @@ -0,0 +1,17 @@ +{ + programs.radio-active = { + enable = true; + + aliases = { + "Deep House Lounge" = "http://198.15.94.34:8006/stream"; + }; + }; + + nmt.script = '' + assertFileExists home-files/.radio-active-alias + assertFileContent home-files/.radio-active-alias \ + ${builtins.toFile "expected.radio-active_aliases.ini" '' + Deep House Lounge==http://198.15.94.34:8006/stream + ''} + ''; +} diff --git a/tests/modules/programs/radio-active/default.nix b/tests/modules/programs/radio-active/default.nix new file mode 100644 index 000000000..72c19ff93 --- /dev/null +++ b/tests/modules/programs/radio-active/default.nix @@ -0,0 +1,11 @@ +{ lib, pkgs, ... }: + +{ + radio-active-aliases = ./aliases.nix; + radio-active-AppConfig = ./AppConfig.nix; + radio-active-AppConfig_all = ./AppConfig_all.nix; + radio-active-AppConfig_player_mpv = ./AppConfig_player_mpv.nix; +} +// lib.optionalAttrs (pkgs.stdenv.hostPlatform.isDarwin != true) { + radio-active-AppConfig_player_vlc = ./AppConfig_player_vlc.nix; +}