|
| 1 | +/** |
| 2 | + WARN: must be applied after PR https://github.com/NixOS/nixpkgs/pull/441029 |
| 3 | +*/ |
| 4 | +{ |
| 5 | + config, |
| 6 | + lib, |
| 7 | + # pkgs, |
| 8 | + ... |
| 9 | +}: |
| 10 | +let |
| 11 | + pkgs = import /home/s0ands0/git/hub/NixOS/nixpkgs/wt/radio-active { }; |
| 12 | + |
| 13 | + inherit (lib) |
| 14 | + mapAttrsToList |
| 15 | + mkEnableOption |
| 16 | + mkIf |
| 17 | + mkOption |
| 18 | + mkPackageOption |
| 19 | + ; |
| 20 | + |
| 21 | + inherit (lib.attrsets) |
| 22 | + attrByPath |
| 23 | + ; |
| 24 | + |
| 25 | + inherit (lib.types) |
| 26 | + nonEmptyStr |
| 27 | + submodule |
| 28 | + attrsOf |
| 29 | + oneOf |
| 30 | + ; |
| 31 | + |
| 32 | + inherit (lib.types.numbers) |
| 33 | + nonnegative |
| 34 | + ; |
| 35 | + |
| 36 | + inherit (pkgs.formats) |
| 37 | + ini |
| 38 | + ; |
| 39 | + |
| 40 | + iniFormat = ini { }; |
| 41 | + |
| 42 | + cfg = config.programs.radio-active; |
| 43 | +in |
| 44 | +{ |
| 45 | + meta.maintainers = [ |
| 46 | + # lib.maintainers.S0AndS0 |
| 47 | + ## TODO: Trade above for below when `node.nixpkgs.locked.rev` is at or beyond |
| 48 | + ## 4d48a4e93b9ffbd291b2d9ca3315848e27eed800 |
| 49 | + { |
| 50 | + name = "S0AndS0"; |
| 51 | + |
| 52 | + github = "S0AndS0"; |
| 53 | + githubId = 4116150; |
| 54 | + matrix = "@s0ands0:matrix.org"; |
| 55 | + } |
| 56 | + ]; |
| 57 | + |
| 58 | + options.programs.radio-active = { |
| 59 | + enable = mkEnableOption "Enable installing radio-active and writing configuration file"; |
| 60 | + |
| 61 | + package = mkPackageOption pkgs "radio-active" { |
| 62 | + nullable = true; |
| 63 | + }; |
| 64 | + |
| 65 | + settings = mkOption { |
| 66 | + default = { }; |
| 67 | + |
| 68 | + type = submodule { |
| 69 | + options = { |
| 70 | + /** |
| 71 | + Requires following PR to be merged; |
| 72 | + https://github.com/NixOS/nixpkgs/pull/441029 |
| 73 | +
|
| 74 | + Requires following PR to be merged; |
| 75 | + https://github.com/nix-community/home-manager/pull/7793 |
| 76 | + */ |
| 77 | + AppConfig = mkOption { |
| 78 | + default = { }; |
| 79 | + |
| 80 | + description = '' |
| 81 | + Declare-able configurations for radio-active |
| 82 | + ''; |
| 83 | + |
| 84 | + example = '' |
| 85 | + ## Input |
| 86 | +
|
| 87 | + ```nix |
| 88 | + programs.radio-active.settings.AppConfig = { |
| 89 | + loglevel = "debug"; |
| 90 | + limit = 41; |
| 91 | + sort = "votes"; |
| 92 | + filter = "none"; |
| 93 | + volume = 68; |
| 94 | + filepath = "/home/{user}/recordings/radioactive/"; |
| 95 | + filetype = "mp3"; |
| 96 | + player = "ffplay"; |
| 97 | + }; |
| 98 | + ``` |
| 99 | +
|
| 100 | + ## Output |
| 101 | +
|
| 102 | + ``` |
| 103 | + [AppConfig] |
| 104 | + loglevel = debug |
| 105 | + limit = 41 |
| 106 | + sort = votes |
| 107 | + filter = none |
| 108 | + volume = 68 |
| 109 | + filepath = /home/{user}/recordings/radioactive/ |
| 110 | + filetype = mp3 |
| 111 | + player = ffplay |
| 112 | + ``` |
| 113 | + ''; |
| 114 | + |
| 115 | + type = attrsOf (oneOf [nonEmptyStr nonnegative]); |
| 116 | + }; |
| 117 | + |
| 118 | + aliases = mkOption { |
| 119 | + description = '' |
| 120 | + Key/value pairs where the key is name of radio station and value is URL |
| 121 | + ''; |
| 122 | + |
| 123 | + example = '' |
| 124 | + ## Input |
| 125 | +
|
| 126 | + ```nix |
| 127 | + programs.radio-active.aliases = { |
| 128 | + "Deep House Lounge" = "http://198.15.94.34:8006/stream"; |
| 129 | + }; |
| 130 | + ``` |
| 131 | +
|
| 132 | + ## Output |
| 133 | +
|
| 134 | + ``` |
| 135 | + Deep House Lounge==http://198.15.94.34:8006/stream |
| 136 | + ``` |
| 137 | + ''; |
| 138 | + |
| 139 | + default = { }; |
| 140 | + type = attrsOf nonEmptyStr; |
| 141 | + }; |
| 142 | + }; |
| 143 | + }; |
| 144 | + }; |
| 145 | + }; |
| 146 | + |
| 147 | + config = mkIf cfg.enable { |
| 148 | + ## TODO: test that dependency `postPatch` modifications works at runtime |
| 149 | + home.packages = |
| 150 | + let |
| 151 | + player = attrByPath ["settings" "AppConfig" "player"] "ffplay" cfg; |
| 152 | + |
| 153 | + radio-active = |
| 154 | + if player == "ffplay" then |
| 155 | + pkgs.radio-active |
| 156 | + else if player == "mpv" then |
| 157 | + pkgs.radio-active.overrideAttrs ( |
| 158 | + finalAttrs: previousAttrs: { |
| 159 | + postPatch = '' |
| 160 | + ${previousAttrs.postPatch} |
| 161 | +
|
| 162 | + substituteInPlace radioactive/mpv.py \ |
| 163 | + --replace-fail 'self.exe_path = which(self.program_name)' \ |
| 164 | + 'self.exe_path = "${lib.getExe pkgs.mpv}"' |
| 165 | + ''; |
| 166 | + } |
| 167 | + ) |
| 168 | + else if player == "vlc" then |
| 169 | + pkgs.radio-active.overrideAttrs ( |
| 170 | + finalAttrs: previousAttrs: { |
| 171 | + postPatch = '' |
| 172 | + ${previousAttrs.postPatch} |
| 173 | +
|
| 174 | + substituteInPlace radioactive/vlc.py \ |
| 175 | + --replace-fail 'self.exe_path = which(self.program_name)' \ |
| 176 | + 'self.exe_path = "${lib.getExe pkgs.vlc}"' |
| 177 | + ''; |
| 178 | + } |
| 179 | + ) |
| 180 | + else |
| 181 | + lib.warn '' |
| 182 | + Unknown player defined in `config.programs.radio-active.AppConfig.player` |
| 183 | + '' pkgs.radio-active; |
| 184 | + in |
| 185 | + mkIf (cfg.package != null) [ |
| 186 | + radio-active |
| 187 | + ]; |
| 188 | + |
| 189 | + xdg.configFile."radio-active/configs.ini" = |
| 190 | + lib.mkIf (cfg.settings != { } && cfg.settings.AppConfig != { }) |
| 191 | + { |
| 192 | + source = iniFormat.generate "radio-active-config" { |
| 193 | + inherit (cfg.settings) AppConfig; |
| 194 | + }; |
| 195 | + }; |
| 196 | + |
| 197 | + home.file."radio-active-alias" = mkIf (cfg.settings != { } && cfg.settings.aliases != { }) { |
| 198 | + text = '' |
| 199 | + ${builtins.concatStringsSep "\n" ( |
| 200 | + mapAttrsToList (name: value: "${name}==${value}") cfg.settings.aliases |
| 201 | + )} |
| 202 | + ''; |
| 203 | + target = "${config.home.homeDirectory}/.radio-active-alias"; |
| 204 | + }; |
| 205 | + }; |
| 206 | +} |
0 commit comments