Skip to content

Commit

Permalink
Extract variant helper code
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Sep 8, 2024
1 parent 63441ef commit d895e0a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 66 deletions.
23 changes: 15 additions & 8 deletions codecs/mozjpeg/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,7 +73,13 @@
'';
dontFixup = true;
};
};
};

packageVariants = forAllVariants {inherit builder variants;};
in

mkInstallable {
packages = packageVariants // {default = packageVariants."mozjpeg-squoosh-base";};
}
);
}
56 changes: 17 additions & 39 deletions codecs/webp/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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
Expand All @@ -57,7 +55,7 @@
'';
};
"webp" = stdenv.mkDerivation {
name = "webp-${name}";
name = "webp-${variantName}";
src = webp-src;
nativeBuildInputs = [
pkgs.emscripten
Expand Down Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions nix/mk-installable/default.nix

This file was deleted.

36 changes: 36 additions & 0 deletions nix/squoosh-helpers/default.nix
Original file line number Diff line number Diff line change
@@ -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
);
}

0 comments on commit d895e0a

Please sign in to comment.