naersk/build.nix

282 lines
8.3 KiB
Nix
Raw Normal View History

2019-06-25 15:36:22 +00:00
src:
2019-06-28 09:43:47 +00:00
{ #| What command to run during the build phase
2019-07-11 09:39:40 +00:00
cargoBuild
2019-06-28 09:43:47 +00:00
, #| What command to run during the test phase
2019-07-04 11:44:08 +00:00
cargoTest ? "cargo test --$CARGO_BUILD_PROFILE"
2019-07-30 10:41:32 +00:00
#| Whether or not to forward build artifacts to $out
, copyBuildArtifacts ? false
2019-06-25 15:36:22 +00:00
, doCheck ? true
2019-07-16 09:06:29 +00:00
, doDoc ? true
, 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-07-31 13:53:30 +00:00
, cargolock ? null
, cargotoml ? null
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
, 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
version;
2019-06-27 10:43:01 +00:00
2019-07-31 14:16:51 +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;
};
};
};
};
outputs = [ "out" ] ++ lib.optional (doDoc && copyDocsToSeparateOutput) "doc";
2019-07-16 09:06:29 +00:00
preInstallPhases = lib.optional doDoc [ "docPhase" ];
2019-07-04 11:44:08 +00:00
CARGO_BUILD_PROFILE = if release then "release" else "debug";
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 =
''
runHook preConfigure
2019-06-27 10:43:01 +00:00
2019-07-31 13:53:30 +00:00
if [ -n "$cargolock" ]
2019-06-28 09:43:47 +00:00
then
echo "Setting Cargo.lock"
if [ -f "Cargo.lock" ]
then
echo "WARNING: replacing existing Cargo.lock"
fi
install -m 644 "$cargolock" Cargo.lock
2019-06-28 09:43:47 +00:00
fi
2019-07-31 13:53:30 +00:00
if [ -n "$cargotoml" ]
2019-06-28 09:43:47 +00:00
then
echo "Setting Cargo.toml"
if [ -f "Cargo.toml" ]
then
echo "WARNING: replacing existing Cargo.toml"
fi
install -m 644 "$cargotoml" Cargo.toml
2019-06-28 09:43:47 +00:00
fi
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
2019-06-26 08:59:29 +00:00
echo "Running build command:"
2019-07-11 09:39:40 +00:00
echo " ${cargoBuild}"
2019-06-26 08:59:29 +00:00
${cargoBuild}
2019-06-25 15:36:22 +00:00
runHook postBuild
'';
checkPhase =
''
runHook preCheck
2019-06-26 08:59:29 +00:00
echo "Running test command:"
2019-07-11 09:39:40 +00:00
echo " ${cargoTest}"
2019-06-26 08:59:29 +00:00
${cargoTest}
2019-06-25 15:36:22 +00:00
runHook postCheck
'';
2019-07-16 09:06:29 +00:00
docPhase = lib.optionalString doDoc ''
runHook preDoc
# cargo doc defaults to "debug", but it doesn't have a
# "--debug" flag, only "--release", so we can't just pass
# "--$CARGO_BUILD_PROFILE" like we do with "cargo build" and "cargo
# test"
doc_arg=""
if [ "$CARGO_BUILD_PROFILE" == "release" ]
then
doc_arg="--release"
fi
cargoDoc="cargo doc --offline $doc_arg"
2019-07-16 09:06:29 +00:00
echo "Running doc command:"
echo " $cargoDoc"
$cargoDoc
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
2019-07-04 11:44:08 +00:00
# cargo install defaults to "release", but it doesn't have a
# "--release" flag, only "--debug", so we can't just pass
# "--$CARGO_BUILD_PROFILE" like we do with "cargo build" and "cargo
# test"
install_arg=""
2019-07-04 14:34:33 +00:00
if [ "$CARGO_BUILD_PROFILE" == "debug" ]
2019-07-04 11:44:08 +00:00
then
install_arg="--debug"
fi
2019-06-25 15:36:22 +00:00
mkdir -p $out/bin
2019-07-11 09:39:40 +00:00
for p in $cratePaths; do
2019-07-04 11:44:08 +00:00
# XXX: we don't quote install_arg to avoid passing an empty arg
# to cargo
2019-07-04 14:34:33 +00:00
cargo install \
--path $p \
$install_arg \
--bins \
--root $out ||\
2019-06-25 15:36:22 +00:00
echo "WARNING: Member wasn't installed: $p"
done
2019-07-30 10:41:32 +00:00
mkdir -p $out
2019-06-25 15:36:22 +00:00
mkdir -p $out/lib
2019-07-30 10:41:32 +00:00
${lib.optionalString copyBuildArtifacts ''
2019-07-04 11:44:08 +00:00
cp -vr target/$CARGO_BUILD_PROFILE/deps/* $out/lib ||\
2019-06-25 15:36:22 +00:00
echo "WARNING: couldn't copy libs"
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
'';
2019-07-31 13:53:30 +00:00
} //
lib.optionalAttrs (! isNull cargolock )
{ cargolock = builtinz.writeTOML "Cargo.lock" cargolock; } //
2019-07-31 13:53:30 +00:00
lib.optionalAttrs (! isNull cargotoml )
{ cargotoml = builtinz.writeTOML "Cargo.toml" cargotoml; }
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