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

View File

@@ -14,9 +14,16 @@
flake-utils, flake-utils,
webp-src, webp-src,
}: }:
flake-utils.lib.eachDefaultSystem (
system:
let 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 = { base = {
simd = false; 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 = builder =
name: variantName:
{ simd }: { simd }:
{ {
"webp-squoosh" = buildSquooshCppCodec { "webp-squoosh" = buildSquooshCppCodec {
name = "webp-squoosh-${name}"; name = "webp-squoosh-${variantName}";
src = lib.sources.sourceByRegex ./. ["Makefile" "enc(/.+)?" "dec(/.+)?"]; src = lib.sources.sourceByRegex ./. ["Makefile" "enc(/.+)?" "dec(/.+)?"];
nativeBuildInputs = [ nativeBuildInputs = [
pkgs.emscripten 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; dontConfigure = true;
buildPhase = '' buildPhase = ''
export HOME=$TMPDIR export HOME=$TMPDIR
@@ -57,7 +55,7 @@
''; '';
}; };
"webp" = stdenv.mkDerivation { "webp" = stdenv.mkDerivation {
name = "webp-${name}"; name = "webp-${variantName}";
src = webp-src; src = webp-src;
nativeBuildInputs = [ nativeBuildInputs = [
pkgs.emscripten 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 { packageVariants = forAllVariants {
inherit builder; inherit builder variants;
variants = variantOptions;
}; };
defaultPackage = let defaultPackage = let
variants = lib.mapAttrs (name: opts: packageVariants."webp-squoosh-${name}") variantOptions; copyAllCodecs = lib.concatLines (lib.mapAttrsToList (name: _: "cp -r ${packageVariants."webp-squoosh-${name}"} $out/${name}") variants);
copyCommands = lib.concatLines (lib.mapAttrsToList (name: path: "cp -r ${path} $out/${name}") variants);
in in
stdenv.mkDerivation { stdenv.mkDerivation {
name = "all-variants"; name = "all-variants";
phases = ["buildPhase"]; phases = ["buildPhase"];
buildPhase = '' buildPhase = ''
mkdir -p $out; mkdir -p $out;
${copyCommands} ${copyAllCodecs}
''; '';
}; };
in 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
);
}