From 24e63eafc646ec13e4ab6beba779eaec99d6d2f5 Mon Sep 17 00:00:00 2001 From: Surma Date: Sat, 7 Sep 2024 14:15:49 +0100 Subject: [PATCH] Improve cpp nix builder --- codecs/mozjpeg/flake.nix | 44 ++++++++--------------------- codecs/rotate/flake.nix | 2 +- nix/squoosh-cxx-builder/default.nix | 31 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 nix/squoosh-cxx-builder/default.nix diff --git a/codecs/mozjpeg/flake.nix b/codecs/mozjpeg/flake.nix index 5b771c622..984d9d924 100644 --- a/codecs/mozjpeg/flake.nix +++ b/codecs/mozjpeg/flake.nix @@ -18,35 +18,24 @@ system: let pkgs = nixpkgs.legacyPackages.${system}; - inherit (pkgs) stdenv runCommand writeShellScriptBin; + inherit (pkgs) callPackage stdenv; + + buildSquooshCppCodec = callPackage (import ../../nix/squoosh-cxx-builder) {}; + mkInstallable = callPackage (import ../../nix/mk-installable) {}; + in - rec { + mkInstallable { packages = rec { + default = mozjpeg-squoosh; - mozjpeg-squoosh = stdenv.mkDerivation { + mozjpeg-squoosh = buildSquooshCppCodec { name = "mozjpeg-squoosh"; - # Only copy files that are actually relevant to avoid unnecessary - # cache invalidations. - src = runCommand "src" { } '' - mkdir $out - cp -r ${./.}/enc $out/ - cp ${./.}/Makefile $out/ - ''; - nativeBuildInputs = [ - pkgs.emscripten - pkgs.mozjpeg - ]; + src = ./.; MOZJPEG = mozjpeg; dontConfigure = true; - buildPhase = '' - export HOME=$TMPDIR - emmake make -j$(nproc) - ''; - installPhase = '' - mkdir -p $out - cp -r enc $out - ''; + decoder = null; }; + mozjpeg = stdenv.mkDerivation { name = "mozjpeg"; src = mozjpeg-src; @@ -82,17 +71,6 @@ ''; dontFixup = true; }; - installScript = writeShellScriptBin "install.sh" '' - ${pkgs.coreutils}/bin/rm -rf wasm_build - ${pkgs.coreutils}/bin/mkdir -p wasm_build - ${pkgs.rsync}/bin/rsync --chmod=u+w -r ${mozjpeg-squoosh}/* wasm_build/ - ''; - }; - apps = { - install = { - type = "app"; - program = "${packages.installScript}/bin/install.sh"; - }; }; } ); diff --git a/codecs/rotate/flake.nix b/codecs/rotate/flake.nix index 4a56a6ede..e5e47fac0 100644 --- a/codecs/rotate/flake.nix +++ b/codecs/rotate/flake.nix @@ -16,7 +16,7 @@ system: let pkgs = nixpkgs.legacyPackages.${system}; - inherit (pkgs) callPackage writeShellScriptBin; + inherit (pkgs) callPackage; buildSquooshRustCodec= callPackage (import ../../nix/squoosh-rust-builder) {fenix = fenix.packages.${system};}; mkInstallable = callPackage (import ../../nix/mk-installable) {}; diff --git a/nix/squoosh-cxx-builder/default.nix b/nix/squoosh-cxx-builder/default.nix new file mode 100644 index 000000000..94a36b202 --- /dev/null +++ b/nix/squoosh-cxx-builder/default.nix @@ -0,0 +1,31 @@ +{ + pkgs, + stdenv, + runCommand, +}: +{ + name, + src, + nativeBuildInputs ? [ ], + encoder ? "enc", + decoder ? "dec", + ... +}@args: + +stdenv.mkDerivation ( + final: + args + // { + inherit name src; + nativeBuildInputs = [ pkgs.emscripten ] ++ nativeBuildInputs; + buildPhase = '' + export HOME=$TMPDIR + emmake make -j$(nproc) + ''; + installPhase = '' + mkdir -p $out + ${if (encoder != null) then "cp -r ${encoder} $out" else ""} + ${if (decoder != null) then "cp -r ${decoder} $out" else ""} + ''; + } +)