diff --git a/codecs/mozjpeg/flake.nix b/codecs/mozjpeg/flake.nix index b0a9849e..91f0fb21 100644 --- a/codecs/mozjpeg/flake.nix +++ b/codecs/mozjpeg/flake.nix @@ -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";}; } ); } diff --git a/codecs/webp/flake.nix b/codecs/webp/flake.nix index 6f5866a8..e9f19651 100644 --- a/codecs/webp/flake.nix +++ b/codecs/webp/flake.nix @@ -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 diff --git a/nix/mk-installable/default.nix b/nix/mk-installable/default.nix deleted file mode 100644 index e032bd2d..00000000 --- a/nix/mk-installable/default.nix +++ /dev/null @@ -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"; - }; -} diff --git a/nix/squoosh-helpers/default.nix b/nix/squoosh-helpers/default.nix new file mode 100644 index 00000000..a36a2121 --- /dev/null +++ b/nix/squoosh-helpers/default.nix @@ -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 + ); +}