Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc with musl target #71195

Closed
nmattia opened this issue Oct 15, 2019 · 32 comments
Closed

rustc with musl target #71195

nmattia opened this issue Oct 15, 2019 · 32 comments
Labels
0.kind: enhancement Add something new 6.topic: musl Running or building packages with musl libc 6.topic: rust 6.topic: static

Comments

@nmattia
Copy link
Contributor

nmattia commented Oct 15, 2019

Project description

I would love to have a rustc version that compiles static binaries on Linux.

I've hacked one together:

commit 67cf46e3a914f9d89a6b367e261b3d33ecbf5dc5
Author: Nicolas Mattia <[email protected]>
Date:   Tue Oct 15 17:25:55 2019 +0200

    wip

diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix
index 430d22f7653..069fe08c6da 100644
--- a/pkgs/development/compilers/rust/rustc.nix
+++ b/pkgs/development/compilers/rust/rustc.nix
@@ -4,6 +4,9 @@
 , pkgconfig, openssl
 , which, libffi
 , withBundledLLVM ? false
+, pkgsStatic
+, musl
+, runCommand
 }:
 
 let
@@ -51,41 +54,45 @@ in stdenv.mkDerivation rec {
   # We need rust to build rust. If we don't provide it, configure will try to download it.
   # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py
   configureFlags = let
-    setBuild  = "--set=target.${stdenv.buildPlatform.config}";
-    setHost   = "--set=target.${stdenv.hostPlatform.config}";
-    setTarget = "--set=target.${stdenv.targetPlatform.config}";
-    ccForBuild  = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc";
-    cxxForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++";
-    ccForHost  = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}cc";
-    cxxForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}c++";
-    ccForTarget  = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}cc";
-    cxxForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}c++";
+    ccGlibc  = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc";
+    cxxGlibc = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++";
+
+    # the musl-enabled GCC and G++
+    ccMusl  = "${pkgsStatic.stdenv.cc}/bin/${pkgsStatic.stdenv.cc.targetPrefix}cc";
+    cxxMusl = "${pkgsStatic.stdenv.cc}/bin/${pkgsStatic.stdenv.cc.targetPrefix}c++";
+    muslRoot =
+      let libunwind = pkgsStatic.llvmPackages_9.libunwind.override
+            { enableShared = false; };
+      in
+      runCommand "musl-root" {}
+    ''
+      mkdir -p $out
+      cp -r ${musl}/* $out
+      chmod +w $out/lib
+      cp ${libunwind}/lib/* $out/lib
+    '';
   in [
     "--release-channel=stable"
     "--set=build.rustc=${rustPlatform.rust.rustc}/bin/rustc"
     "--set=build.cargo=${rustPlatform.rust.cargo}/bin/cargo"
     "--enable-rpath"
     "--enable-vendor"
-    "--build=${stdenv.buildPlatform.config}"
-    "--host=${stdenv.hostPlatform.config}"
-    "--target=${stdenv.targetPlatform.config}"
+    "--build=x86_64-unknown-linux-gnu"
+    "--host=x86_64-unknown-linux-gnu"
+    "--target=x86_64-unknown-linux-musl"
 
-    "${setBuild}.cc=${ccForBuild}"
-    "${setHost}.cc=${ccForHost}"
-    "${setTarget}.cc=${ccForTarget}"
+    "--set=target.x86_64-unknown-linux-gnu.cc=${ccGlibc}"
+    "--set=target.x86_64-unknown-linux-gnu.linker=${ccGlibc}"
+    "--set=target.x86_64-unknown-linux-gnu.cxx=${cxxGlibc}"
 
-    "${setBuild}.linker=${ccForBuild}"
-    "${setHost}.linker=${ccForHost}"
-    "${setTarget}.linker=${ccForTarget}"
+    "--set=target.x86_64-unknown-linux-musl.cc=${ccMusl}"
+    "--set=target.x86_64-unknown-linux-musl.linker=${ccMusl}"
+    "--set=target.x86_64-unknown-linux-musl.cxx=${cxxMusl}"
 
-    "${setBuild}.cxx=${cxxForBuild}"
-    "${setHost}.cxx=${cxxForHost}"
-    "${setTarget}.cxx=${cxxForTarget}"
+    "--set=target.x86_64-unknown-linux-musl.musl-root=${muslRoot}"
   ] ++ optional (!withBundledLLVM) [
     "--enable-llvm-link-shared"
-    "${setBuild}.llvm-config=${llvmSharedForBuild}/bin/llvm-config"
-    "${setHost}.llvm-config=${llvmSharedForHost}/bin/llvm-config"
-    "${setTarget}.llvm-config=${llvmSharedForTarget}/bin/llvm-config"
+    "--set=target.x86_64-unknown-linux-gnu.llvm-config=${llvmSharedForBuild}/bin/llvm-config"
   ] ++ optional stdenv.isLinux [
     "--enable-profiler" # build libprofiler_builtins
   ];
@@ -132,7 +139,7 @@ in stdenv.mkDerivation rec {
 
   # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so
   # and thus a transitive dependency on ncurses
-  postInstall = ''
+  postInstall = optionalString (!withBundledLLVM) ''
     find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+'
   '';
 

The idea is that I really, really don't want to go through the pkgsCross infrastructure as this involves using a regular rustc (pkgsBuild) but then build two static rustcs (pkgsHost + pkgsTarget) when using pkgsStatic (not even sure this is possible). Instead I'd rather just instruct the rustc build system to build an extra target (i.e. stdlib) targetting musl. The above works well (modulo the withBundledLLVM which I've always set to true so far).

Would love to hear your thoughts @dtzWill @matthewbauer .

@nmattia nmattia added the 0.kind: packaging request Request for a new package to be added label Oct 15, 2019
@dingxiangfei2009
Copy link
Contributor

dingxiangfei2009 commented Oct 18, 2019

If you are looking at cross-compilating with rustc, maybe you can check out https://github.com/tenx-tech/cargo2nix

Although, one major blocker with compiling musl binaries is that openssl on musl stops building at the moment, and it unfortunately prevents any meaningful networking application from being built.

@veprbl veprbl added 6.topic: rust 0.kind: enhancement Add something new 6.topic: static and removed 0.kind: packaging request Request for a new package to be added labels Oct 20, 2019
@andrew-d
Copy link
Contributor

andrew-d commented Nov 5, 2019

FWIW, I just ran into this when investigating moving the firecracker derivation to be build-from-source. I can build the x86_64-unknown-linux-gnu target just fine, but the upstream project recommends using the x86_64-unknown-linux-musl target (and this is important since it provides its' own "jailer" that doesn't know about Nix).

It'd be convenient if I could just give target = "x86_64-unknown-linux-musl"; to rustPlatform.buildRustPackage, but that doesn't work today.

@nh2
Copy link
Contributor

nh2 commented Nov 28, 2019

I got triggered into working on this, or at least a relevant part of it. I'm making pkgsMusl.rustc work.

I managed to build static GUI apps with GTK (nh2/static-haskell-nix#50 (comment)), and next want to build static Rust GUI apps.

Of course I'll publish/upstream what I currently have in my git tree, but for the impatient and my own record, I needed

export LD_LIBRARY_PATH="${stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH"

got get around the libgcc_s.so error, and commit 14ee26c#diff-fb2bc02945c6ee61a9ea9fd6aeddd789 which brings upstream rustc commit rust-lang/rust@5941acd in which fixes the couldn't find libunwind.a in musl dir error, using llvmPackages_9.libraries.libunwind.

I also had to add the bootstrap compiler download to

else if stdenv.hostPlatform.system == "x86_64-linux"
then "x86_64-unknown-linux-gnu"
.

I'm currently at

cargo:warning=/nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/include/c++/8.3.0/cmath:45:15: fatal error: math.h: No such file or directory
cargo:warning= #include_next <math.h>

@nh2
Copy link
Contributor

nh2 commented Nov 28, 2019

Also super relevant are the Alpine package description and its patches: https://git.alpinelinux.org/aports/tree/community/rust?id=351e828ddb447e649acfc38775596d290f92cb05

@nh2
Copy link
Contributor

nh2 commented Nov 29, 2019

Full output of my current blocker:

   Compiling rustc_save_analysis v0.0.0 (/build/rustc-1.39.0-src/src/librustc_save_analysis)
   Compiling rustc-main v0.0.0 (/build/rustc-1.39.0-src/src/rustc)
    Finished release [optimized] target(s) in 8m 01s
Copying stage0 rustc from stage0 (x86_64-unknown-linux-musl -> x86_64-unknown-linux-musl / x86_64-unknown-linux-musl)
Building stage0 codegen artifacts (x86_64-unknown-linux-musl -> x86_64-unknown-linux-musl, llvm)
   Compiling build_helper v0.1.0 (/build/rustc-1.39.0-src/src/build_helper)
   Compiling cc v1.0.35
   Compiling rustc_codegen_llvm v0.0.0 (/build/rustc-1.39.0-src/src/librustc_codegen_llvm)
   Compiling rustc_llvm v0.0.0 (/build/rustc-1.39.0-src/src/librustc_llvm)
error: failed to run custom build command for `rustc_llvm v0.0.0 (/build/rustc-1.39.0-src/src/librustc_llvm)`

Caused by:
  process didn't exit successfully: `/build/rustc-1.39.0-src/build/x86_64-unknown-linux-musl/stage0-codegen/release/build/rustc_llvm-3aa2ad75c0334416/build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=REAL_LIBRARY_PATH_VAR
cargo:rerun-if-env-changed=REAL_LIBRARY_PATH
cargo:rerun-if-changed=/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/bin/llvm-config
cargo:rerun-if-env-changed=LLVM_CONFIG
cargo:rustc-cfg=llvm_component="aarch64"
cargo:rustc-cfg=llvm_component="amdgpu"
cargo:rustc-cfg=llvm_component="arm"
cargo:rustc-cfg=llvm_component="asmparser"
cargo:rustc-cfg=llvm_component="bitreader"
cargo:rustc-cfg=llvm_component="bitwriter"
cargo:rustc-cfg=llvm_component="hexagon"
cargo:rustc-cfg=llvm_component="instrumentation"
cargo:rustc-cfg=llvm_component="ipo"
cargo:rustc-cfg=llvm_component="linker"
cargo:rustc-cfg=llvm_component="lto"
cargo:rustc-cfg=llvm_component="mips"
cargo:rustc-cfg=llvm_component="msp430"
cargo:rustc-cfg=llvm_component="nvptx"
cargo:rustc-cfg=llvm_component="powerpc"
cargo:rustc-cfg=llvm_component="riscv"
cargo:rustc-cfg=llvm_component="sparc"
cargo:rustc-cfg=llvm_component="systemz"
cargo:rustc-cfg=llvm_component="webassembly"
cargo:rustc-cfg=llvm_component="x86"
cargo:rustc-cfg=llvm_has_msp430_asm_parser
cargo:rerun-if-changed-env=LLVM_RUSTLLVM
cargo:rerun-if-changed=../rustllvm/Linker.cpp
cargo:rerun-if-changed=../rustllvm/ArchiveWrapper.cpp
cargo:rerun-if-changed=../rustllvm/PassWrapper.cpp
cargo:rerun-if-changed=../rustllvm/rustllvm.h
cargo:rerun-if-changed=../rustllvm/.editorconfig
cargo:rerun-if-changed=../rustllvm/RustWrapper.cpp
cargo:rerun-if-changed=../rustllvm/README
TARGET = Some("x86_64-unknown-linux-musl")
OPT_LEVEL = Some("2")
HOST = Some("x86_64-unknown-linux-musl")
CXX_x86_64-unknown-linux-musl = Some("/nix/store/k1bj2ldn9572pdf3szq60k7iln45rjwz-gcc-wrapper-8.3.0/bin/c++")
CXXFLAGS_x86_64-unknown-linux-musl = Some("-ffunction-sections -fdata-sections -fPIC -m64")
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,mmx,sse,sse2")
running: "/nix/store/k1bj2ldn9572pdf3szq60k7iln45rjwz-gcc-wrapper-8.3.0/bin/c++" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include" "-std=c++11" "-fno-exceptions" "-D__STDC_CONSTANT_MACROS" "-D__STDC_FORMAT_MACROS" "-D__STDC_LIMIT_MACROS" "-DLLVM_COMPONENT_AARCH64" "-DLLVM_COMPONENT_AMDGPU" "-DLLVM_COMPONENT_ARM" "-DLLVM_COMPONENT_ASMPARSER" "-DLLVM_COMPONENT_BITREADER" "-DLLVM_COMPONENT_BITWRITER" "-DLLVM_COMPONENT_HEXAGON" "-DLLVM_COMPONENT_INSTRUMENTATION" "-DLLVM_COMPONENT_IPO" "-DLLVM_COMPONENT_LINKER" "-DLLVM_COMPONENT_LTO" "-DLLVM_COMPONENT_MIPS" "-DLLVM_COMPONENT_MSP430" "-DLLVM_COMPONENT_NVPTX" "-DLLVM_COMPONENT_POWERPC" "-DLLVM_COMPONENT_RISCV" "-DLLVM_COMPONENT_SPARC" "-DLLVM_COMPONENT_SYSTEMZ" "-DLLVM_COMPONENT_WEBASSEMBLY" "-DLLVM_COMPONENT_X86" "-DNDEBUG" "-o" "/build/rustc-1.39.0-src/build/x86_64-unknown-linux-musl/stage0-codegen/x86_64-unknown-linux-musl/release/build/rustc_llvm-40f65fabdfc6e7e7/out/../rustllvm/PassWrapper.o" "-c" "../rustllvm/PassWrapper.cpp"
cargo:warning=In file included from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/DataTypes.h:28,
cargo:warning=                 from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/Types.h:17,
cargo:warning=                 from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/BitReader.h:22,
cargo:warning=                 from ../rustllvm/rustllvm.h:1,
cargo:warning=                 from ../rustllvm/PassWrapper.cpp:6:
cargo:warning=/nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/include/c++/8.3.0/cmath:45:15: fatal error: math.h: No such file or directory
cargo:warning= #include_next <math.h>
cargo:warning=               ^~~~~~~~
cargo:warning=compilation terminated.
exit code: 1

--- stderr
thread 'main' panicked at '

Internal error occurred: Command "/nix/store/k1bj2ldn9572pdf3szq60k7iln45rjwz-gcc-wrapper-8.3.0/bin/c++" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include" "-std=c++11" "-fno-exceptions" "-D__STDC_CONSTANT_MACROS" "-D__STDC_FORMAT_MACROS" "-D__STDC_LIMIT_MACROS" "-DLLVM_COMPONENT_AARCH64" "-DLLVM_COMPONENT_AMDGPU" "-DLLVM_COMPONENT_ARM" "-DLLVM_COMPONENT_ASMPARSER" "-DLLVM_COMPONENT_BITREADER" "-DLLVM_COMPONENT_BITWRITER" "-DLLVM_COMPONENT_HEXAGON" "-DLLVM_COMPONENT_INSTRUMENTATION" "-DLLVM_COMPONENT_IPO" "-DLLVM_COMPONENT_LINKER" "-DLLVM_COMPONENT_LTO" "-DLLVM_COMPONENT_MIPS" "-DLLVM_COMPONENT_MSP430" "-DLLVM_COMPONENT_NVPTX" "-DLLVM_COMPONENT_POWERPC" "-DLLVM_COMPONENT_RISCV" "-DLLVM_COMPONENT_SPARC" "-DLLVM_COMPONENT_SYSTEMZ" "-DLLVM_COMPONENT_WEBASSEMBLY" "-DLLVM_COMPONENT_X86" "-DNDEBUG" "-o" "/build/rustc-1.39.0-src/build/x86_64-unknown-linux-musl/stage0-codegen/x86_64-unknown-linux-musl/release/build/rustc_llvm-40f65fabdfc6e7e7/out/../rustllvm/PassWrapper.o" "-c" "../rustllvm/PassWrapper.cpp" with args "c++" did not execute successfully (status code exit code: 1).

', /build/rustc-1.39.0-src/vendor/cc/src/lib.rs:2398:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

command did not execute successfully: "/nix/store/21gn0gg7h187wfbw8jb6ywi5id7vi1yw-cargo-bootstrap-1.38.0/bin/cargo" "build" "-Zconfig-profile" "--target" "x86_64-unknown-linux-musl" "-Zbinary-dep-depinfo" "-j" "12" "--release" "--frozen" "--manifest-path" "/build/rustc-1.39.0-src/src/librustc_codegen_llvm/Cargo.toml" "--features" "" "--message-format" "json-render-diagnostics"
expected success, got: exit code: 101
failed to run: /build/rustc-1.39.0-src/build/bootstrap/debug/bootstrap build --jobs 12

@nmattia
Copy link
Contributor Author

nmattia commented Nov 29, 2019

I've given up on rustc as packaged in nixpkgs. I'm using this instead, copied and tweaked from an older version of nixpkgs:

Only in rust/: 1_38_0.nix
Only in rust/: 1_39_0.nix
diff /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/bootstrap.nix rust/bootstrap.nix
1c1
< { stdenv, fetchurl, callPackage, version, hashes }:
---
> { stdenv, fetchurl, callPackage }:
3a4,17
>   # Note: the version MUST be one version prior to the version we're
>   # building
>   version = "1.38.0";
> 
>   # fetch hashes by running `print-hashes.sh 1.38.0`
>   hashes = {
>    i686-unknown-linux-gnu = "41aed8a350e24a0cac1444ed99b3dd24a90bc581dd88cb420c6e547d6b5f57af";
>    x86_64-unknown-linux-gnu = "adda26b3f0609dbfbdc2019da4a20101879b9db2134fae322a4e863a069ec221";
>    armv7-unknown-linux-gnueabihf = "8b1bf1680a61a643d6b5c7a3b1a1ce88448652756395e20ba5846739cbd085c4";
>    aarch64-unknown-linux-gnu = "06afd6d525326cea95c3aa658aaa8542eab26f44235565bb16913ac9d12b7bda";
>    i686-apple-darwin = "cdbf2807774bed350a3af6f41d7f7dd7ceff28777cde310c3ba90033188eb2f8";
>    x86_64-apple-darwin = "bd301b78ddcd5d4553962b115e1dca5436dd3755ed323f86f4485769286a8a5a";
>   };
> 
diff /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/default.nix rust/default.nix
1,6d0
< { rustcVersion
< , rustcSha256
< , bootstrapVersion
< , bootstrapHashes
< , selectRustPackage
< }:
12c6
< , pkgsBuildTarget, pkgsBuildBuild
---
> , path
19c13,14
<     buildRustPackage = callPackage ../../../build-support/rust {
---
>     # XXX: we should probably replace this with naersk
>     buildRustPackage = callPackage (path + /pkgs/build-support/rust) {
22c17
<       fetchcargo = buildPackages.callPackage ../../../build-support/rust/fetchcargo.nix {
---
>       fetchcargo = buildPackages.callPackage (path + /pkgs/build-support/rust/fetchcargo.nix) {
45,48c40
<     prebuilt = callPackage ./bootstrap.nix {
<       version = bootstrapVersion;
<       hashes = bootstrapHashes;
<     };
---
>     prebuilt = callPackage ./bootstrap.nix {};
55c47
<           (selectRustPackage buildPackages).packages.prebuilt);
---
>           buildPackages.rust.packages.prebuilt);
59c51
<       buildRustPackages = (selectRustPackage buildPackages).packages.stable;
---
>       buildRustPackages = buildPackages.rust.packages.stable;
63,65d54
<         version = rustcVersion;
<         sha256 = rustcSha256;
< 
70,72c59,61
<         pkgsBuildBuild = pkgsBuildBuild // { targetPackages.stdenv = llvmPackages_5.stdenv; };
<         pkgsBuildHost = pkgsBuildBuild // { targetPackages.stdenv = llvmPackages_5.stdenv; };
<         pkgsBuildTarget = pkgsBuildTarget // { targetPackages.stdenv = llvmPackages_5.stdenv; };
---
>         #pkgsBuildBuild = pkgsBuildBuild // { targetPackages.stdenv = llvmPackages_5.stdenv; };
>         #pkgsBuildHost = pkgsBuildBuild // { targetPackages.stdenv = llvmPackages_5.stdenv; };
>         #pkgsBuildTarget = pkgsBuildTarget // { targetPackages.stdenv = llvmPackages_5.stdenv; };
Common subdirectories: /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/rls and rust/rls
diff /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/rust-src.nix rust/rust-src.nix
9c9
<     rm -rf $out/{ci,doc,etc,grammar,llvm-project,llvm-emscripten,rtstartup,rustllvm,test,tools,vendor,stdarch}
---
>     rm -rf $out/{ci,doc,driver,etc,grammar,llvm,rt,rtstartup,rustllvm,test,tools,vendor}
diff /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/rustc.nix rust/rustc.nix
1c1,2
< { stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget
---
> { stdenv, removeReferencesTo
> , lib
3c4
< , llvm_9, darwin, git, cmake, rustPlatform
---
> , llvm_8, llvmPackages_8, darwin, git, cmake, rustPlatform
7,8c8,12
< , version
< , sha256
---
> , pkgsStatic
> , pkgs
> , runCommand
> , musl
> , makeWrapper
10a15,23
> # We make some assumptions about the platform below, this is just a sanity
> # check
> assert
>   if stdenv.isLinux then
>     stdenv.buildPlatform.config == "x86_64-unknown-linux-gnu"
>   else
>     stdenv.buildPlatform.config == "x86_64-apple-darwin"
>   ;
> 
11a25,33
>   targets =
>     [
>       "${stdenv.buildPlatform.config}"
>       "wasm32-unknown-unknown"
>     ]
>     # on Linux we also build the musl target for fully static executables
>     ++ lib.optionals stdenv.isLinux [ "x86_64-unknown-linux-musl" ]
>     ;
>   host = stdenv.buildPlatform.config;
15,18d36
<   llvmSharedForBuild = pkgsBuildBuild.llvm_9.override { enableSharedLibraries = true; };
<   llvmSharedForHost = pkgsBuildHost.llvm_9.override { enableSharedLibraries = true; };
<   llvmSharedForTarget = pkgsBuildTarget.llvm_9.override { enableSharedLibraries = true; };
< 
20c38
<   llvmShared = llvm_9.override { enableSharedLibraries = true; };
---
>   llvmShared = llvm_8.override { enableSharedLibraries = true; };
23c41
<   inherit version;
---
>   version = "1.39.0";
27c45
<     inherit sha256;
---
>     sha256 = "0mwkc1bnil2cfyf6nglpvbn2y0zfbv44zfhsd5qg4c9rm6vgd8dl";
48c66
<     ++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib";
---
>     ++ optional stdenv.isDarwin "-rpath ${llvmShared}/lib";
56,64c74,94
<     setBuild  = "--set=target.${stdenv.buildPlatform.config}";
<     setHost   = "--set=target.${stdenv.hostPlatform.config}";
<     setTarget = "--set=target.${stdenv.targetPlatform.config}";
<     ccForBuild  = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc";
<     cxxForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++";
<     ccForHost  = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}cc";
<     cxxForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}c++";
<     ccForTarget  = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}cc";
<     cxxForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}c++";
---
>     ccForBuild  = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
>     cxxForBuild = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
> 
>     # musl-enabled cc, used in linux builds
>     ccMusl  = "${pkgsStatic.stdenv.cc}/bin/${pkgsStatic.stdenv.cc.targetPrefix}cc";
>     cxxMusl = "${pkgsStatic.stdenv.cc}/bin/${pkgsStatic.stdenv.cc.targetPrefix}c++";
>     muslRoot =
>       # the musl-root requires a libunwind.a, so we provide one from llvm
>       # XXX: create ticket about libunwind.a from LLVM breaking because rustc
>       # doesn't link-in libstdc++
>       let libunwind = pkgsStatic.callPackage ../packages/libunwind.nix
>             { enableShared = false; };
>       in
>       runCommand "musl-root" {}
>     ''
>       mkdir -p $out
>       cp -r ${musl}/* $out
>       chmod +w $out/lib
>       cp ${libunwind}/lib/libunwind.a $out/lib/libunwind.a
>     '';
> 
73,86c103
<     "--target=${stdenv.targetPlatform.config}"
< 
<     "${setBuild}.cc=${ccForBuild}"
<     "${setHost}.cc=${ccForHost}"
<     "${setTarget}.cc=${ccForTarget}"
< 
<     "${setBuild}.linker=${ccForBuild}"
<     "${setHost}.linker=${ccForHost}"
<     "${setTarget}.linker=${ccForTarget}"
< 
<     "${setBuild}.cxx=${cxxForBuild}"
<     "${setHost}.cxx=${cxxForHost}"
<     "${setTarget}.cxx=${cxxForTarget}"
<   ] ++ optional (!withBundledLLVM) [
---
>     "--target=${lib.concatStringsSep "," targets}"
88,91c105,117
<     "${setBuild}.llvm-config=${llvmSharedForBuild}/bin/llvm-config"
<     "${setHost}.llvm-config=${llvmSharedForHost}/bin/llvm-config"
<     "${setTarget}.llvm-config=${llvmSharedForTarget}/bin/llvm-config"
<   ] ++ optional stdenv.isLinux [
---
>     "--set=target.${stdenv.buildPlatform.config}.llvm-config=${llvmShared}/bin/llvm-config"
>     ] ++ lib.optionals stdenv.isLinux
>     [
>     "--set=target.x86_64-unknown-linux-gnu.cc=${ccForBuild}"
>     "--set=target.x86_64-unknown-linux-gnu.linker=${ccForBuild}"
>     "--set=target.x86_64-unknown-linux-gnu.cxx=${cxxForBuild}"
> 
>     "--set=target.x86_64-unknown-linux-musl.cc=${ccMusl}"
>     "--set=target.x86_64-unknown-linux-musl.linker=${ccMusl}"
>     "--set=target.x86_64-unknown-linux-musl.cxx=${cxxMusl}"
>     "--set=target.x86_64-unknown-linux-musl.musl-root=${muslRoot}"
> 
> 
93c119,124
<   ];
---
>     ] ++ lib.optionals stdenv.isDarwin
>     [
>     "--set=target.x86_64-apple-darwin.cc=${ccForBuild}"
>     "--set=target.x86_64-apple-darwin.linker=${ccForBuild}"
>     "--set=target.x86_64-apple-darwin.cxx=${cxxForBuild}"
>     ];
125c156
<     which libffi removeReferencesTo pkgconfig
---
>     which libffi removeReferencesTo pkgconfig makeWrapper
136a168
>   # XXX: change path wrapping
138a171,176
> 
>     # rustdoc needs a cc
>     # we wrap it with a new PATH until 1.39:
>     # https://github.com/rust-lang/cargo/issues/7529
>     wrapProgram $out/bin/rustdoc \
>       --prefix PATH ":" "${stdenv.cc}/bin"
diff /Users/nicolas/nixpkgs/pkgs/development/compilers/rust/setup-hook.sh rust/setup-hook.sh
2c2
< if [[ -z ${IN_NIX_SHELL-} && -z ${CARGO_HOME-} ]]; then
---
> if [[ -z $IN_NIX_SHELL && -z $CARGO_HOME ]]; then

with libunwind:

# We need libunwind-9.0.0 because earlier versions link c++ symbols we don't
# have to have to link to
{ fetchurl, stdenv, cmake, enableShared ? true }:
let
  version = "9.0.0";
  fetch = sha256: fetchurl {
    url = "https://releases.llvm.org/${version}/libunwind-${version}.src.tar.xz";
    inherit sha256;
  };
in
stdenv.mkDerivation rec {
  pname = "libunwind";
  inherit version;

  src = fetch "1chd1nz4bscrs6qa7p8sqgk5df86ll0frv0f451vhks2w44qsslp";

  nativeBuildInputs = [ cmake ];

  enableParallelBuilding = true;

  cmakeFlags = stdenv.lib.optional (!enableShared) "-DLIBUNWIND_ENABLE_SHARED=OFF";
}

Hope that helps, let me know if you have any questions.

@nh2
Copy link
Contributor

nh2 commented Nov 30, 2019

Still struggling with the includ error.

$ /nix/store/k1bj2ldn9572pdf3szq60k7iln45rjwz-gcc-wrapper-8.3.0/bin/c++ "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include" "-std=c++11" "-fno-exceptions" "-D__STDC_CONSTANT_MACROS" "-D__STDC_FORMAT_MACROS" "-D__STDC_LIMIT_MACROS" "-DLLVM_COMPONENT_AARCH64" "-DLLVM_COMPONENT_AMDGPU" "-DLLVM_COMPONENT_ARM" "-DLLVM_COMPONENT_ASMPARSER" "-DLLVM_COMPONENT_BITREADER" "-DLLVM_COMPONENT_BITWRITER" "-DLLVM_COMPONENT_HEXAGON" "-DLLVM_COMPONENT_INSTRUMENTATION" "-DLLVM_COMPONENT_IPO" "-DLLVM_COMPONENT_LINKER" "-DLLVM_COMPONENT_LTO" "-DLLVM_COMPONENT_MIPS" "-DLLVM_COMPONENT_MSP430" "-DLLVM_COMPONENT_NVPTX" "-DLLVM_COMPONENT_POWERPC" "-DLLVM_COMPONENT_RISCV" "-DLLVM_COMPONENT_SPARC" "-DLLVM_COMPONENT_SYSTEMZ" "-DLLVM_COMPONENT_WEBASSEMBLY" "-DLLVM_COMPONENT_X86" "-DNDEBUG" "-o" "/build/rustc-1.39.0-src/build/x86_64-unknown-linux-musl/stage0-codegen/x86_64-unknown-linux-musl/release/build/rustc_llvm-40f65fabdfc6e7e7/out/../rustllvm/PassWrapper.o" "-c" "../rustllvm/PassWrapper.cpp" -idirafter asdf -v
...
GNU C++11 (GCC) version 8.3.0 (x86_64-unknown-linux-musl)
	compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.17.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/nix/store/9zg130vcrkz1rfrkjrxrh94wqgzgadr1-file-5.37/include"
ignoring duplicate directory "/nix/store/26n4hxnaw8rkfvw20f3v23cg2wyp32id-python-2.7.16/include"
ignoring duplicate directory "/nix/store/n6rwwc63pzxc03bphc8ac6fdkyxfwpgf-libffi-3.2.1-dev/include"
ignoring duplicate directory "/nix/store/djr3xbw4k3w8nm6qxv884vahfhw2szm5-musl-1.1.22-dev/include"
ignoring duplicate directory "/nix/store/khkpaayqdqzv5ja88jjwsi5zg07xk4jk-openssl-1.1.1d-dev/include"
ignoring duplicate directory "/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include"
ignoring duplicate directory "/nix/store/5cjsn99jwpyfc03y1va0xdhwd12vs7di-ncurses-6.1-20190112-dev/include"
ignoring duplicate directory "/nix/store/kav11lvmpykhr8ikn4r1wzdhzl4330fq-zlib-1.2.11-dev/include"
ignoring nonexistent directory "/nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/../../../../x86_64-unknown-linux-musl/include"
ignoring duplicate directory "/nix/store/djr3xbw4k3w8nm6qxv884vahfhw2szm5-musl-1.1.22-dev/include"
ignoring nonexistent directory "asdf"
ignoring duplicate directory "/nix/store/djr3xbw4k3w8nm6qxv884vahfhw2szm5-musl-1.1.22-dev/include"
ignoring duplicate directory "/nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /nix/store/9zg130vcrkz1rfrkjrxrh94wqgzgadr1-file-5.37/include
 /nix/store/26n4hxnaw8rkfvw20f3v23cg2wyp32id-python-2.7.16/include
 /nix/store/n6rwwc63pzxc03bphc8ac6fdkyxfwpgf-libffi-3.2.1-dev/include
 /nix/store/djr3xbw4k3w8nm6qxv884vahfhw2szm5-musl-1.1.22-dev/include
 /nix/store/khkpaayqdqzv5ja88jjwsi5zg07xk4jk-openssl-1.1.1d-dev/include
 /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include
 /nix/store/5cjsn99jwpyfc03y1va0xdhwd12vs7di-ncurses-6.1-20190112-dev/include
 /nix/store/kav11lvmpykhr8ikn4r1wzdhzl4330fq-zlib-1.2.11-dev/include
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/../../../../include/c++/8.3.0
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/../../../../include/c++/8.3.0/x86_64-unknown-linux-musl
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/../../../../include/c++/8.3.0/backward
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/include
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/include
 /nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/lib/gcc/x86_64-unknown-linux-musl/8.3.0/include-fixed
End of search list.
GNU C++11 (GCC) version 8.3.0 (x86_64-unknown-linux-musl)
	compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.17.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 648f9d22571b7b9c46e5f05e2d3aee6f
In file included from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/DataTypes.h:28,
                 from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/Types.h:17,
                 from /nix/store/77cdrqqd7icjca7y91ivlrj9k9igaf1d-llvm-9.0.0/include/llvm-c/BitReader.h:22,
                 from ../rustllvm/rustllvm.h:1,
                 from ../rustllvm/PassWrapper.cpp:6:
/nix/store/68kld13k9h11lj13zk3il4x6fxncwxkh-gcc-8.3.0/include/c++/8.3.0/cmath:45:15: fatal error: math.h: No such file or directory
 #include_next <math.h>
               ^~~~~~~~
compilation terminated.

@nh2
Copy link
Contributor

nh2 commented Nov 30, 2019

I can't figure out how to add stuff to the end of the search path.

Even my -idirafter asdf is added before the relevant includes.

@nh2
Copy link
Contributor

nh2 commented Nov 30, 2019

I got it to build.

-idirafter on the musl include dir didn't work because the musl path was already a normal -isystem, and the deduplication logic ignored -idirafter in that case.

My mistake was to add musl/musl.dev to buildInputs of rustc in the first place.

I've pushed my WIP to nh2@70e7b40 (part of my hacky gtk3-static branch for now).

@nh2
Copy link
Contributor

nh2 commented Nov 30, 2019

Next obstacle (when building cargo after rustc has built):

error: cannot produce proc-macro for `failure_derive v0.1.5` as the target `x86_64-unknown-linux-musl` does not support these crate types

Which is probably rust-lang/rust#40174.

@nmattia
Copy link
Contributor Author

nmattia commented Nov 30, 2019

@nh2 that's because you're cross compiling rustc itself, which means that the proc macro crates (TH) need to be compiled for that target as well, though unfortunately (AFAIR) they must be dynamically linked (like GHC by default only loading .sos for TH), that's when I gave up

@nh2
Copy link
Contributor

nh2 commented Dec 1, 2019

that's because you're cross compiling rustc itself

@nmattia But I'm not cross compiling (using none of the cross infrastructure), I'm just compiling with musl as my libc.

(I've also clarified with an edit that I have finished building rustc, the next build error is in compiling cargo with the finished rustc.)

My understanding of the situation from the linked issue rust-lang/rust#40174 is that when the musl --target is given, rust compiles only static executables and libraries (not sure why, perhaps they thought that's the main thing you want to do with musl? If yes it that assumption should probably be discard), and proc macro crates need shared libs.

I think the Alpine patches in https://git.alpinelinux.org/aports/tree/community/rust?id=351e828ddb447e649acfc38775596d290f92cb05 handle this, also by introducing a custom targets (e.g. x86_64-alpine-linux-musl in alpine-target.patch). We probably have to do something similar.

@nh2
Copy link
Contributor

nh2 commented Dec 20, 2019

I have commented upstream: rust-lang/rust#58575 (comment)

@nh2
Copy link
Contributor

nh2 commented Apr 24, 2020

I have commented upstream: rust-lang/rust#58575 (comment)

Apparently this is fixed in 1.44.0-beta: rust-lang/rust#40174 (comment)

@stale
Copy link

stale bot commented Oct 21, 2020

Hello, I'm a bot and I thank you in the name of the community for opening this issue.

To help our human contributors focus on the most-relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 180 days, and so I marked it as stale, but you can rest assured it will never be closed by a non-human.

The community would appreciate your effort in checking if the issue is still valid. If it isn't, please close it.

If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me". If you'd like it to get more attention, you can ask for help by searching for maintainers and people that previously touched related code and @ mention them in a comment. You can use Git blame or GitHub's web interface on the relevant files to find them.

Lastly, you can always ask for help at our Discourse Forum or at #nixos' IRC channel.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 21, 2020
@andrew-d
Copy link
Contributor

Still important to me - would love to get this in nixpkgs!

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 21, 2020
@ghost
Copy link

ghost commented Nov 30, 2020

I was able to build rustc 1.47 with musl target without any modifications. I tried to build fd and got a result, but it instantly segfaults on start:

[pbb@mozarella:~/proj/nixpkgs]$ nix build -f . pkgsStatic.fd
[...]

[pbb@mozarella:~/proj/nixpkgs]$ file result/bin/fd                                                                                                                                                                                                                                                    
result/bin/fd: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/vkh9hgiijvxgdp730qw34z9kgwvlq8pk-musl-1.2.0-x86_64-unknown-linux-musl/lib/ld-musl-x86_64.so.1, not stripped

[pbb@mozarella:~/proj/nixpkgs]$ ldd result/bin/fd
statically linked

[pbb@mozarella:~/proj/nixpkgs]$ result/bin/fd
Segmentation fault (core dumped)

[pbb@mozarella:~/proj/nixpkgs]$

@ghost
Copy link

ghost commented Nov 30, 2020

Oh! It works fine when I use rustc 1.45, because it the issue described above is #94228. So we already have a rustc compiler capable of building for musl in nixpkgs, and it is even in the binary cache. Maybe we should add a line to the static overlay to set the default rust version to 1.45 when building pkgsStatic.*.

@ghost
Copy link

ghost commented Nov 30, 2020

This works (using Rust 1.45):

with import <nixpkgs> {}; pkgsStatic.fd.override {
  rustPlatform = makeRustPlatform rustPackages_1_45;
}

And this works as well (using Rust 1.47, but using a different, I guess unwrapped, linker):

with import <nixpkgs> {}; pkgsStatic.fd.overrideAttrs (oA: {
  CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${llvmPackages_10.lld}/bin/lld";
})

@utsl42
Copy link
Contributor

utsl42 commented Nov 30, 2020

I would rather not pin to 1.45. You're on the right track I think, using lld instead of gold. I've fixed one bug with gold in #101666, but I don't think that's the only thing wrong with it. At this point, my suggestion is to disable it entirely for Musl.

@Rizary
Copy link
Contributor

Rizary commented Dec 2, 2020

@petabyteboy do you have any code example on how to use that to make buildRustPackage works for musl target? do we need to compile rustc using musl target? Right now I'm using rustc from nixpkgs-mozilla.

@ghost
Copy link

ghost commented Dec 2, 2020

Yes. The examples above actually use buildRustPackage since the fd derivation I'm overriding is using buildRustPackage. But here's another example with a package that's defined in the example file itself:

with import <nixpkgs> {
  overlays = [
    (self: super: {
      mypackage = self.rustPlatform.buildRustPackage {
        pname = "mypackage";
        version = "1.0.0";
        src = ./.;
        cargoSha256 = "...";
        CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${self.buildPackages.llvmPackages_10.lld}/bin/lld";
      };
    })
  ];
};

pkgsStatic.mypackage

And yes, rustc for the musl target is not in the binary cache, so you'll have to build that locally.
If you really need to use nixpkgs-mozilla, you have to somehow tell it to give you a rust compiler with the x86_64-unknown-linux-musl target. I have no idea how to do that.

By the way: Under the hood pkgsStatic evaluates nixpkgs with a crossSystem parameter. This should be mostly equivalent:

let
  lib = import <nixpkgs/lib>;
  pkgs = import <nixpkgs> {
    crossOverlays = [ (import <nixpkgs/pkgs/top-level/static.nix>) ];
    crossSystem = {
      isStatic = true;
      config = "x86_64-unknown-linux-musl";
    };
    overlays = [
      (self: super: {
        mypackage = self.rustPlatform.buildRustPackage {
          pname = "mypackage";
          version = "1.0.0";
          src = ...;
          cargoSha256 = "...";
          CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${self.buildPackages.llvmPackages_10.lld}/bin/lld";
        };
      })
    ];
  };
in
  pkgs.mypackage

@Rizary
Copy link
Contributor

Rizary commented Dec 3, 2020

@petabyteboy thank you very much!! I've tried with your setup, I get this error:

sl --frozen
   Compiling libc v0.2.80
error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `libc`

To learn more, run the command again with --verbose.

I think I am at rustPackages_1_48. The rustc is compiled actually. Now I am still trying with rustPackages_1_45. In case I'm missing something, if you want to look into the repo,

As for nixpkgs-mozilla, I'll try open issue there.

@ghost
Copy link

ghost commented Dec 3, 2020

There's no need to override the rust compiler or to set the target/TARGET variable in the build environment when you have crossSystem. Also no need to explicitly use prev.pkgsStatic anywhere. All buildInputs will be automatically cross-compiled as static libraries as well. If you still need to change something about libraries to make them compile statically, you can add this to a crossOverlay.

@ghost
Copy link

ghost commented Dec 3, 2020

The issue you are seeing is with the new Rust 1.48 that was merged with staging-next this week.

@Rizary
Copy link
Contributor

Rizary commented Dec 3, 2020

@petabyteboy thank you! so I have use rustPackages_1_45 but still got this error:

error: cannot produce dylib for `rustc_driver v0.0.0 (/tmp/nix-build-rustc-1.45.2-x86_64-unknown-linux-musl.drv-0/rustc-1.45.2-src/src/librustc_driver)` as the target `x86_64-unknown-linux-musl` does not support these crate types
command did not execute successfully: "/nix/store/9n9adic3pfzixw5dcfavv18z257ri62j-cargo-1.45.2/bin/cargo" "build" "--target" "x86_64-unknown-linux-musl" "-Zdual-proc-macros" "-Zbinary-dep-depinfo" "-j" "1" "--release" "--frozen" "--features" " llvm" "--manifest-path" "/tmp/nix-build-rustc-1.45.2-x86_64-unknown-linux-musl.drv-0/rustc-1.45.2-src/src/rustc/Cargo.toml" "--message-format" "json-render-diagnostics"
expected success, got: exit code: 101
failed to run: /tmp/nix-build-rustc-1.45.2-x86_64-unknown-linux-musl.drv-0/rustc-1.45.2-src/build/bootstrap/debug/bootstrap build --jobs 1
Build completed unsuccessfully in 2:26:19
make: *** [Makefile:18: all] Error 1

Now I'm still building the package again (compiling rustc every time I failed is really tedious :p)

I'll let you know the progress.

@ghost
Copy link

ghost commented Dec 3, 2020

Try Rust 1.47 from nixpkgs 24eb3f8

@ghost
Copy link

ghost commented Dec 3, 2020

You can find another example here: https://github.com/petabyteboy/chaostomato/blob/main/default.nix
It uses naersk instead of buildRustPackage, the it might still be useful and the binary cache has the compiler you probably want to use.
The CI for that that repository uses the binary cache at https://chaostomato.cachix.org/. You should be able to reproduce the result using this command:

nix build -f https://github.com/petabyteboy/chaostomato/archive/a3349078b9625d204aee7f9994e019d7f58a1ce5.tar.gz \
  --option trusted-public-keys "chaostomato.cachix.org-1:MtFdKLf8Hkmsih2TNxX8X3mnQdR0Xr8+BHpQoVr1TE8= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" \
  --option substituters "https://chaostomato.cachix.org https://cache.nixos.org/" \
  pkgsStatic.chaostomato

@Rizary
Copy link
Contributor

Rizary commented Dec 6, 2020

Try Rust 1.47 from nixpkgs 24eb3f8

@petabyteboy using rust 1.47 works, thanks. Though I haven't used it to build openssl. Using latest nixpkgs, I got an error in LLVM build.

rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 7, 2020
Problem: Rust doesn't work well with musl on nix, see
NixOS/nixpkgs#71195.

Solution: Build dynamic binaries instead. It's fine since these
binaries aren't used in the releases and used only in our
internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 7, 2020
Problem: Rust doesn't work well with musl on nix, see
NixOS/nixpkgs#71195.

Solution: Build dynamic binaries instead. It's fine since these
binaries aren't used in the releases and used only in our
internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 7, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 7, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 7, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 9, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 11, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
rvem added a commit to serokell/tezos-packaging that referenced this issue Dec 14, 2020
Problem: Rust doesn't work well with musl, see
NixOS/nixpkgs#71195.

Solution: Provide dynamically linked binaries instead. This should be
fine since we don't use them in the releases and they're mostly used in
our internal infrastructure.
@nomeata
Copy link
Contributor

nomeata commented Apr 7, 2021

It’s unclear from the comments if this now simply works, and could be closed?

@tv42
Copy link

tv42 commented May 7, 2021

(Whoops, commented on the non-rust issue first. Deleting that in favor of this)

I don't think this works, at this time.

I would love to see this just work, but for others needing a workaround, this is cribbed together from https://www.reddit.com/r/NixOS/comments/f0yi3b/how_to_build_a_simple_static_rust_binary_using/ and here and works for me on NixOS 20.09. I have a feeling the mozilla overlay is not needed, but I don't know how to do the .rust.override part without it:

{ pkgs ? import <nixpkgs> {} }:

let
  moz_overlay = import (builtins.fetchTarball {
    url = https://github.com/mozilla/nixpkgs-mozilla/archive/8c007b60731c07dd7a052cce508de3bb1ae849b4.tar.gz;
    sha256 = "1zybp62zz0h077zm2zmqs2wcg3whg6jqaah9hcl1gv4x8af4zhs6";
  });
  pkgs = import <nixpkgs> {
    overlays = [ moz_overlay ];
  };
  rustChannel = pkgs.rustChannelOf { };
  rustc = rustChannel.rust.override {
    extensions = [ "rust-src" "rust-analysis" ];
    targets = [ "x86_64-unknown-linux-musl" ];
  };
  rustPlatform = pkgs.makeRustPlatform {
    cargo = rustc;
    rustc = rustc;
  };
in
rustPlatform.buildRustPackage {
  name = "rust-static-nix";
  src = ./.;
  cargoSha256 = "00zv1ndgilza6f6qgqi6bcp264zwvlibzb7dwm39jahk7pzpj3bv";
  target = "x86_64-unknown-linux-musl";
  # https://github.com/NixOS/nixpkgs/issues/71195
  CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.llvmPackages_10.lld}/bin/lld";
}

@symphorien
Copy link
Member

I haven't read all the thread, but I sucessfully cross-compiled a x86_64 linux musl executable with crate2nix.

@tomodachi94 tomodachi94 added the 6.topic: musl Running or building packages with musl libc label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: enhancement Add something new 6.topic: musl Running or building packages with musl libc 6.topic: rust 6.topic: static
Projects
None yet
Development

No branches or pull requests