Skip to content

Commit 95df9a5

Browse files
committed
Fix creating a fully static binary
Using `NIX_LDFLAGS` to pass in the `-static` flag does not work, since the CC wrapper decides early on (using the `checkLinkType` helper funtion) if the link type is dynamic or not, only looking at the passed arguments (and response files). Depending on this, it will then pass the `-dynamic-linker` flag to the linker which we need to avoid. We need to pass `-static` directly when invoking the CC wrapper. Fortunately, the `NIX_CFLAGS_LINK` variable already contains this flag (on Linux).
1 parent c823f88 commit 95df9a5

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Diff for: build.sbt

+8-1
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,24 @@ lazy val scalals =
8181
.nativeSettings(
8282
nativeConfig ~= { config =>
8383
val nixCC = sys.env.get("NIX_CC")
84+
8485
val cc = for {
8586
n <- nixCC
8687
c <- sys.env.get("CC")
8788
} yield s"$n/bin/$c"
89+
8890
val cxx = for {
8991
n <- nixCC
9092
c <- sys.env.get("CXX")
9193
} yield s"$n/bin/$c"
9294

95+
val nixCFlagsLink = for {
96+
flags <- sys.env.get("NIX_CFLAGS_LINK").toList
97+
flag <- flags.split(" +") if flag.nonEmpty
98+
} yield flag
99+
93100
config
94-
.withLinkingOptions(List("-fuse-ld=lld"))
101+
.withLinkingOptions("-fuse-ld=lld" +: nixCFlagsLink)
95102
.withClang(cc.fold(config.clang)(Paths.get(_)))
96103
.withClangPP(cxx.fold(config.clangPP)(Paths.get(_)))
97104
},

Diff for: flake.nix

+2-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@
9999
SCALANATIVE_MODE = "release-full"; # {debug, release-fast, release-full}
100100
SCALANATIVE_LTO = "thin"; # {none, full, thin}
101101

102-
buildPhase = lib.optionalString (stdenvStatic.isLinux) ''
103-
NIX_LDFLAGS="-static $NIX_LDFLAGS"
104-
'' + ''
105-
NIX_LDFLAGS="$NIX_LDFLAGS -L${empty-gcc-eh}/lib"
102+
buildPhase = ''
103+
export NIX_LDFLAGS="$NIX_LDFLAGS -L${empty-gcc-eh}/lib"
106104
107105
sbt 'project scalalsNative' 'show nativeConfig' nativeLink
108106
'';

0 commit comments

Comments
 (0)