0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

build-rust-crate: nixpkgs-fmt

This commit is contained in:
happysalada 2021-09-03 12:14:46 +09:00 committed by Raphael Megzari
parent 0e8d59e3cb
commit 0585c981f1

View file

@ -4,8 +4,16 @@
# This can be useful for deploying packages with NixOps, and to share # This can be useful for deploying packages with NixOps, and to share
# binary dependencies between projects. # binary dependencies between projects.
{ lib, stdenv, defaultCrateOverrides, fetchCrate, pkgsBuildBuild, rustc, rust { lib
, cargo, jq }: , stdenv
, defaultCrateOverrides
, fetchCrate
, pkgsBuildBuild
, rustc
, rust
, cargo
, jq
}:
let let
# Create rustc arguments to link against the given list of dependencies # Create rustc arguments to link against the given list of dependencies
@ -13,18 +21,21 @@ let
# #
# See docs for crateRenames below. # See docs for crateRenames below.
mkRustcDepArgs = dependencies: crateRenames: mkRustcDepArgs = dependencies: crateRenames:
lib.concatMapStringsSep " " (dep: lib.concatMapStringsSep " "
(dep:
let let
normalizeName = lib.replaceStrings ["-"] ["_"]; normalizeName = lib.replaceStrings [ "-" ] [ "_" ];
extern = normalizeName dep.libName; extern = normalizeName dep.libName;
# Find a choice that matches in name and optionally version. # Find a choice that matches in name and optionally version.
findMatchOrUseExtern = choices: findMatchOrUseExtern = choices:
lib.findFirst (choice: lib.findFirst
(choice:
(!(choice ? version) (!(choice ? version)
|| choice.version == dep.version or "")) || choice.version == dep.version or ""))
{ rename = extern; } { rename = extern; }
choices; choices;
name = if lib.hasAttr dep.crateName crateRenames then name =
if lib.hasAttr dep.crateName crateRenames then
let choices = crateRenames.${dep.crateName}; let choices = crateRenames.${dep.crateName};
in in
normalizeName ( normalizeName (
@ -34,11 +45,13 @@ let
) )
else else
extern; extern;
in (if lib.any (x: x == "lib" || x == "rlib") dep.crateType then in
(if lib.any (x: x == "lib" || x == "rlib") dep.crateType then
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib" " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib"
else else
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}") " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
) dependencies; )
dependencies;
# Create feature arguments for rustc. # Create feature arguments for rustc.
mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"''); mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"'');
@ -60,12 +73,13 @@ let
rustAttrs = rust; rustAttrs = rust;
in in
/* The overridable pkgs.buildRustCrate function. /* The overridable pkgs.buildRustCrate function.
* *
* Any unrecognized parameters will be passed as to * Any unrecognized parameters will be passed as to
* the underlying stdenv.mkDerivation. * the underlying stdenv.mkDerivation.
*/ */
crate_: lib.makeOverridable ( crate_: lib.makeOverridable
(
# The rust compiler to use. # The rust compiler to use.
# #
# Default: pkgs.rustc # Default: pkgs.rustc
@ -185,13 +199,28 @@ in
, postInstall , postInstall
}: }:
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_); let
crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: { }) crateOverrides crate_);
dependencies_ = dependencies; dependencies_ = dependencies;
buildDependencies_ = buildDependencies; buildDependencies_ = buildDependencies;
processedAttrs = [ processedAttrs = [
"src" "nativeBuildInputs" "buildInputs" "crateBin" "crateLib" "libName" "libPath" "src"
"buildDependencies" "dependencies" "features" "crateRenames" "nativeBuildInputs"
"crateName" "version" "build" "authors" "colors" "edition" "buildInputs"
"crateBin"
"crateLib"
"libName"
"libPath"
"buildDependencies"
"dependencies"
"features"
"crateRenames"
"crateName"
"version"
"build"
"authors"
"colors"
"edition"
"buildTests" "buildTests"
]; ];
extraDerivationAttrs = builtins.removeAttrs crate processedAttrs; extraDerivationAttrs = builtins.removeAttrs crate processedAttrs;
@ -203,10 +232,10 @@ let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverr
# crate2nix has a hack for the old bash based build script that did split # crate2nix has a hack for the old bash based build script that did split
# entries at `,`. No we have to work around that hack. # entries at `,`. No we have to work around that hack.
# https://github.com/kolloch/crate2nix/blame/5b19c1b14e1b0e5522c3e44e300d0b332dc939e7/crate2nix/templates/build.nix.tera#L89 # https://github.com/kolloch/crate2nix/blame/5b19c1b14e1b0e5522c3e44e300d0b332dc939e7/crate2nix/templates/build.nix.tera#L89
crateBin = lib.filter (bin: !(bin ? name && bin.name == ",")) (crate.crateBin or []); crateBin = lib.filter (bin: !(bin ? name && bin.name == ",")) (crate.crateBin or [ ]);
hasCrateBin = crate ? crateBin; hasCrateBin = crate ? crateBin;
in in
stdenv.mkDerivation (rec { stdenv.mkDerivation (rec {
inherit (crate) crateName; inherit (crate) crateName;
inherit inherit
@ -228,8 +257,8 @@ stdenv.mkDerivation (rec {
name = "rust_${crate.crateName}-${crate.version}${lib.optionalString buildTests_ "-test"}"; name = "rust_${crate.crateName}-${crate.version}${lib.optionalString buildTests_ "-test"}";
version = crate.version; version = crate.version;
depsBuildBuild = [ pkgsBuildBuild.stdenv.cc ]; depsBuildBuild = [ pkgsBuildBuild.stdenv.cc ];
nativeBuildInputs = [ rust stdenv.cc cargo jq ] ++ (crate.nativeBuildInputs or []) ++ nativeBuildInputs_; nativeBuildInputs = [ rust stdenv.cc cargo jq ] ++ (crate.nativeBuildInputs or [ ]) ++ nativeBuildInputs_;
buildInputs = (crate.buildInputs or []) ++ buildInputs_; buildInputs = (crate.buildInputs or [ ]) ++ buildInputs_;
dependencies = map lib.getLib dependencies_; dependencies = map lib.getLib dependencies_;
buildDependencies = map lib.getLib buildDependencies_; buildDependencies = map lib.getLib buildDependencies_;
@ -250,12 +279,14 @@ stdenv.mkDerivation (rec {
# Seed the symbol hashes with something unique every time. # Seed the symbol hashes with something unique every time.
# https://doc.rust-lang.org/1.0.0/rustc/metadata/loader/index.html#frobbing-symbols # https://doc.rust-lang.org/1.0.0/rustc/metadata/loader/index.html#frobbing-symbols
metadata = let metadata =
let
depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies); depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
hashedMetadata = builtins.hashString "sha256" hashedMetadata = builtins.hashString "sha256"
(crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) + (crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) +
"___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform); "___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform);
in lib.substring 0 10 hashedMetadata; in
lib.substring 0 10 hashedMetadata;
build = crate.build or ""; build = crate.build or "";
# Either set to a concrete sub path to the crate root # Either set to a concrete sub path to the crate root
@ -263,14 +294,14 @@ stdenv.mkDerivation (rec {
workspace_member = crate.workspace_member or "."; workspace_member = crate.workspace_member or ".";
crateVersion = crate.version; crateVersion = crate.version;
crateDescription = crate.description or ""; crateDescription = crate.description or "";
crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else []; crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [ ];
crateHomepage = crate.homepage or ""; crateHomepage = crate.homepage or "";
crateType = crateType =
if lib.attrByPath ["procMacro"] false crate then ["proc-macro"] else if lib.attrByPath [ "procMacro" ] false crate then [ "proc-macro" ] else
if lib.attrByPath ["plugin"] false crate then ["dylib"] else if lib.attrByPath [ "plugin" ] false crate then [ "dylib" ] else
(crate.type or ["lib"]); (crate.type or [ "lib" ]);
colors = lib.attrByPath [ "colors" ] "always" crate; colors = lib.attrByPath [ "colors" ] "always" crate;
extraLinkFlags = lib.concatStringsSep " " (crate.extraLinkFlags or []); extraLinkFlags = lib.concatStringsSep " " (crate.extraLinkFlags or [ ]);
edition = crate.edition or null; edition = crate.edition or null;
extraRustcOpts = extraRustcOpts =
lib.optionals (crate ? extraRustcOpts) crate.extraRustcOpts lib.optionals (crate ? extraRustcOpts) crate.extraRustcOpts
@ -297,20 +328,22 @@ stdenv.mkDerivation (rec {
outputs = if buildTests then [ "out" ] else [ "out" "lib" ]; outputs = if buildTests then [ "out" ] else [ "out" "lib" ];
outputDev = if buildTests then [ "out" ] else [ "lib" ]; outputDev = if buildTests then [ "out" ] else [ "lib" ];
} // extraDerivationAttrs } // extraDerivationAttrs
)) { )
)
{
rust = rustc; rust = rustc;
release = crate_.release or true; release = crate_.release or true;
verbose = crate_.verbose or true; verbose = crate_.verbose or true;
extraRustcOpts = []; extraRustcOpts = [ ];
features = []; features = [ ];
nativeBuildInputs = []; nativeBuildInputs = [ ];
buildInputs = []; buildInputs = [ ];
crateOverrides = defaultCrateOverrides; crateOverrides = defaultCrateOverrides;
preUnpack = crate_.preUnpack or ""; preUnpack = crate_.preUnpack or "";
postUnpack = crate_.postUnpack or ""; postUnpack = crate_.postUnpack or "";
prePatch = crate_.prePatch or ""; prePatch = crate_.prePatch or "";
patches = crate_.patches or []; patches = crate_.patches or [ ];
postPatch = crate_.postPatch or ""; postPatch = crate_.postPatch or "";
preConfigure = crate_.preConfigure or ""; preConfigure = crate_.preConfigure or "";
postConfigure = crate_.postConfigure or ""; postConfigure = crate_.postConfigure or "";
@ -318,8 +351,8 @@ stdenv.mkDerivation (rec {
postBuild = crate_.postBuild or ""; postBuild = crate_.postBuild or "";
preInstall = crate_.preInstall or ""; preInstall = crate_.preInstall or "";
postInstall = crate_.postInstall or ""; postInstall = crate_.postInstall or "";
dependencies = crate_.dependencies or []; dependencies = crate_.dependencies or [ ];
buildDependencies = crate_.buildDependencies or []; buildDependencies = crate_.buildDependencies or [ ];
crateRenames = crate_.crateRenames or {}; crateRenames = crate_.crateRenames or { };
buildTests = crate_.buildTests or false; buildTests = crate_.buildTests or false;
} }