Extract variant helper code

This commit is contained in:
Surma
2024-09-08 13:18:15 +01:00
parent 63441ef371
commit d895e0a6b6
4 changed files with 68 additions and 66 deletions

View File

@@ -21,17 +21,18 @@
inherit (pkgs) callPackage stdenv lib;
buildSquooshCppCodec = callPackage (import ../../nix/squoosh-cxx-builder) {};
mkInstallable = callPackage (import ../../nix/mk-installable) {};
in
mkInstallable {
packages = rec {
squooshHelpers = callPackage (import ../../nix/squoosh-helpers) {};
inherit (squooshHelpers) mkInstallable forAllVariants;
default = mozjpeg-squoosh;
variants = {
base = {};
};
builder = variantName: opts: {
mozjpeg-squoosh = buildSquooshCppCodec {
name = "mozjpeg-squoosh";
src = lib.sources.sourceByRegex ./. ["Makefile" "enc(/.+)?"];
MOZJPEG = mozjpeg;
MOZJPEG = self.packages.${system}."mozjpeg-${variantName}";
dontConfigure = true;
decoder = null;
@@ -72,7 +73,13 @@
'';
dontFixup = true;
};
};
};
packageVariants = forAllVariants {inherit builder variants;};
in
mkInstallable {
packages = packageVariants // {default = packageVariants."mozjpeg-squoosh-base";};
}
);
}

View File

@@ -14,9 +14,16 @@
flake-utils,
webp-src,
}:
let
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib stdenv callPackage;
buildSquooshCppCodec = callPackage (import ../../nix/squoosh-cxx-builder) {};
squooshHelpers = callPackage (import ../../nix/squoosh-helpers) {};
inherit (squooshHelpers) mkInstallable forAllVariants;
variantOptions = {
variants = {
base = {
simd = false;
};
@@ -25,27 +32,18 @@
};
};
in
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib stdenv callPackage;
buildSquooshCppCodec = callPackage (import ../../nix/squoosh-cxx-builder) {};
mkInstallable = callPackage (import ../../nix/mk-installable) {};
builder =
name:
variantName:
{ simd }:
{
"webp-squoosh" = buildSquooshCppCodec {
name = "webp-squoosh-${name}";
name = "webp-squoosh-${variantName}";
src = lib.sources.sourceByRegex ./. ["Makefile" "enc(/.+)?" "dec(/.+)?"];
nativeBuildInputs = [
pkgs.emscripten
self.packages.${system}."webp-${name}"
self.packages.${system}."webp-${variantName}"
];
WEBP = self.packages.${system}."webp-${name}";
WEBP = self.packages.${system}."webp-${variantName}";
dontConfigure = true;
buildPhase = ''
export HOME=$TMPDIR
@@ -57,7 +55,7 @@
'';
};
"webp" = stdenv.mkDerivation {
name = "webp-${name}";
name = "webp-${variantName}";
src = webp-src;
nativeBuildInputs = [
pkgs.emscripten
@@ -97,39 +95,19 @@
};
};
suffixAttrNames = suffix: attrs:
lib.mapAttrs'
(name: val:
lib.nameValuePair
"${name}${suffix}"
val)
attrs;
forAllVariants =
{builder, variants}:
lib.lists.foldl
(acc: v: acc//v)
{}
(lib.mapAttrsToList
(variantName: value:
suffixAttrNames "-${variantName}" (builder variantName value))
variants
);
packageVariants = forAllVariants {
inherit builder;
variants = variantOptions;
inherit builder variants;
};
defaultPackage = let
variants = lib.mapAttrs (name: opts: packageVariants."webp-squoosh-${name}") variantOptions;
copyCommands = lib.concatLines (lib.mapAttrsToList (name: path: "cp -r ${path} $out/${name}") variants);
copyAllCodecs = lib.concatLines (lib.mapAttrsToList (name: _: "cp -r ${packageVariants."webp-squoosh-${name}"} $out/${name}") variants);
in
stdenv.mkDerivation {
name = "all-variants";
phases = ["buildPhase"];
buildPhase = ''
mkdir -p $out;
${copyCommands}
${copyAllCodecs}
'';
};
in

View File

@@ -1,19 +0,0 @@
{
coreutils,
rsync,
writeShellScriptBin,
lib,
}:
flake:
let
installScript = writeShellScriptBin "install.sh" ''
${coreutils}/bin/mkdir -p wasm_build
${rsync}/bin/rsync --chmod=u+w -r ${flake.packages.default}/* wasm_build/
'';
in
lib.recursiveUpdate flake {
apps.install = {
type = "app";
program = "${installScript}/bin/install.sh";
};
}

View File

@@ -0,0 +1,36 @@
{
coreutils,
rsync,
writeShellScriptBin,
lib,
}:
let
suffixAttrNames =
suffix: attrs: lib.mapAttrs' (name: val: lib.nameValuePair "${name}${suffix}" val) attrs;
in
{
inherit suffixAttrNames;
mkInstallable =
flake:
let
installScript = writeShellScriptBin "install.sh" ''
${coreutils}/bin/mkdir -p wasm_build
${rsync}/bin/rsync --chmod=u+w -r ${flake.packages.default}/* wasm_build/
'';
in
lib.recursiveUpdate flake {
apps.install = {
type = "app";
program = "${installScript}/bin/install.sh";
};
};
forAllVariants =
{ builder, variants }:
lib.lists.foldl (acc: v: acc // v) { } (
lib.mapAttrsToList (
variantName: value: suffixAttrNames "-${variantName}" (builder variantName value)
) variants
);
}