retronix/modules/es-config.nix

185 lines
4.5 KiB
Nix
Raw Permalink Normal View History

2023-12-30 18:38:14 +01:00
{ lib, config, pkgs, ... }:
let
t = v: builtins.trace v v;
filterAttrs' = l: lib.filterAttrs (v: _: builtins.elem v l);
cfg = config.retronix.emulationstation;
attrToXml = attrs: lib.concatLines (lib.mapAttrsToList (k: v: "<${k}>${builtins.toString v}</${k}>") attrs);
2024-01-06 22:42:36 +01:00
gameTemplate = game: let
2023-12-30 18:38:14 +01:00
attr = [
"path"
"name"
2024-01-06 22:42:36 +01:00
"sortname"
2023-12-30 18:38:14 +01:00
"desc"
"image"
"video"
2024-01-06 22:42:36 +01:00
"marquee"
"thumbnail"
2023-12-30 18:38:14 +01:00
"rating"
"releasedate"
"developer"
"publisher"
"genre"
"players"
2024-01-06 22:42:36 +01:00
"favorite"
"hidden"
"kidgame"
"playcount"
"lastplayed"
2023-12-30 18:38:14 +01:00
];
2024-01-06 22:42:36 +01:00
args = game.meta // { path = "@out@/${game.name}.sh"; };
2023-12-30 18:38:14 +01:00
in ''
<game>
${attrToXml (filterAttrs' attr args)}
</game>
'';
systemTemplate = args: let
attr = [
"name"
"fullname"
"path"
"extension"
"command"
"platform"
"theme"
];
in ''
<system>
${attrToXml (filterAttrs' attr args)}
</system>
'';
mkSystemPath = name: games: let
gamelist = ''
<?xml version="1.0"?>
<gameList>
${lib.concatLines (builtins.map gameTemplate games)}
</gameList>
'';
symlinkCommands = builtins.map (
2024-01-06 22:42:36 +01:00
v: "ln -s ${v}/${v.launchPath} $out/${v.name}.sh"
2023-12-30 18:38:14 +01:00
) games;
2024-01-17 13:37:12 +01:00
2023-12-30 18:38:14 +01:00
in pkgs.runCommand "${name}-roms" {
inherit gamelist symlinkCommands;
passAsFile = [
"gamelist"
];
} ''
mkdir -p $out
cp $gamelistPath $out/gamelist.xml
2024-01-06 22:42:36 +01:00
substituteAllInPlace $out/gamelist.xml
2023-12-30 18:38:14 +01:00
${lib.concatLines symlinkCommands}
'';
sectionOptions = lib.types.submodule ({config, name, ...}: {
freeformType = with lib.types; attrsOf str;
options = {
name = lib.mkOption {
type = lib.types.str;
default = name;
};
fullname = lib.mkOption {
type = lib.types.str;
default = name;
};
2024-01-06 22:42:36 +01:00
path = lib.mkOption {
2023-12-30 18:38:14 +01:00
type = lib.types.path;
};
command = lib.mkOption {
type = lib.types.str;
2024-01-06 22:42:36 +01:00
default = "%ROM%";
2023-12-30 18:38:14 +01:00
};
extension = lib.mkOption {
type = lib.types.str;
};
games = lib.mkOption {
2024-01-06 22:42:36 +01:00
type = lib.types.listOf lib.types.package;
2023-12-30 18:38:14 +01:00
description = "Takes attributes of `games` tag in gamelist.xml";
};
};
config = {
path = mkSystemPath config.name config.games;
};
});
in {
options = {
2024-01-06 22:42:36 +01:00
retronix = {
emulationstation = {
systemCfgFile = lib.mkOption {
type = lib.types.path;
};
inputCfgFile = lib.mkOption {
type = lib.types.path;
};
2024-01-17 13:37:05 +01:00
settingsCfgFile = lib.mkOption {
type = lib.types.path;
};
2024-01-06 22:42:36 +01:00
themesDir = lib.mkOption {
type = lib.types.path;
};
2024-01-17 13:37:12 +01:00
themes = lib.mkOption {
type = lib.types.attrsOf lib.types.path;
default = {};
};
2024-01-06 22:42:36 +01:00
inputCfg = lib.mkOption {
type = lib.types.str;
default = "";
};
homeDir = lib.mkOption {
internal = true;
type = lib.types.path;
};
extraConfigFiles = lib.mkOption {
type = with lib.types; listOf path;
default = [];
description = ''
Must be store paths.
Directory structure will be merged.
'';
};
cli = lib.mkOption {
type = lib.types.package;
};
};
2023-12-30 18:38:14 +01:00
systems = lib.mkOption {
type = lib.types.attrsOf (sectionOptions);
default = {};
};
};
};
config = {
retronix.emulationstation = {
2024-01-06 22:42:36 +01:00
systemCfgFile = pkgs.writeTextDir "es_systems.cfg" ''
2023-12-30 18:38:14 +01:00
<?xml version="1.0"?>
<systemList>
2024-01-06 22:42:36 +01:00
${lib.concatLines (lib.mapAttrsToList (_: systemTemplate) config.retronix.systems)}
2023-12-30 18:38:14 +01:00
</systemList>
'';
2024-01-06 22:42:36 +01:00
inputCfgFile = lib.mkDefault (pkgs.writeTextDir "es_input.cfg" cfg.inputCfg);
2024-01-17 13:37:12 +01:00
themesDir = let
copyCommands = lib.concatLines (lib.mapAttrsToList (k: v: "cp -r ${v} $out/themes/${k}") cfg.themes);
in pkgs.runCommand "es-themes" {} ''
mkdir -p $out/themes
${copyCommands}
'';
2024-01-06 22:42:36 +01:00
homeDir = pkgs.symlinkJoinSubdir {
2023-12-30 18:38:14 +01:00
name = "es-config-dir";
2024-01-06 22:42:36 +01:00
subdir = ".emulationstation";
2023-12-30 18:38:14 +01:00
paths = [
cfg.systemCfgFile
cfg.inputCfgFile
2024-01-17 13:37:05 +01:00
cfg.settingsCfgFile
2024-01-17 13:37:12 +01:00
cfg.themesDir
2023-12-30 18:38:14 +01:00
] ++ cfg.extraConfigFiles;
};
cli = pkgs.writeShellScript "emulationstation" "${pkgs.emulationstationPatched}/bin/emulationstation --home ${cfg.homeDir} --gamelist-only";
2023-12-30 18:38:14 +01:00
};
};
}