naersk/build.nix

235 lines
6.6 KiB
Nix
Raw Normal View History

2019-06-25 15:36:22 +00:00
src:
2019-09-04 10:14:36 +00:00
{ preBuild
#| What command to run during the build phase
, cargoBuild
2019-06-28 09:43:47 +00:00
, #| What command to run during the test phase
cargoTestCommands
#| Whether or not to forward intermediate build artifacts to $out
, copyTarget ? false
#| Whether or not to copy binaries to $out/bin
, copyBins ? true
2019-06-25 15:36:22 +00:00
, doCheck ? true
2019-07-16 09:06:29 +00:00
, doDoc ? true
2019-08-28 17:21:13 +00:00
#| Whether or not the rustdoc can fail the build
, doDocFail ? false
, copyDocsToSeparateOutput ? true
#| Whether to remove references to source code from the generated cargo docs
# to reduce Nix closure size. By default cargo doc includes snippets like the
# following in the generated highlighted source code in files like: src/rand/lib.rs.html:
#
# <meta name="description" content="Source to the Rust file `/nix/store/mdwpqciww926xayfasl85i4wvvpbgb9a-crates-io/rand-0.7.0/src/lib.rs`.">
#
# The reference to /nix/store/...-crates-io/... causes a run-time dependency
# to the complete source code blowing up the Nix closure size for no good
# reason. If this argument is set to true (which is the default) the latter
# will be replaced by:
#
# <meta name="description" content="Source to the Rust file removed to reduce Nix closure size.">
#
# Which drops the run-time dependency on the crates-io source thereby
# significantly reducing the Nix closure size.
, removeReferencesToSrcFromDocs ? true
2019-07-04 14:34:33 +00:00
, name
2019-07-11 09:39:40 +00:00
, version
2019-07-02 10:50:32 +00:00
, rustc
, cargo
2019-06-25 15:36:22 +00:00
, override ? null
, buildInputs ? []
, nativeBuildInputs ? []
2019-06-27 10:43:01 +00:00
, builtDependencies ? []
2019-08-28 17:21:13 +00:00
, replaceToml ? true
2019-07-04 11:44:08 +00:00
, release ? true
2019-06-25 15:36:22 +00:00
, stdenv
, lib
, llvmPackages
2019-06-27 10:43:01 +00:00
, rsync
2019-06-25 15:36:22 +00:00
, jq
, darwin
, writeText
, symlinkJoin
, runCommand
2019-06-27 15:49:59 +00:00
, remarshal
2019-07-04 14:34:33 +00:00
, crateDependencies
2019-08-28 17:21:13 +00:00
# TODO: rename to "members"
2019-07-04 14:34:33 +00:00
, cratePaths
2019-06-25 15:36:22 +00:00
}:
2019-07-02 10:50:32 +00:00
with
{ builtinz =
builtins //
2019-07-31 11:41:34 +00:00
import ./builtins
2019-07-30 10:41:32 +00:00
{ inherit lib writeText remarshal runCommand ; };
2019-07-02 10:50:32 +00:00
};
2019-06-25 15:36:22 +00:00
with rec
{
2019-07-31 13:53:30 +00:00
drv = stdenv.mkDerivation (
2019-07-04 14:34:33 +00:00
{ inherit
src
doCheck
nativeBuildInputs
cratePaths
2019-07-11 09:39:40 +00:00
name
2019-09-04 10:14:36 +00:00
version
preBuild;
2019-06-27 10:43:01 +00:00
2019-08-28 17:21:13 +00:00
cargoconfig = builtinz.toTOML
{ source =
{ crates-io = { replace-with = "nix-sources"; } ;
nix-sources =
{ directory = symlinkJoin
{ name = "crates-io";
paths = map (v: unpackCrate v.name v.version v.sha256)
crateDependencies;
};
};
};
};
2019-07-31 14:16:51 +00:00
outputs = [ "out" ] ++ lib.optional (doDoc && copyDocsToSeparateOutput) "doc";
2019-07-16 09:06:29 +00:00
preInstallPhases = lib.optional doDoc [ "docPhase" ];
2019-06-25 15:36:22 +00:00
# Otherwise specifying CMake as a dep breaks the build
dontUseCmakeConfigure = true;
buildInputs =
[ cargo
# needed for "dsymutil"
llvmPackages.stdenv.cc.bintools
# needed for "cc"
llvmPackages.stdenv.cc
2019-07-04 14:34:33 +00:00
# needed at various steps in the build
2019-06-25 15:36:22 +00:00
jq
2019-06-27 10:43:01 +00:00
rsync
2019-06-25 15:36:22 +00:00
] ++ (stdenv.lib.optionals stdenv.isDarwin
[ darwin.Security
darwin.apple_sdk.frameworks.CoreServices
darwin.cf-private
]) ++ buildInputs;
2019-06-27 10:43:01 +00:00
2019-06-25 15:36:22 +00:00
LIBCLANG_PATH="${llvmPackages.libclang.lib}/lib";
CXX="clang++";
RUSTC="${rustc}/bin/rustc";
configurePhase =
''
cargo_release=( ${lib.optionalString release "--release" } )
2019-06-25 15:36:22 +00:00
runHook preConfigure
2019-06-27 10:43:01 +00:00
2019-09-17 10:27:53 +00:00
logRun() {
echo "$@"
eval "$@"
}
2019-06-27 10:43:01 +00:00
mkdir -p target
2019-07-30 10:41:32 +00:00
cat ${builtinz.writeJSON "dependencies-json" builtDependencies} |\
2019-06-25 15:36:22 +00:00
jq -r '.[]' |\
while IFS= read -r dep
do
2019-06-27 10:43:01 +00:00
echo pre-installing dep $dep
2019-07-11 09:39:40 +00:00
rsync -rl \
--no-perms \
--no-owner \
--no-group \
--chmod=+w \
--executability $dep/target/ target
2019-06-27 10:43:01 +00:00
chmod +w -R target
2019-06-25 15:36:22 +00:00
done
export CARGO_HOME=''${CARGO_HOME:-$PWD/.cargo-home}
mkdir -p $CARGO_HOME
2019-07-31 14:16:51 +00:00
echo "$cargoconfig" > $CARGO_HOME/config
2019-06-25 15:36:22 +00:00
2019-06-27 10:43:01 +00:00
# TODO: figure out why "1" works whereas "0" doesn't
find . -type f -exec touch --date=@1 {} +
2019-06-25 15:36:22 +00:00
runHook postConfigure
'';
buildPhase =
''
runHook preBuild
logRun ${cargoBuild}
2019-06-25 15:36:22 +00:00
runHook postBuild
'';
checkPhase =
''
runHook preCheck
${lib.concatMapStringsSep "\n" (cmd: "logRun ${cmd}") cargoTestCommands}
2019-06-25 15:36:22 +00:00
runHook postCheck
'';
2019-07-16 09:06:29 +00:00
docPhase = lib.optionalString doDoc ''
runHook preDoc
logRun cargo doc --offline "''${cargo_release[*]}" || ${if doDocFail then "false" else "true" }
2019-07-16 09:06:29 +00:00
${lib.optionalString removeReferencesToSrcFromDocs ''
# Remove references to the source derivation to reduce closure size
match='<meta name="description" content="Source to the Rust file `${builtins.storeDir}[^`]*`.">'
replacement='<meta name="description" content="Source to the Rust file removed to reduce Nix closure size.">'
find target/doc -name "*\.rs\.html" -exec sed -i "s|$match|$replacement|" {} +
''}
2019-07-16 09:06:29 +00:00
runHook postDoc
'';
2019-06-25 15:36:22 +00:00
installPhase =
''
runHook preInstall
${lib.optionalString copyBins ''
2019-06-25 15:36:22 +00:00
mkdir -p $out/bin
find out -type f -executable -exec cp {} $out/bin \;
''}
2019-06-25 15:36:22 +00:00
${lib.optionalString copyTarget ''
2019-07-30 10:41:32 +00:00
mkdir -p $out
2019-06-27 10:43:01 +00:00
cp -r target $out
2019-07-30 10:41:32 +00:00
''}
2019-06-27 10:43:01 +00:00
${lib.optionalString (doDoc && copyDocsToSeparateOutput) ''
2019-07-30 10:41:32 +00:00
cp -r target/doc $doc
2019-07-16 09:06:29 +00:00
''}
2019-06-25 15:36:22 +00:00
runHook postInstall
'';
passthru = {
# Handy for debugging
inherit builtDependencies;
};
2019-08-28 17:21:13 +00:00
}
2019-07-31 13:53:30 +00:00
)
;
2019-06-25 15:36:22 +00:00
# XXX: the actual crate format is not documented but in practice is a
# gzipped tar; we simply unpack it and introduce a ".cargo-checksum.json"
# file that cargo itself uses to double check the sha256
unpackCrate = name: version: sha256:
with
{ crate = builtins.fetchurl
{ url = "https://crates.io/api/v1/crates/${name}/${version}/download";
inherit sha256;
};
};
runCommand "unpack-${name}-${version}" {}
''
mkdir -p $out
tar -xvzf ${crate} -C $out
echo '{"package":"${sha256}","files":{}}' > $out/${name}-${version}/.cargo-checksum.json
'';
};
if isNull override then drv else drv.overrideAttrs override