From fae59593923fad5dc72e089de0c3ea2f1845803c Mon Sep 17 00:00:00 2001 From: Surma Date: Fri, 6 Sep 2024 20:04:42 +0100 Subject: [PATCH] Minor refactor --- codecs/resize/flake.nix | 7 +-- codecs/rotate/flake.nix | 5 +- nix/squoosh-codec-builders/default.nix | 82 -------------------------- nix/squoosh-rust-builder/default.nix | 78 ++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 89 deletions(-) delete mode 100644 nix/squoosh-codec-builders/default.nix create mode 100644 nix/squoosh-rust-builder/default.nix diff --git a/codecs/resize/flake.nix b/codecs/resize/flake.nix index 6ebd3f48f..0901bb742 100644 --- a/codecs/resize/flake.nix +++ b/codecs/resize/flake.nix @@ -17,15 +17,14 @@ pkgs = nixpkgs.legacyPackages.${system}; inherit (pkgs) callPackage writeShellScriptBin; - squooshCodecBuilders = callPackage (import ../../nix/squoosh-codec-builders) {fenix = fenix.packages.${system};}; - inherit (squooshCodecBuilders) buildSquooshCodecRust; - + buildSquooshRustCodec= callPackage (import ../../nix/squoosh-rust-builder) {fenix = fenix.packages.${system};}; + src = ./.; in { packages = rec { default = resize-squoosh; - resize-squoosh = buildSquooshCodecRust { + resize-squoosh = buildSquooshRustCodec { name = "resize-squoosh"; inherit src; cargoLock = { diff --git a/codecs/rotate/flake.nix b/codecs/rotate/flake.nix index dd9fde4d7..bbc33a12d 100644 --- a/codecs/rotate/flake.nix +++ b/codecs/rotate/flake.nix @@ -18,15 +18,14 @@ pkgs = nixpkgs.legacyPackages.${system}; inherit (pkgs) callPackage writeShellScriptBin; - squooshCodecBuilders = callPackage (import ../../nix/squoosh-codec-builders) {fenix = fenix.packages.${system};}; - inherit (squooshCodecBuilders) buildSquooshCodecRust; + buildSquooshRustCodec= callPackage (import ../../nix/squoosh-rust-builder) {fenix = fenix.packages.${system};}; src = ./.; in { packages = rec { default = rotate-squoosh; - rotate-squoosh = buildSquooshCodecRust { + rotate-squoosh = buildSquooshRustCodec { name = "rotate-squoosh"; inherit src; cargoLock = { diff --git a/nix/squoosh-codec-builders/default.nix b/nix/squoosh-codec-builders/default.nix deleted file mode 100644 index 285856500..000000000 --- a/nix/squoosh-codec-builders/default.nix +++ /dev/null @@ -1,82 +0,0 @@ -{ - pkgs, - fenix, - wasm-bindgen ? pkgs.callPackage (import ../wasm-bindgen) { }, - rust-helpers ? pkgs.callPackage (import ../rust-helpers) { inherit fenix; }, - binaryen, - stdenv, -}: -let - inherit (rust-helpers) buildRustPackage; -in - -{ - buildSquooshCodecRust = - { - name, - src, - cargoLock ? { - lockFile = "${src}/Cargo.lock"; - }, - wasmBindgen ? { - sha256 = ""; - }, - ... - }@args: - let - codecBuild = buildRustPackage { - inherit src cargoLock; - name = "${name}-codec"; - target = "wasm32-unknown-unknown"; - }; - - wasm-bindgen-bin = wasm-bindgen.buildFromCargoLock { - inherit cargoLock; - sha256 = wasmBindgen.sha256; - }; - in - if wasmBindgen != null then - stdenv.mkDerivation ( - (removeAttrs args [ - "cargoLock" - "wasmBindgen" - ]) - // { - inherit codecBuild; - dontConfigure = true; - nativeBuildInputs = [ wasm-bindgen-bin ]; - buildPhase = '' - runHook preBuild - - wasm-bindgen --target web --out-dir $out $codecBuild/*.wasm - - runHook postBuild - ''; - dontInstall = true; - } - ) - else - stdenv.mkDerivation ( - (removeAttrs args [ - "cargoLock" - "wasmBindgen" - ]) - // { - inherit codecBuild; - dontConfigure = true; - nativeBuildInputs = [ binaryen ]; - buildPhase = '' - runHook preBuild - - wasm-opt -O3 --strip -o optimized.wasm $codecBuild/*.wasm - - runHook postBuild - ''; - installPhase = '' - mkdir -p $out - cp optimized.wasm $out - ''; - } - ); - -} diff --git a/nix/squoosh-rust-builder/default.nix b/nix/squoosh-rust-builder/default.nix new file mode 100644 index 000000000..bbff6d731 --- /dev/null +++ b/nix/squoosh-rust-builder/default.nix @@ -0,0 +1,78 @@ +{ + pkgs, + fenix, + wasm-bindgen ? pkgs.callPackage (import ../wasm-bindgen) { }, + rust-helpers ? pkgs.callPackage (import ../rust-helpers) { inherit fenix; }, + binaryen, + stdenv, +}: +let + inherit (rust-helpers) buildRustPackage; +in + +{ + name, + src, + cargoLock ? { + lockFile = "${src}/Cargo.lock"; + }, + wasmBindgen ? { + sha256 = ""; + }, + ... +}@args: +let + codecBuild = buildRustPackage { + inherit src cargoLock; + name = "${name}-codec"; + target = "wasm32-unknown-unknown"; + }; + + wasm-bindgen-bin = wasm-bindgen.buildFromCargoLock { + inherit cargoLock; + sha256 = wasmBindgen.sha256; + }; +in +if wasmBindgen != null then + stdenv.mkDerivation ( + (removeAttrs args [ + "cargoLock" + "wasmBindgen" + ]) + // { + inherit codecBuild; + dontConfigure = true; + nativeBuildInputs = [ wasm-bindgen-bin ]; + buildPhase = '' + runHook preBuild + + wasm-bindgen --target web --out-dir $out $codecBuild/*.wasm + + runHook postBuild + ''; + dontInstall = true; + } + ) +else + stdenv.mkDerivation ( + (removeAttrs args [ + "cargoLock" + "wasmBindgen" + ]) + // { + inherit codecBuild; + dontConfigure = true; + nativeBuildInputs = [ binaryen ]; + buildPhase = '' + runHook preBuild + + wasm-opt -O3 --strip -o optimized.wasm $codecBuild/*.wasm + + runHook postBuild + ''; + installPhase = '' + mkdir -p $out + cp optimized.wasm $out + ''; + } + )