diff --git a/codecs/webp/flake.nix b/codecs/webp/flake.nix index 89ba386ba..6d4c2aebd 100644 --- a/codecs/webp/flake.nix +++ b/codecs/webp/flake.nix @@ -15,7 +15,7 @@ webp-src, }: let - optionSets = { + packageVariants = { base = { simd = false; }; @@ -28,10 +28,10 @@ system: let pkgs = nixpkgs.legacyPackages.${system}; - packageBuilder = - with pkgs; + inherit (pkgs) lib stdenv runCommand emscripten writeShellScriptBin cmake; + packageVariantBuilder = name: - { simd }: + { simd }@variantOptions: { "webp-squoosh-${name}" = stdenv.mkDerivation { name = "webp-squoosh-${name}"; @@ -105,16 +105,18 @@ ''; }; - forEachOption = pkgs.callPackage (import ../../nix/for-each-option.nix) { }; - packageVariants = forEachOption packageBuilder optionSets; + packages = lib.foldl (acc: v: acc//v) {} (lib.mapAttrsToList packageVariantBuilder packageVariants); + + joinLines = lines: lib.foldl (text: line: text + line) "" lines; + + globalInstallerCode = joinLines (lib.lists.map (variantName: '' + ${self.packages.${system}."install-${variantName}"}/bin/install.sh + '') (lib.attrNames packageVariants)); in - with pkgs; + { - packages = packageVariants // { - installScript = writeShellScriptBin "install.sh" '' - ${self.packages.${system}.install-base}/bin/install.sh - ${self.packages.${system}.install-simd}/bin/install.sh - ''; + packages = packages // { + installScript = writeShellScriptBin "install.sh" globalInstallerCode; }; apps = { install = { @@ -123,5 +125,6 @@ }; }; } + ); } diff --git a/nix/rust-helpers/default.nix b/nix/rust-helpers/default.nix new file mode 100644 index 000000000..1fb69df53 --- /dev/null +++ b/nix/rust-helpers/default.nix @@ -0,0 +1,67 @@ +{ + rustPlatform, + jq, + rsync, + stdenv, + fenix, +}: +{ + buildRustPackage = + { + target, + src, + cargoLock ? { + lockFile = "${src}/Cargo.lock"; + }, + release ? true, + ... + }@args: + let + # Setup a toolchain for the the host system targeting `target`. + toolchain = + let + inherit (fenix) stable targets combine; + in + combine [ + stable.rustc + stable.cargo + targets.${target}.stable.rust-std + ]; + + # Create `vendor` folder with all dependencies. + vendoredDependencies = rustPlatform.importCargoLock cargoLock; + + rustcTargetDir = "target/${target}/${if release then "release" else "debug"}"; + in + stdenv.mkDerivation ( + (removeAttrs args [ "cargoLock" ]) + // { + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ + toolchain + jq + rsync + ]; + dontConfigure = true; + buildPhase = '' + runHook preBuild + + cargo build \ + --config 'source.crates-io.replace-with="vendored-sources"' \ + --config 'source.vendored-sources.directory="${vendoredDependencies}"' \ + --offline \ + --target ${target} ${if release then "-r" else ""} + + runHook postBuild + ''; + installPhase = '' + runHook preInstall; + mkdir -p $out + + find ${rustcTargetDir} -type f -maxdepth 1 | \ + xargs -I ___X -n1 cp ___X $out + + runHook postInstall; + ''; + } + ); +} diff --git a/nix/rust-helpers/flake.nix b/nix/rust-helpers/flake.nix deleted file mode 100644 index bcb8474d6..000000000 --- a/nix/rust-helpers/flake.nix +++ /dev/null @@ -1,74 +0,0 @@ -{ - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/24.05"; - fenix.url = "github:nix-community/fenix"; - }; - outputs = - { - self, - nixpkgs, - fenix, - }: - { - lib = { - buildRustPackage = - { - system, - target, - src, - cargoLock ? { - lockFile = "${src}/Cargo.lock"; - }, - release ? true, - ... - }@args: - with nixpkgs.legacyPackages.${system}; - let - # Setup a toolchain for the the host system targeting `target`. - toolchain = - with fenix.packages.${system}; - combine [ - stable.rustc - stable.cargo - targets.${target}.stable.rust-std - ]; - - # Create `vendor` folder with all dependencies. - vendoredDependencies = rustPlatform.importCargoLock cargoLock; - - rustcTargetDir = "target/${target}/${if release then "release" else "debug"}"; - in - stdenv.mkDerivation ( - (removeAttrs args [ "cargoLock" ]) - // { - nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ - toolchain - jq - rsync - ]; - dontConfigure = true; - buildPhase = '' - runHook preBuild - - cargo build \ - --config 'source.crates-io.replace-with="vendored-sources"' \ - --config 'source.vendored-sources.directory="${vendoredDependencies}"' \ - --offline \ - --target ${target} ${if release then "-r" else ""} - - runHook postBuild - ''; - installPhase = '' - runHook preInstall; - mkdir -p $out - - find ${rustcTargetDir} -type f -maxdepth 1 | \ - xargs -I ___X -n1 cp ___X $out - - runHook postInstall; - ''; - } - ); - }; - }; -} diff --git a/nix/squoosh-codec-builders/default.nix b/nix/squoosh-codec-builders/default.nix new file mode 100644 index 000000000..7e63db73f --- /dev/null +++ b/nix/squoosh-codec-builders/default.nix @@ -0,0 +1,51 @@ +{ + fenix, + wasm-bindgen, + rust-helpers, + stdenv, +}: +{ + buildSquooshCodecRust = + { + name, + src, + cargoLock ? { + lockFile = "${src}/Cargo.lock"; + }, + wasmBindgen ? { + sha256 = ""; + }, + ... + }@args: + let + codecBuild = rust-helpers.lib.buildRustPackage { + inherit src cargoLock; + name = "${name}-codec"; + target = "wasm32-unknown-unknown"; + }; + + wasm-bindgen-bin = wasm-bindgen.lib.buildFromCargoLock { + inherit cargoLock; + sha256 = wasmBindgen.sha256; + }; + in + if wasmBindgen != null then + stdenv.mkDerivation ( + (removeAttrs args [ "cargoLock" ]) + // { + inherit codecBuild; + dontConfigure = true; + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ wasm-bindgen-bin ]; + buildPhase = '' + runHook preBuild + + wasm-bindgen --target web --out-dir $out $codecBuild/*.wasm + + runHook postBuild + ''; + dontInstall = true; + } + ) + else + codecBuild; +} diff --git a/nix/squoosh-codec-builders/flake.nix b/nix/squoosh-codec-builders/flake.nix deleted file mode 100644 index 2ea89100b..000000000 --- a/nix/squoosh-codec-builders/flake.nix +++ /dev/null @@ -1,66 +0,0 @@ -{ - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/24.05"; - fenix.url = "github:nix-community/fenix"; - wasm-bindgen = { - url = "path:../wasm-bindgen"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - rust-helpers = { - url = "path:../rust-helpers"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - }; - outputs = - { - self, - nixpkgs, - fenix, - wasm-bindgen, - rust-helpers, - }: - { - lib = { - buildSquooshCodecRust = - { - name, - system, - src, - cargoLock ? { - lockFile = "${src}/Cargo.lock"; - }, - wasmBindgenSha, - ... - }@args: - with nixpkgs.legacyPackages.${system}; - let - wasm-bindgen-bin = wasm-bindgen.lib.buildFromCargoLock { - inherit system cargoLock; - sha256 = wasmBindgenSha; - }; - - codecBuild = rust-helpers.lib.buildRustPackage { - inherit system src cargoLock; - name = "${name}-codec"; - target = "wasm32-unknown-unknown"; - }; - in - stdenv.mkDerivation ( - (removeAttrs args [ "cargoLock" ]) - // { - inherit codecBuild; - dontConfigure = true; - nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ wasm-bindgen-bin ]; - buildPhase = '' - runHook preBuild - - wasm-bindgen --target web --out-dir $out $codecBuild/*.wasm - - runHook postBuild - ''; - dontInstall = true; - } - ); - }; - }; -} diff --git a/nix/wasm-bindgen/default.nix b/nix/wasm-bindgen/default.nix new file mode 100644 index 000000000..9e7518027 --- /dev/null +++ b/nix/wasm-bindgen/default.nix @@ -0,0 +1,55 @@ +{ + lib, + fetchCrate, + rustPlatform, + curl, + darwin, +}: +rec { + build = + { version, sha256 }: + let + src = fetchCrate { + pname = "wasm-bindgen-cli"; + inherit version sha256; + }; + + cargoLock = { + lockFile = "${src}/Cargo.lock"; + }; + in + rustPlatform.buildRustPackage { + name = "wasm-bindgen-cli"; + inherit src cargoLock; + buildInputs = [ + curl + darwin.apple_sdk.frameworks.Security + ]; + doCheck = false; + }; + + buildFromCargoLock = + { + system, + cargoLock, + sha256, + }: + assert (cargoLock.lockFile or null == null) != (cargoLock.lockFileContents or null == null); + let + lockFileContents = + if cargoLock.lockFile != null then + builtins.readFile cargoLock.lockFile + else + cargoLock.lockFileContents; + + parsedLockFile = builtins.fromTOML lockFileContents; + + wasm-bindgen-version = + (lib.lists.findFirst (x: x.name == "wasm-bindgen") null parsedLockFile.package).version; + in + assert wasm-bindgen-version != null; + build { + inherit system sha256; + version = wasm-bindgen-version; + }; +} diff --git a/nix/wasm-bindgen/flake.nix b/nix/wasm-bindgen/flake.nix deleted file mode 100644 index bc7bf6138..000000000 --- a/nix/wasm-bindgen/flake.nix +++ /dev/null @@ -1,60 +0,0 @@ -{ - outputs = - { self, nixpkgs }: - { - lib = { - build = - { - system, - version, - sha256, - }: - with nixpkgs.legacyPackages.${system}; - let - src = pkgs.fetchCrate { - pname = "wasm-bindgen-cli"; - inherit version sha256; - }; - - cargoLock = { - lockFile = "${src}/Cargo.lock"; - }; - in - rustPlatform.buildRustPackage { - name = "wasm-bindgen-cli"; - inherit src cargoLock; - buildInputs = [ - curl - darwin.apple_sdk.frameworks.Security - ]; - doCheck = false; - }; - - buildFromCargoLock = - { - system, - cargoLock, - sha256, - }: - with nixpkgs.legacyPackages.${system}; - assert (cargoLock.lockFile or null == null) != (cargoLock.lockFileContents or null == null); - let - lockFileContents = - if cargoLock.lockFile != null then - builtins.readFile cargoLock.lockFile - else - cargoLock.lockFileContents; - - parsedLockFile = builtins.fromTOML lockFileContents; - - wasm-bindgen-version = - (lib.lists.findFirst (x: x.name == "wasm-bindgen") null parsedLockFile.package).version; - in - assert wasm-bindgen-version != null; - self.lib.build { - inherit system sha256; - version = wasm-bindgen-version; - }; - }; - }; -}