From 1fd2dc01ee34817858261bebae6a79cb9debc1f9 Mon Sep 17 00:00:00 2001 From: oxalica Date: Thu, 2 Dec 2021 22:17:07 +0800 Subject: [PATCH] Split out aggregation derivation and refactor Fix #59 --- mk-aggregated.nix | 55 ++++++++++++++++++++++++++++++++++++++++++++ mk-component-set.nix | 25 ++++++++------------ rust-overlay.nix | 39 ++++--------------------------- 3 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 mk-aggregated.nix diff --git a/mk-aggregated.nix b/mk-aggregated.nix new file mode 100644 index 0000000..39c9568 --- /dev/null +++ b/mk-aggregated.nix @@ -0,0 +1,55 @@ +{ lib, stdenv, symlinkJoin, pkgsTargetTarget, bash, gcc }: +{ pname, version, components }: +let + inherit (lib) optional optionalString; +in +symlinkJoin { + name = pname + "-" + version; + inherit pname version; + + paths = components; + + # Ourselves have offset -1. In order to make these offset -1 dependencies of downstream derivation, + # they are offset 0 propagated. + + # CC for build script linking. + # Workaround: should be `pkgsHostHost.cc` but `stdenv`'s cc itself have -1 offset. + depsHostHostPropagated = [ stdenv.cc ]; + # CC for crate linking. + # Workaround: should be `pkgsHostTarget.cc` but `stdenv`'s cc itself have -1 offset. + propagatedBuildInputs = [ pkgsTargetTarget.stdenv.cc ]; + + # Link dependency for target, required by darwin std. + depsTargetTargetPropagated = + optional (stdenv.targetPlatform.isDarwin) [ pkgsTargetTarget.libiconv ]; + + postBuild = '' + # If rustc or rustdoc is in the derivation, we need to copy their + # executable into the final derivation. This is required + # for making them find the correct SYSROOT. + for target in $out/bin/{rustc,rustdoc,miri,cargo-miri}; do + if [ -e $target ]; then + cp --remove-destination "$(realpath -e $target)" $target + fi + done + + if [ -e $out/bin/cargo-miri ]; then + mv $out/bin/{cargo-miri,.cargo-miri-wrapped} + cp -f ${./cargo-miri-wrapper.sh} $out/bin/cargo-miri + chmod +w $out/bin/cargo-miri + substituteInPlace $out/bin/cargo-miri \ + --replace "@bash@" "${bash}/bin/bash" \ + --replace "@cargo_miri@" "$out/bin/.cargo-miri-wrapped" \ + --replace "@out@" "$out" + fi + + # symlinkJoin doesn't automatically handle it. Thus do it manually. + mkdir $out/nix-support + echo "$depsHostHostPropagated " >$out/nix-support/propagated-host-host-deps + echo "$propagatedBuildInputs " >$out/nix-support/propagated-build-inputs + '' + optionalString (stdenv.targetPlatform.isDarwin) '' + echo "$depsTargetTargetPropagated " >$out/nix-support/propagated-target-target-deps + ''; + + meta.platforms = lib.platforms.all; +} diff --git a/mk-component-set.nix b/mk-component-set.nix index 65542d8..3e94a19 100644 --- a/mk-component-set.nix +++ b/mk-component-set.nix @@ -1,5 +1,5 @@ # Define component derivations and special treatments. -{ lib, stdenv, buildPackages, gnutar, gcc, zlib, libiconv, autoPatchelfHook +{ lib, stdenv, gnutar, autoPatchelfHook, zlib , toRustTarget, removeNulls }: # Release version of the whole set. @@ -16,7 +16,7 @@ let inherit (stdenv) hostPlatform targetPlatform; mkComponent = pname: src: - stdenv.mkDerivation { + stdenv.mkDerivation rec { inherit pname version src; name = "${pname}-${version}-${platform}"; @@ -26,18 +26,11 @@ let # entire unpacked contents after just a little twiddling. preferLocalBuild = true; - nativeBuildInputs = [ gnutar autoPatchelfHook ]; - buildInputs = [ zlib ] ++ - # These components link to `librustc_driver-*.so`. - optional (elem pname [ "clippy-preview" "rls-preview" "miri-preview" ]) [ self.rustc ]; - - # Ourselves have offset -1. In order to make these offset -1 dependencies of downstream derivation, - # they are offset 0 propagated. - propagatedBuildInputs = - optional (pname == "rustc") [ stdenv.cc buildPackages.stdenv.cc ]; - # This goes downstream packages' buildInputs. - depsTargetTargetPropagated = - optional (pname == "rustc" && targetPlatform.isDarwin) libiconv; + nativeBuildInputs = [ gnutar ] ++ optional (!dontFixup) autoPatchelfHook; + buildInputs = + optional (elem pname [ "rustc" "cargo" ]) zlib ++ + # These components link to `librustc_driver*.so` or `libLLVM*.so`. + optional (elem pname [ "clippy-preview" "rls-preview" "miri-preview" "rustc-dev" ]) self.rustc; dontConfigure = true; dontBuild = true; @@ -71,8 +64,8 @@ let fi ''; - # `rust-docs` only contains tons of html files. Don't waste time scanning files. - dontFixup = pname == "rust-docs"; + # Only contain tons of html files. Don't waste time scanning files. + dontFixup = elem pname [ "rust-docs" "rustc-docs" ]; dontStrip = true; }; diff --git a/rust-overlay.nix b/rust-overlay.nix index 2fabf7b..fb6dc5a 100644 --- a/rust-overlay.nix +++ b/rust-overlay.nix @@ -1,4 +1,4 @@ -# Define component resolution, component aggregation and utility functions. +# Define component resolution and utility functions. self: super: let @@ -31,6 +31,8 @@ let inherit toRustTarget removeNulls; }; + mkAggregated = self.callPackage ./mk-aggregated.nix {}; + # Manifest selector. selectManifest = { channel, date ? null }: let inherit (self.rust-bin) manifests; @@ -127,37 +129,6 @@ let in self.fetchurl { inherit name sha256; url = url'; }; - aggregateComponents = { pname, version, components }: - self.symlinkJoin { - name = pname + "-" + version; - inherit pname version; - - paths = components; - - postBuild = '' - # If rustc or rustdoc is in the derivation, we need to copy their - # executable into the final derivation. This is required - # for making them find the correct SYSROOT. - for target in $out/bin/{rustc,rustdoc,miri,cargo-miri}; do - if [ -e $target ]; then - cp --remove-destination "$(realpath -e $target)" $target - fi - done - - if [ -e $out/bin/cargo-miri ]; then - mv $out/bin/{cargo-miri,.cargo-miri-wrapped} - cp -f ${./cargo-miri-wrapper.sh} $out/bin/cargo-miri - chmod +w $out/bin/cargo-miri - substituteInPlace $out/bin/cargo-miri \ - --replace "@bash@" "${self.bash}/bin/bash" \ - --replace "@cargo_miri@" "$out/bin/.cargo-miri-wrapped" \ - --replace "@out@" "$out" - fi - ''; - - meta.platforms = self.lib.platforms.all; - }; - # Resolve final components to install from mozilla-overlay style `extensions`, `targets` and `targetExtensions`. # # `componentSet` has a layout of `componentSet.. : Derivation`. @@ -321,7 +292,7 @@ let mkProfile = name: profileComponents: makeOverridable ({ extensions, targets, targetExtensions }: - aggregateComponents { + mkAggregated { pname = "rust-${name}"; version = manifest.version; components = resolveComponents { @@ -410,7 +381,7 @@ let srcs = mapAttrs hashToSrc components; }; in - aggregateComponents { + mkAggregated { inherit pname version; components = attrValues components'; };