build-rust-crate: nixpkgs-fmt

launchpad/nixpkgs/master
happysalada 3 years ago committed by Raphael Megzari
parent 0e8d59e3cb
commit 0585c981f1
  1. 585
      pkgs/build-support/rust/build-rust-crate/default.nix

@ -4,313 +4,346 @@
# 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
# and renames. # and renames.
# #
# 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 ? version) (choice:
|| choice.version == dep.version or "")) (!(choice ? version)
{ rename = extern; } || choice.version == dep.version or ""))
choices; { rename = extern; }
name = if lib.hasAttr dep.crateName crateRenames then choices;
let choices = crateRenames.${dep.crateName}; name =
in if lib.hasAttr dep.crateName crateRenames then
normalizeName ( let choices = crateRenames.${dep.crateName};
if builtins.isList choices in
then (findMatchOrUseExtern choices).rename normalizeName (
else choices if builtins.isList choices
) then (findMatchOrUseExtern choices).rename
else else choices
extern; )
in (if lib.any (x: x == "lib" || x == "rlib") dep.crateType then else
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib" extern;
else in
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}") (if lib.any (x: x == "lib" || x == "rlib") dep.crateType then
) dependencies; " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib"
else
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
)
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}\"'');
inherit (import ./log.nix { inherit lib; }) noisily echo_colored; inherit (import ./log.nix { inherit lib; }) noisily echo_colored;
configureCrate = import ./configure-crate.nix { configureCrate = import ./configure-crate.nix {
inherit lib stdenv rust echo_colored noisily mkRustcDepArgs mkRustcFeatureArgs; inherit lib stdenv rust echo_colored noisily mkRustcDepArgs mkRustcFeatureArgs;
}; };
buildCrate = import ./build-crate.nix { buildCrate = import ./build-crate.nix {
inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs rust; inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs rust;
}; };
installCrate = import ./install-crate.nix { inherit stdenv; }; installCrate = import ./install-crate.nix { inherit stdenv; };
# Allow access to the rust attribute set from inside buildRustCrate, which # Allow access to the rust attribute set from inside buildRustCrate, which
# has a parameter that shadows the name. # has a parameter that shadows the name.
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 #
{ rust # Default: pkgs.rustc
# Whether to build a release version (`true`) or a debug { rust
# version (`false`). Debug versions are faster to build # Whether to build a release version (`true`) or a debug
# but might be much slower at runtime. # version (`false`). Debug versions are faster to build
, release # but might be much slower at runtime.
# Whether to print rustc invocations etc. , release
# # Whether to print rustc invocations etc.
# Example: false #
# Default: true # Example: false
, verbose # Default: true
# A list of rust/cargo features to enable while building the crate. , verbose
# Example: [ "std" "async" ] # A list of rust/cargo features to enable while building the crate.
, features # Example: [ "std" "async" ]
# Additional native build inputs for building this crate. , features
, nativeBuildInputs # Additional native build inputs for building this crate.
# Additional build inputs for building this crate. , nativeBuildInputs
# # Additional build inputs for building this crate.
# Example: [ pkgs.openssl ] #
, buildInputs # Example: [ pkgs.openssl ]
# Allows to override the parameters to buildRustCrate , buildInputs
# for any rust dependency in the transitive build tree. # Allows to override the parameters to buildRustCrate
# # for any rust dependency in the transitive build tree.
# Default: pkgs.defaultCrateOverrides #
# # Default: pkgs.defaultCrateOverrides
# Example: #
# # Example:
# pkgs.defaultCrateOverrides // { #
# hello = attrs: { buildInputs = [ openssl ]; }; # pkgs.defaultCrateOverrides // {
# } # hello = attrs: { buildInputs = [ openssl ]; };
, crateOverrides # }
# Rust library dependencies, i.e. other libaries that were built , crateOverrides
# with buildRustCrate. # Rust library dependencies, i.e. other libaries that were built
, dependencies # with buildRustCrate.
# Rust build dependencies, i.e. other libaries that were built , dependencies
# with buildRustCrate and are used by a build script. # Rust build dependencies, i.e. other libaries that were built
, buildDependencies # with buildRustCrate and are used by a build script.
# Specify the "extern" name of a library if it differs from the library target. , buildDependencies
# See above for an extended explanation. # Specify the "extern" name of a library if it differs from the library target.
# # See above for an extended explanation.
# Default: no renames. #
# # Default: no renames.
# Example: #
# # Example:
# `crateRenames` supports two formats. #
# # `crateRenames` supports two formats.
# The simple version is an attrset that maps the #
# `crateName`s of the dependencies to their alternative # The simple version is an attrset that maps the
# names. # `crateName`s of the dependencies to their alternative
# # names.
# ```nix #
# { # ```nix
# my_crate_name = "my_alternative_name"; # {
# # ... # my_crate_name = "my_alternative_name";
# } # # ...
# ``` # }
# # ```
# The extended version is also keyed by the `crateName`s but allows #
# different names for different crate versions: # The extended version is also keyed by the `crateName`s but allows
# # different names for different crate versions:
# ```nix #
# { # ```nix
# my_crate_name = [ # {
# { version = "1.2.3"; rename = "my_alternative_name01"; } # my_crate_name = [
# { version = "3.2.3"; rename = "my_alternative_name03"; } # { version = "1.2.3"; rename = "my_alternative_name01"; }
# ] # { version = "3.2.3"; rename = "my_alternative_name03"; }
# # ... # ]
# } # # ...
# ``` # }
# # ```
# This roughly corresponds to the following snippet in Cargo.toml: #
# # This roughly corresponds to the following snippet in Cargo.toml:
# ```toml #
# [dependencies] # ```toml
# my_alternative_name01 = { package = "my_crate_name", version = "0.1" } # [dependencies]
# my_alternative_name03 = { package = "my_crate_name", version = "0.3" } # my_alternative_name01 = { package = "my_crate_name", version = "0.1" }
# ``` # my_alternative_name03 = { package = "my_crate_name", version = "0.3" }
# # ```
# Dependencies which use the lib target name as extern name, do not need #
# to be specified in the crateRenames, even if their crate name differs. # Dependencies which use the lib target name as extern name, do not need
# # to be specified in the crateRenames, even if their crate name differs.
# Including multiple versions of a crate is very popular during #
# ecosystem transitions, e.g. from futures 0.1 to futures 0.3. # Including multiple versions of a crate is very popular during
, crateRenames # ecosystem transitions, e.g. from futures 0.1 to futures 0.3.
# A list of extra options to pass to rustc. , crateRenames
# # A list of extra options to pass to rustc.
# Example: [ "-Z debuginfo=2" ] #
# Default: [] # Example: [ "-Z debuginfo=2" ]
, extraRustcOpts # Default: []
# Whether to enable building tests. , extraRustcOpts
# Use true to enable. # Whether to enable building tests.
# Default: false # Use true to enable.
, buildTests # Default: false
# Passed to stdenv.mkDerivation. , buildTests
, preUnpack # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , preUnpack
, postUnpack # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , postUnpack
, prePatch # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , prePatch
, patches # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , patches
, postPatch # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , postPatch
, preConfigure # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , preConfigure
, postConfigure # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , postConfigure
, preBuild # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , preBuild
, postBuild # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , postBuild
, preInstall # Passed to stdenv.mkDerivation.
# Passed to stdenv.mkDerivation. , preInstall
, postInstall # Passed to stdenv.mkDerivation.
}: , postInstall
}:
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_); let
dependencies_ = dependencies; crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: { }) crateOverrides crate_);
buildDependencies_ = buildDependencies; dependencies_ = dependencies;
processedAttrs = [ buildDependencies_ = buildDependencies;
"src" "nativeBuildInputs" "buildInputs" "crateBin" "crateLib" "libName" "libPath" processedAttrs = [
"buildDependencies" "dependencies" "features" "crateRenames" "src"
"crateName" "version" "build" "authors" "colors" "edition" "nativeBuildInputs"
"buildTests" "buildInputs"
]; "crateBin"
extraDerivationAttrs = builtins.removeAttrs crate processedAttrs; "crateLib"
nativeBuildInputs_ = nativeBuildInputs; "libName"
buildInputs_ = buildInputs; "libPath"
extraRustcOpts_ = extraRustcOpts; "buildDependencies"
buildTests_ = buildTests; "dependencies"
"features"
"crateRenames"
"crateName"
"version"
"build"
"authors"
"colors"
"edition"
"buildTests"
];
extraDerivationAttrs = builtins.removeAttrs crate processedAttrs;
nativeBuildInputs_ = nativeBuildInputs;
buildInputs_ = buildInputs;
extraRustcOpts_ = extraRustcOpts;
buildTests_ = buildTests;
# 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
preUnpack preUnpack
postUnpack postUnpack
prePatch prePatch
patches patches
postPatch postPatch
preConfigure preConfigure
postConfigure postConfigure
preBuild preBuild
postBuild postBuild
preInstall preInstall
postInstall postInstall
buildTests buildTests
; ;
src = crate.src or (fetchCrate { inherit (crate) crateName version sha256; }); src = crate.src or (fetchCrate { inherit (crate) crateName version sha256; });
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_;
completeDeps = lib.unique (dependencies ++ lib.concatMap (dep: dep.completeDeps) dependencies); completeDeps = lib.unique (dependencies ++ lib.concatMap (dep: dep.completeDeps) dependencies);
completeBuildDeps = lib.unique ( completeBuildDeps = lib.unique (
buildDependencies buildDependencies
++ lib.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies ++ lib.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies
); );
# Create a list of features that are enabled by the crate itself and # Create a list of features that are enabled by the crate itself and
# through the features argument of buildRustCrate. Exclude features # through the features argument of buildRustCrate. Exclude features
# with a forward slash, since they are passed through to dependencies. # with a forward slash, since they are passed through to dependencies.
crateFeatures = lib.optionals (crate ? features) crateFeatures = lib.optionals (crate ? features)
(builtins.filter (f: !lib.hasInfix "/" f) (crate.features ++ features)); (builtins.filter (f: !lib.hasInfix "/" f) (crate.features ++ features));
libName = if crate ? libName then crate.libName else crate.crateName; libName = if crate ? libName then crate.libName else crate.crateName;
libPath = if crate ? libPath then crate.libPath else ""; libPath = if crate ? libPath then crate.libPath else "";
# 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 =
depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies); let
hashedMetadata = builtins.hashString "sha256" depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
(crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) + hashedMetadata = builtins.hashString "sha256"
"___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform); (crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) +
in lib.substring 0 10 hashedMetadata; "___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform);
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
# or use `null` for auto-detect. # or use `null` for auto-detect.
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
++ extraRustcOpts_ ++ extraRustcOpts_
++ (lib.optional (edition != null) "--edition ${edition}"); ++ (lib.optional (edition != null) "--edition ${edition}");
configurePhase = configureCrate { configurePhase = configureCrate {
inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription
crateFeatures crateRenames libName build workspace_member release libPath crateVersion crateFeatures crateRenames libName build workspace_member release libPath crateVersion
extraLinkFlags extraRustcOpts extraLinkFlags extraRustcOpts
crateAuthors crateHomepage verbose colors; crateAuthors crateHomepage verbose colors;
}; };
buildPhase = buildCrate { buildPhase = buildCrate {
inherit crateName dependencies inherit crateName dependencies
crateFeatures crateRenames libName release libPath crateType crateFeatures crateRenames libName release libPath crateType
metadata hasCrateBin crateBin verbose colors metadata hasCrateBin crateBin verbose colors
extraRustcOpts buildTests; extraRustcOpts buildTests;
}; };
installPhase = installCrate crateName metadata buildTests; installPhase = installCrate crateName metadata buildTests;
# depending on the test setting we are either producing something with bins # depending on the test setting we are either producing something with bins
# and libs or just test binaries # and libs or just test binaries
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;
} }

Loading…
Cancel
Save