Move away from subflakes

This commit is contained in:
Surma
2024-09-06 17:32:29 +01:00
parent 7e564e240a
commit 738d07d98a
7 changed files with 188 additions and 212 deletions

View File

@@ -15,7 +15,7 @@
webp-src, webp-src,
}: }:
let let
optionSets = { packageVariants = {
base = { base = {
simd = false; simd = false;
}; };
@@ -28,10 +28,10 @@
system: system:
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
packageBuilder = inherit (pkgs) lib stdenv runCommand emscripten writeShellScriptBin cmake;
with pkgs; packageVariantBuilder =
name: name:
{ simd }: { simd }@variantOptions:
{ {
"webp-squoosh-${name}" = stdenv.mkDerivation { "webp-squoosh-${name}" = stdenv.mkDerivation {
name = "webp-squoosh-${name}"; name = "webp-squoosh-${name}";
@@ -105,16 +105,18 @@
''; '';
}; };
forEachOption = pkgs.callPackage (import ../../nix/for-each-option.nix) { }; packages = lib.foldl (acc: v: acc//v) {} (lib.mapAttrsToList packageVariantBuilder packageVariants);
packageVariants = forEachOption packageBuilder optionSets;
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 in
with pkgs;
{ {
packages = packageVariants // { packages = packages // {
installScript = writeShellScriptBin "install.sh" '' installScript = writeShellScriptBin "install.sh" globalInstallerCode;
${self.packages.${system}.install-base}/bin/install.sh
${self.packages.${system}.install-simd}/bin/install.sh
'';
}; };
apps = { apps = {
install = { install = {
@@ -123,5 +125,6 @@
}; };
}; };
} }
); );
} }

View File

@@ -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;
'';
}
);
}

View File

@@ -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;
'';
}
);
};
};
}

View File

@@ -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;
}

View File

@@ -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;
}
);
};
};
}

View File

@@ -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;
};
}

View File

@@ -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;
};
};
};
}