nte/engine.nix

100 lines
2.8 KiB
Nix
Raw Normal View History

2024-05-25 15:50:10 +02:00
pkgs: src: {extraArgs, entries, templates}: let
2024-05-22 21:19:37 +02:00
inherit (pkgs) lib;
2024-05-25 15:50:10 +02:00
inherit (builtins) abort dirOf toString;
inherit (lib.attrsets) hasAttr;
inherit (lib.lists) forEach findFirst;
2024-05-25 15:50:10 +02:00
inherit (lib.path) removePrefix;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg hasSuffix isString removeSuffix;
2024-05-22 21:19:37 +02:00
inherit (lib.trivial) functionArgs;
args = {inherit pkgs;}
// (import ./stdlib.nix pkgs)
// extraArgs;
2024-05-25 15:50:10 +02:00
isBaseTemplate = template:
isString template.output;
2024-05-22 21:19:37 +02:00
2024-05-25 15:50:10 +02:00
findTemplateFile = entry: let
_template = findFirst (templateFile: let
2024-05-22 21:19:37 +02:00
templateFn = import templateFile;
template = templateFn (functionArgs templateFn);
in
template.name == entry.template)
null
templates;
2024-05-25 15:50:10 +02:00
in
if _template == null then
abort "unknown template `${entry.template}`"
else
_template;
2024-05-22 21:19:37 +02:00
applyTemplate = entry: templateFile: let
templateFn = import templateFile;
template = templateFn (args // entry);
in
2024-05-25 15:50:10 +02:00
if isBaseTemplate template then {
2024-05-22 21:19:37 +02:00
inherit (entry) file;
inherit (template) output;
}
else let
2024-05-25 15:50:10 +02:00
newEntry = template.output // {inherit (entry) file;};
2024-05-22 21:19:37 +02:00
foundTemplateFile = findTemplateFile newEntry;
in
applyTemplate newEntry foundTemplateFile;
processEntryFile = entryFile: let
2024-05-25 15:50:10 +02:00
entry = getEntry entryFile;
2024-05-22 21:19:37 +02:00
foundTemplateFile = findTemplateFile entry;
in
2024-05-25 15:50:10 +02:00
applyTemplate entry foundTemplateFile;
replaceSuffix = from: to: string:
if !(hasSuffix from string) then
abort "invalid suffix `${from}` for string `${string}`"
2024-05-22 21:19:37 +02:00
else
2024-05-25 15:50:10 +02:00
concatStrings [ (removeSuffix from string) to ];
getTemplateFormat = entry: templateFile: let
templateFn = import templateFile;
# getEntry needs to go down to the base template for the format
# but any template through the way can ask for a file
# so we just give it a placeholder - an empty string here
# since the output of this thing doesn't matter
template = templateFn (args // entry // { file = ""; });
in
if isBaseTemplate template then
template.format
else let
newEntry = template.output;
foundTemplateFile = findTemplateFile newEntry;
in
getTemplateFormat newEntry foundTemplateFile;
getEntry = entryFile: let
sourceFile = toString (removePrefix src entryFile);
entry = (import entryFile) args;
foundTemplateFile = findTemplateFile entry;
entryFormat = getTemplateFormat entry foundTemplateFile;
in
if !(hasAttr "file" entry) then
entry // {
file = replaceSuffix ".nix" ".${entryFormat}" sourceFile;
}
else
entry;
in {
inherit getEntry;
buildScript = /*sh*/''
${concatMapStrings
(result: /*sh*/''
mkdir -p $out/${dirOf result.file}
echo ${escapeShellArg result.output} > $out/${result.file}
'')
(forEach entries processEntryFile)
}
'';
}