Skip to content

Commit

Permalink
Move away from subflakes
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Sep 6, 2024
1 parent 7e564e2 commit 738d07d
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 212 deletions.
27 changes: 15 additions & 12 deletions codecs/webp/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
webp-src,
}:
let
optionSets = {
packageVariants = {
base = {
simd = false;
};
Expand All @@ -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}";
Expand Down Expand Up @@ -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 = {
Expand All @@ -123,5 +125,6 @@
};
};
}

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

This file was deleted.

51 changes: 51 additions & 0 deletions nix/squoosh-codec-builders/default.nix
Original file line number Diff line number Diff line change
@@ -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;
}
66 changes: 0 additions & 66 deletions nix/squoosh-codec-builders/flake.nix

This file was deleted.

55 changes: 55 additions & 0 deletions nix/wasm-bindgen/default.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
Loading

0 comments on commit 738d07d

Please sign in to comment.