magic-nix-cache/crane.nix

109 lines
3.4 KiB
Nix
Raw Normal View History

2023-05-19 08:48:52 +00:00
{ stdenv
, pkgs
, lib
, crane
, rust
, rust-bin
2023-05-19 08:48:52 +00:00
, nix-gitignore
, supportedSystems
}:
let
inherit (stdenv.hostPlatform) system;
nightlyVersion = "2023-05-01";
rustNightly = (pkgs.rust-bin.nightly.${nightlyVersion}.default.override {
extensions = [ "rust-src" "rust-analyzer-preview" ];
targets = cargoTargets;
}).overrideAttrs (old: {
# Remove the propagated libiconv since we want to add our static version
depsTargetTargetPropagated = lib.filter (d: d.pname != "libiconv")
(lib.flatten (old.depsTargetTargetPropagated or []));
});
2023-05-19 08:48:52 +00:00
# For easy cross-compilation in devShells
# We are just composing the pkgsCross.*.stdenv.cc together
crossPlatforms = let
makeCrossPlatform = crossSystem: let
pkgsCross =
if crossSystem == system then pkgs
else import pkgs.path {
inherit system crossSystem;
overlays = [];
};
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";
2023-05-19 08:48:52 +00:00
in {
name = crossSystem;
value = {
inherit rustTargetSpec;
cc = pkgsCross.stdenv.cc;
2023-05-19 08:48:52 +00:00
pkgs = pkgsCross;
buildInputs = makeBuildInputs pkgsCross;
2023-05-19 08:48:52 +00:00
env = {
"${cargoLinkerEnv}" = ccbin;
"${cargoCcEnv}" = ccbin;
2023-05-19 08:48:52 +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";
2023-05-19 08:48:52 +00:00
in builtins.listToAttrs (map makeCrossPlatform systems);
cargoTargets = lib.mapAttrsToList (_: p: p.rustTargetSpec) crossPlatforms;
cargoCrossEnvs = lib.foldl (acc: p: acc // p.env) {} (builtins.attrValues crossPlatforms);
makeBuildInputs = pkgs: with pkgs; []
++ lib.optionals pkgs.stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
(libiconv.override { enableStatic = true; enableShared = false; })
];
2023-05-19 08:48:52 +00:00
buildFor = system: let
crossPlatform = crossPlatforms.${system};
inherit (crossPlatform) pkgs;
craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly;
2023-05-19 19:00:36 +00:00
crateName = craneLib.crateNameFromCargoToml {
cargoToml = ./nix-actions-cache/Cargo.toml;
};
2023-05-19 08:48:52 +00:00
src = nix-gitignore.gitignoreSource [] ./.;
2023-05-22 22:00:34 +00:00
commonArgs = {
inherit (crateName) pname version;
inherit src;
buildInputs = makeBuildInputs pkgs;
2023-05-19 08:48:52 +00:00
2023-05-22 22:00:34 +00:00
cargoExtraArgs = "--target ${crossPlatform.rustTargetSpec}";
2023-05-19 08:48:52 +00:00
2023-05-22 22:00:34 +00:00
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles src) cargoConfigs;
cargoLockList = [
./Cargo.lock
"${rustNightly.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock"
];
};
} // crossPlatform.env;
2023-05-19 08:48:52 +00:00
2023-05-22 22:00:34 +00:00
crate = craneLib.buildPackage (commonArgs // {
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
2023-05-19 08:48:52 +00:00
# The resulting executable must be standalone
allowedRequisites = [];
2023-05-22 22:00:34 +00:00
});
2023-05-19 08:48:52 +00:00
in crate;
in {
inherit crossPlatforms cargoTargets cargoCrossEnvs rustNightly;
2023-05-19 08:48:52 +00:00
nix-actions-cache = buildFor system;
}