Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget, targetPackages 2, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget, llvmPackages 3, fetchurl, file, python3 4, darwin, cargo, cmake, rust, rustc 5, pkg-config, openssl, xz 6, libiconv 7, which, libffi 8, withBundledLLVM ? false 9, enableRustcDev ? true 10, version 11, sha256 12, patches ? [] 13, fd 14, ripgrep 15, wezterm 16, firefox 17, thunderbird 18}: 19 20let 21 inherit (lib) optionals optional optionalString concatStringsSep; 22 inherit (darwin.apple_sdk.frameworks) Security; 23in stdenv.mkDerivation rec { 24 pname = "${targetPackages.stdenv.cc.targetPrefix}rustc"; 25 inherit version; 26 27 src = fetchurl { 28 url = "https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz"; 29 inherit sha256; 30 }; 31 32 __darwinAllowLocalNetworking = true; 33 34 # rustc complains about modified source files otherwise 35 dontUpdateAutotoolsGnuConfigScripts = true; 36 37 # Running the default `strip -S` command on Darwin corrupts the 38 # .rlib files in "lib/". 39 # 40 # See https://github.com/NixOS/nixpkgs/pull/34227 41 # 42 # Running `strip -S` when cross compiling can harm the cross rlibs. 43 # See: https://github.com/NixOS/nixpkgs/pull/56540#issuecomment-471624656 44 stripDebugList = [ "bin" ]; 45 46 # The Rust pkg-config crate does not support prefixed pkg-config executables[1], 47 # but it does support checking these idiosyncratic PKG_CONFIG_${TRIPLE} 48 # environment variables. 49 # [1]: https://github.com/rust-lang/pkg-config-rs/issues/53 50 "PKG_CONFIG_${builtins.replaceStrings ["-"] ["_"] (rust.toRustTarget stdenv.buildPlatform)}" = 51 "${pkgsBuildHost.stdenv.cc.targetPrefix}pkg-config"; 52 53 NIX_LDFLAGS = toString ( 54 # when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch' 55 optional (stdenv.isLinux && !withBundledLLVM) "--push-state --as-needed -lstdc++ --pop-state" 56 ++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++" 57 ++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib"); 58 59 # Increase codegen units to introduce parallelism within the compiler. 60 RUSTFLAGS = "-Ccodegen-units=10"; 61 62 # We need rust to build rust. If we don't provide it, configure will try to download it. 63 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py 64 configureFlags = let 65 setBuild = "--set=target.${rust.toRustTarget stdenv.buildPlatform}"; 66 setHost = "--set=target.${rust.toRustTarget stdenv.hostPlatform}"; 67 setTarget = "--set=target.${rust.toRustTarget stdenv.targetPlatform}"; 68 ccForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc"; 69 cxxForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++"; 70 ccForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}cc"; 71 cxxForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}c++"; 72 ccForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}cc"; 73 cxxForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}c++"; 74 in [ 75 "--release-channel=stable" 76 "--set=build.rustc=${rustc}/bin/rustc" 77 "--set=build.cargo=${cargo}/bin/cargo" 78 "--enable-rpath" 79 "--enable-vendor" 80 "--build=${rust.toRustTargetSpec stdenv.buildPlatform}" 81 "--host=${rust.toRustTargetSpec stdenv.hostPlatform}" 82 # std is built for all platforms in --target. 83 "--target=${concatStringsSep "," ([ 84 (rust.toRustTargetSpec stdenv.targetPlatform) 85 86 # (build!=target): When cross-building a compiler we need to add 87 # the build platform as well so rustc can compile build.rs 88 # scripts. 89 ] ++ optionals (stdenv.buildPlatform != stdenv.targetPlatform) [ 90 (rust.toRustTargetSpec stdenv.buildPlatform) 91 92 # (host!=target): When building a cross-targeting compiler we 93 # need to add the host platform as well so rustc can compile 94 # build.rs scripts. 95 ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform) [ 96 (rust.toRustTargetSpec stdenv.hostPlatform) 97 ])}" 98 99 "${setBuild}.cc=${ccForBuild}" 100 "${setHost}.cc=${ccForHost}" 101 "${setTarget}.cc=${ccForTarget}" 102 103 "${setBuild}.linker=${ccForBuild}" 104 "${setHost}.linker=${ccForHost}" 105 "${setTarget}.linker=${ccForTarget}" 106 107 "${setBuild}.cxx=${cxxForBuild}" 108 "${setHost}.cxx=${cxxForHost}" 109 "${setTarget}.cxx=${cxxForTarget}" 110 111 "${setBuild}.crt-static=${lib.boolToString stdenv.buildPlatform.isStatic}" 112 "${setHost}.crt-static=${lib.boolToString stdenv.hostPlatform.isStatic}" 113 "${setTarget}.crt-static=${lib.boolToString stdenv.targetPlatform.isStatic}" 114 ] ++ optionals (!withBundledLLVM) [ 115 "--enable-llvm-link-shared" 116 "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config" 117 "${setHost}.llvm-config=${llvmSharedForHost.dev}/bin/llvm-config" 118 "${setTarget}.llvm-config=${llvmSharedForTarget.dev}/bin/llvm-config" 119 ] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [ 120 "--enable-profiler" # build libprofiler_builtins 121 ] ++ optionals stdenv.buildPlatform.isMusl [ 122 "${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}" 123 ] ++ optionals stdenv.hostPlatform.isMusl [ 124 "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}" 125 ] ++ optionals stdenv.targetPlatform.isMusl [ 126 "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}" 127 ] ++ optionals (rust.IsNoStdTarget stdenv.targetPlatform) [ 128 "--disable-docs" 129 ] ++ optionals (stdenv.isDarwin && stdenv.isx86_64) [ 130 # https://github.com/rust-lang/rust/issues/92173 131 "--set rust.jemalloc" 132 ]; 133 134 # The bootstrap.py will generated a Makefile that then executes the build. 135 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass 136 # to the bootstrap builder. 137 postConfigure = '' 138 substituteInPlace Makefile \ 139 --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)' 140 ''; 141 142 # the rust build system complains that nix alters the checksums 143 dontFixLibtool = true; 144 145 inherit patches; 146 147 postPatch = '' 148 patchShebangs src/etc 149 150 ${optionalString (!withBundledLLVM) "rm -rf src/llvm"} 151 152 # Fix the configure script to not require curl as we won't use it 153 sed -i configure \ 154 -e '/probe_need CFG_CURL curl/d' 155 156 # Useful debugging parameter 157 # export VERBOSE=1 158 '' + lib.optionalString (stdenv.targetPlatform.isMusl && !stdenv.targetPlatform.isStatic) '' 159 # Upstream rustc still assumes that musl = static[1]. The fix for 160 # this is to disable crt-static by default for non-static musl 161 # targets. 162 # 163 # Even though Cargo will build build.rs files for the build platform, 164 # cross-compiling _from_ musl appears to work fine, so we only need 165 # to do this when rustc's target platform is dynamically linked musl. 166 # 167 # [1]: https://github.com/rust-lang/compiler-team/issues/422 168 substituteInPlace compiler/rustc_target/src/spec/linux_musl_base.rs \ 169 --replace "base.crt_static_default = true" "base.crt_static_default = false" 170 '' + lib.optionalString (stdenv.isDarwin && stdenv.isx86_64) '' 171 # See https://github.com/jemalloc/jemalloc/issues/1997 172 # Using a value of 48 should work on both emulated and native x86_64-darwin. 173 export JEMALLOC_SYS_WITH_LG_VADDR=48 174 ''; 175 176 # rustc unfortunately needs cmake to compile llvm-rt but doesn't 177 # use it for the normal build. This disables cmake in Nix. 178 dontUseCmakeConfigure = true; 179 180 depsBuildBuild = [ pkgsBuildHost.stdenv.cc pkg-config ]; 181 182 nativeBuildInputs = [ 183 file python3 rustc cmake 184 which libffi removeReferencesTo pkg-config xz 185 ]; 186 187 buildInputs = [ openssl ] 188 ++ optionals stdenv.isDarwin [ libiconv Security ] 189 ++ optional (!withBundledLLVM) llvmShared; 190 191 outputs = [ "out" "man" "doc" ]; 192 setOutputFlags = false; 193 194 postInstall = lib.optionalString enableRustcDev '' 195 # install rustc-dev components. Necessary to build rls, clippy... 196 python x.py dist rustc-dev 197 tar xf build/dist/rustc-dev*tar.gz 198 cp -r rustc-dev*/rustc-dev*/lib/* $out/lib/ 199 rm $out/lib/rustlib/install.log 200 for m in $out/lib/rustlib/manifest-rust* 201 do 202 sort --output=$m < $m 203 done 204 205 '' + '' 206 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so 207 # and thus a transitive dependency on ncurses 208 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+' 209 210 # remove uninstall script that doesn't really make sense for Nix. 211 rm $out/lib/rustlib/uninstall.sh 212 ''; 213 214 configurePlatforms = []; 215 216 enableParallelBuilding = true; 217 218 setupHooks = ./setup-hook.sh; 219 220 requiredSystemFeatures = [ "big-parallel" ]; 221 222 passthru = { 223 llvm = llvmShared; 224 inherit llvmPackages; 225 tests = { 226 inherit fd ripgrep wezterm; 227 } // lib.optionalAttrs stdenv.hostPlatform.isLinux { inherit firefox thunderbird; }; 228 }; 229 230 meta = with lib; { 231 homepage = "https://www.rust-lang.org/"; 232 description = "A safe, concurrent, practical language"; 233 maintainers = with maintainers; [ cstrahan globin havvy ] ++ teams.rust.members; 234 license = [ licenses.mit licenses.asl20 ]; 235 platforms = [ 236 # Platforms with host tools from 237 # https://doc.rust-lang.org/nightly/rustc/platform-support.html 238 "x86_64-darwin" "i686-darwin" "aarch64-darwin" 239 "i686-freebsd13" "x86_64-freebsd13" 240 "x86_64-solaris" 241 "aarch64-linux" "armv7l-linux" "i686-linux" "mipsel-linux" 242 "mips64el-linux" "powerpc64-linux" "powerpc64le-linux" 243 "riscv64-linux" "s390x-linux" "x86_64-linux" 244 "aarch64-netbsd" "armv7l-netbsd" "i686-netbsd" "powerpc-netbsd" 245 "x86_64-netbsd" 246 "i686-openbsd" "x86_64-openbsd" 247 "i686-windows" "x86_64-windows" 248 ]; 249 }; 250}