207 lines
6.1 KiB
Nix
207 lines
6.1 KiB
Nix
{ lib, pkgs }:
|
|
|
|
let
|
|
inherit (builtins)
|
|
isAttrs
|
|
isList
|
|
isString
|
|
hasAttr
|
|
getAttr
|
|
attrNames
|
|
toString
|
|
replaceStrings
|
|
;
|
|
|
|
mapAttrs = lib.mapAttrs;
|
|
recursiveUpdate = lib.recursiveUpdate;
|
|
|
|
# Deep-merge attrsets (right-biased).
|
|
deepMerge = a: b: recursiveUpdate a b;
|
|
|
|
# Merge component sources: base.sources overlaid by overrides (component-wise deep merge).
|
|
mergeSources =
|
|
baseSources: overrides:
|
|
baseSources
|
|
// mapAttrs (
|
|
name: ov: if hasAttr name baseSources then deepMerge (getAttr name baseSources) ov else ov
|
|
) overrides;
|
|
|
|
# Apply a single variant overlay (variables + sources).
|
|
applyVariantOnce =
|
|
selected: variant:
|
|
let
|
|
vVars = if variant ? variables then variant.variables else { };
|
|
vSrcs = if variant ? sources then variant.sources else { };
|
|
in
|
|
{
|
|
variables = selected.variables // vVars;
|
|
sources = mergeSources selected.sources vSrcs;
|
|
};
|
|
|
|
# Apply platform-specific overrides if present for the given system.
|
|
applyPlatforms =
|
|
selected: variant: system:
|
|
if system == null || !(variant ? platforms) || !(hasAttr system variant.platforms) then
|
|
selected
|
|
else
|
|
let
|
|
p = variant.platforms.${system};
|
|
pVars = if p ? variables then p.variables else { };
|
|
pSrcs = if p ? sources then p.sources else { };
|
|
in
|
|
{
|
|
variables = selected.variables // pVars;
|
|
sources = mergeSources selected.sources pSrcs;
|
|
};
|
|
|
|
# Resolve variant chain via inherits (ancestor first), then apply platforms.
|
|
resolveVariant =
|
|
spec: baseSelected: variantName: system:
|
|
if variantName == null || !(spec ? variants) || !(hasAttr variantName spec.variants) then
|
|
baseSelected
|
|
else
|
|
let
|
|
v = spec.variants.${variantName};
|
|
parentSelected =
|
|
if v ? inherits then resolveVariant spec baseSelected v.inherits system else baseSelected;
|
|
withVariant = applyVariantOnce parentSelected v;
|
|
in
|
|
applyPlatforms withVariant v system;
|
|
|
|
# Render ${var} substitutions in any string within attrs/lists.
|
|
renderValue =
|
|
value: vars:
|
|
if isString value then
|
|
let
|
|
keys = attrNames vars;
|
|
patterns = map (k: "\${" + k + "}") keys;
|
|
replacements = map (k: toString (getAttr k vars)) keys;
|
|
in
|
|
replaceStrings patterns replacements value
|
|
else if isAttrs value then
|
|
mapAttrs (_: v: renderValue v vars) value
|
|
else if isList value then
|
|
map (v: renderValue v vars) value
|
|
else
|
|
value;
|
|
|
|
# Decide fetcher for URL type based on optional extra.unpack hint.
|
|
useFetchZip = comp: comp ? extra && comp.extra ? unpack && comp.extra.unpack == "zip";
|
|
|
|
# Build a single src from a rendered component spec.
|
|
mkSrcFromRendered =
|
|
comp:
|
|
let
|
|
fetcher = if comp ? fetcher then comp.fetcher else "none";
|
|
in
|
|
if fetcher == "github" then
|
|
pkgs.fetchFromGitHub (
|
|
{
|
|
owner = comp.owner;
|
|
repo = comp.repo;
|
|
# Allow tag as rev (ignore null/empty tag)
|
|
rev = if comp ? tag && comp.tag != null && comp.tag != "" then comp.tag else comp.rev;
|
|
fetchSubmodules = if comp ? submodules then comp.submodules else false;
|
|
hash = comp.hash;
|
|
}
|
|
// lib.optionalAttrs (comp ? name) { name = comp.name; }
|
|
)
|
|
else if fetcher == "git" then
|
|
pkgs.fetchgit {
|
|
url = comp.url;
|
|
rev = comp.rev;
|
|
fetchSubmodules = if comp ? submodules then comp.submodules else false;
|
|
hash = comp.hash;
|
|
}
|
|
else if fetcher == "url" then
|
|
let
|
|
url = if comp ? url then comp.url else comp.urlTemplate;
|
|
in
|
|
if useFetchZip comp then
|
|
pkgs.fetchzip (
|
|
{
|
|
inherit url;
|
|
hash = comp.hash;
|
|
}
|
|
// lib.optionalAttrs (comp ? extra && comp.extra ? stripRoot) { stripRoot = comp.extra.stripRoot; }
|
|
)
|
|
else
|
|
pkgs.fetchurl {
|
|
inherit url;
|
|
hash = comp.hash;
|
|
}
|
|
else if fetcher == "pypi" then
|
|
pkgs.python3Packages.fetchPypi {
|
|
pname = comp.name;
|
|
version = comp.version;
|
|
hash = comp.hash;
|
|
}
|
|
else
|
|
# fetcher == "none": pass-through (e.g., linux version/hash consumed by custom logic)
|
|
comp;
|
|
|
|
in
|
|
rec {
|
|
/*
|
|
Select a variant from a loaded version.json specification.
|
|
|
|
Usage:
|
|
let selected = versioning.selectVariant spec variantName system;
|
|
- spec: attrset from lib.importJSON ./version.json
|
|
- variantName: string or null (when null, uses spec.defaultVariant if present)
|
|
- system: string like "x86_64-linux" or null (to apply platforms overrides)
|
|
*/
|
|
selectVariant =
|
|
spec: variantName: system:
|
|
let
|
|
chosen =
|
|
if variantName != null then
|
|
variantName
|
|
else
|
|
(if spec ? defaultVariant then spec.defaultVariant else null);
|
|
baseSelected = {
|
|
variables = if spec ? variables then spec.variables else { };
|
|
sources = if spec ? sources then spec.sources else { };
|
|
};
|
|
in
|
|
resolveVariant spec baseSelected chosen system;
|
|
|
|
/*
|
|
Render ${var} template substitutions across any value using provided variables.
|
|
Strings, attrsets, and lists are traversed.
|
|
*/
|
|
render = value: variables: renderValue value variables;
|
|
|
|
/*
|
|
Render a component with variables and then build its src (or pass-through for fetcher "none").
|
|
Prefer using mkAllSources, which handles rendering for all components.
|
|
*/
|
|
mkSrc =
|
|
comp: variables:
|
|
let
|
|
rendered = renderValue comp variables;
|
|
in
|
|
mkSrcFromRendered rendered;
|
|
|
|
/*
|
|
Produce an attrset of all sources for a selected spec:
|
|
mkAllSources selected
|
|
Where:
|
|
selected = selectVariant spec variantName system
|
|
Returns:
|
|
{ componentName = src | renderedComp (for "none"); ... }
|
|
*/
|
|
mkAllSources =
|
|
selected:
|
|
mapAttrs (
|
|
_name: comp:
|
|
if comp ? fetcher && comp.fetcher == "none" then
|
|
renderValue comp selected.variables
|
|
else
|
|
mkSrc (renderValue comp selected.variables) selected.variables
|
|
) selected.sources;
|
|
|
|
# Expose deepMerge for convenience (right-biased).
|
|
inherit deepMerge;
|
|
}
|