2023-05-19 08:48:52 +00:00
|
|
|
{ stdenv
|
|
|
|
, pkgs
|
|
|
|
, lib
|
|
|
|
, crane
|
|
|
|
, rust
|
2023-05-22 22:00:34 +00:00
|
|
|
, rust-bin
|
2023-05-19 08:48:52 +00:00
|
|
|
, nix-gitignore
|
|
|
|
, supportedSystems
|
2023-12-14 16:09:09 +00:00
|
|
|
, nix-flake
|
2023-05-19 08:48:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (stdenv.hostPlatform) system;
|
|
|
|
|
2024-03-29 20:19:44 +00:00
|
|
|
nightlyVersion = "2024-03-28";
|
2023-05-22 22:00:34 +00:00
|
|
|
rustNightly = (pkgs.rust-bin.nightly.${nightlyVersion}.default.override {
|
2023-05-22 22:00:34 +00:00
|
|
|
extensions = [ "rust-src" "rust-analyzer-preview" ];
|
|
|
|
targets = cargoTargets;
|
2023-05-22 22:00:34 +00:00
|
|
|
}).overrideAttrs (old: {
|
|
|
|
# Remove the propagated libiconv since we want to add our static version
|
|
|
|
depsTargetTargetPropagated = lib.filter (d: d.pname != "libiconv")
|
2023-06-22 18:19:49 +00:00
|
|
|
(lib.flatten (old.depsTargetTargetPropagated or [ ]));
|
2023-05-22 22:00:34 +00:00
|
|
|
});
|
2023-05-22 22:00:34 +00:00
|
|
|
|
2023-05-19 08:48:52 +00:00
|
|
|
# For easy cross-compilation in devShells
|
|
|
|
# We are just composing the pkgsCross.*.stdenv.cc together
|
2023-06-22 18:19:49 +00:00
|
|
|
crossPlatforms =
|
|
|
|
let
|
|
|
|
makeCrossPlatform = crossSystem:
|
|
|
|
let
|
|
|
|
pkgsCross =
|
|
|
|
if crossSystem == system then pkgs
|
|
|
|
else
|
|
|
|
import pkgs.path {
|
|
|
|
inherit system crossSystem;
|
2023-12-14 16:09:09 +00:00
|
|
|
overlays = [ nix-flake.overlays.default ];
|
2023-06-22 18:19:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rustTargetSpec = rust.toRustTargetSpec pkgsCross.pkgsStatic.stdenv.hostPlatform;
|
|
|
|
rustTargetSpecUnderscored = builtins.replaceStrings [ "-" ] [ "_" ] rustTargetSpec;
|
|
|
|
|
|
|
|
cargoLinkerEnv = lib.strings.toUpper "CARGO_TARGET_${rustTargetSpecUnderscored}_LINKER";
|
|
|
|
cargoCcEnv = "CC_${rustTargetSpecUnderscored}"; # for ring
|
|
|
|
|
|
|
|
ccbin = "${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
name = crossSystem;
|
|
|
|
value = {
|
|
|
|
inherit rustTargetSpec;
|
|
|
|
cc = pkgsCross.stdenv.cc;
|
|
|
|
pkgs = pkgsCross;
|
|
|
|
buildInputs = makeBuildInputs pkgsCross;
|
|
|
|
env = {
|
|
|
|
"${cargoLinkerEnv}" = ccbin;
|
|
|
|
"${cargoCcEnv}" = ccbin;
|
|
|
|
};
|
|
|
|
};
|
2023-05-19 08:48:52 +00:00
|
|
|
};
|
2023-06-22 18:19:49 +00:00
|
|
|
systems = lib.filter (s: s == system || lib.hasInfix "linux" s) supportedSystems
|
|
|
|
# Cross from aarch64-darwin -> x86_64-darwin doesn't work yet
|
|
|
|
# Hopefully the situation will improve with the SDK bumps
|
|
|
|
++ lib.optional (system == "x86_64-darwin") "aarch64-darwin";
|
|
|
|
in
|
|
|
|
builtins.listToAttrs (map makeCrossPlatform systems);
|
2023-05-19 08:48:52 +00:00
|
|
|
|
|
|
|
cargoTargets = lib.mapAttrsToList (_: p: p.rustTargetSpec) crossPlatforms;
|
2023-06-22 18:19:49 +00:00
|
|
|
cargoCrossEnvs = lib.foldl (acc: p: acc // p.env) { } (builtins.attrValues crossPlatforms);
|
2023-05-19 08:48:52 +00:00
|
|
|
|
2023-12-14 16:09:09 +00:00
|
|
|
makeBuildInputs = pkgs:
|
|
|
|
[ pkgs.nix
|
|
|
|
pkgs.boost # needed for clippy
|
|
|
|
]
|
2023-05-22 22:00:34 +00:00
|
|
|
++ lib.optionals pkgs.stdenv.isDarwin [
|
2023-12-14 16:09:09 +00:00
|
|
|
pkgs.darwin.apple_sdk.frameworks.Security
|
|
|
|
(pkgs.libiconv.override { enableStatic = true; enableShared = false; })
|
2023-06-22 18:19:49 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
buildFor = system:
|
|
|
|
let
|
|
|
|
crossPlatform = crossPlatforms.${system};
|
|
|
|
inherit (crossPlatform) pkgs;
|
|
|
|
craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly;
|
|
|
|
crateName = craneLib.crateNameFromCargoToml {
|
2023-06-24 22:24:52 +00:00
|
|
|
cargoToml = ./magic-nix-cache/Cargo.toml;
|
2023-05-22 22:00:34 +00:00
|
|
|
};
|
2023-05-19 08:48:52 +00:00
|
|
|
|
2023-06-22 18:19:49 +00:00
|
|
|
src = nix-gitignore.gitignoreSource [ ] ./.;
|
|
|
|
|
|
|
|
commonArgs = {
|
|
|
|
inherit (crateName) pname version;
|
|
|
|
inherit src;
|
2023-05-19 08:48:52 +00:00
|
|
|
|
2023-12-14 16:09:09 +00:00
|
|
|
nativeBuildInputs = [ pkgs.pkg-config ];
|
|
|
|
|
2023-06-22 18:19:49 +00:00
|
|
|
buildInputs = makeBuildInputs pkgs;
|
|
|
|
|
|
|
|
cargoExtraArgs = "--target ${crossPlatform.rustTargetSpec}";
|
|
|
|
|
|
|
|
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
|
|
|
|
inherit (craneLib.findCargoFiles src) cargoConfigs;
|
|
|
|
cargoLockList = [
|
|
|
|
./Cargo.lock
|
|
|
|
"${rustNightly.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
} // crossPlatform.env;
|
|
|
|
|
|
|
|
crate = craneLib.buildPackage (commonArgs // {
|
|
|
|
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
|
|
|
|
|
|
|
|
# The resulting executable must be standalone
|
|
|
|
allowedRequisites = [ ];
|
|
|
|
});
|
|
|
|
in
|
|
|
|
crate;
|
|
|
|
in
|
|
|
|
{
|
2023-05-22 22:00:34 +00:00
|
|
|
inherit crossPlatforms cargoTargets cargoCrossEnvs rustNightly;
|
2023-05-19 08:48:52 +00:00
|
|
|
|
2023-06-24 22:24:52 +00:00
|
|
|
magic-nix-cache = buildFor system;
|
2023-05-19 08:48:52 +00:00
|
|
|
}
|