1 # Overlay that builds static packages.
3 # Not all packages will build but support is done on a
6 # Note on Darwin/macOS: Apple does not provide a static libc
7 # so any attempts at static binaries are going to be very
10 # Basic things like pkgsStatic.hello should work out of the box. More
11 # complicated things will need to be fixed with overrides.
14 inherit (super.stdenvAdapters) makeStaticBinaries
17 inherit (super.lib) foldl optional flip id composeExtensions optionalAttrs optionalString;
18 inherit (super) makeSetupHook;
20 # Best effort static binaries. Will still be linked to libSystem,
21 # but more portable than Nix store binaries.
22 makeStaticDarwin = stdenv_: let stdenv = stdenv_.override {
23 # extraBuildInputs are dropped in cross.nix, but darwin still needs them
24 extraBuildInputs = [ self.buildPackages.darwin.CF ];
26 mkDerivation = args: stdenv.mkDerivation (args // {
27 NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "")
28 + optionalString (stdenv_.cc.isGNU or false) " -static-libgcc";
29 nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ (makeSetupHook {
31 libsystem = "${stdenv.cc.libc}/lib/libSystem.B.dylib";
33 } ../stdenv/darwin/portable-libsystem.sh) ];
38 # makeStaticDarwin must go first so that the extraBuildInputs
39 # override does not recreate mkDerivation, removing subsequent
41 optional super.stdenv.hostPlatform.isDarwin makeStaticDarwin
43 ++ [ makeStaticLibraries propagateBuildInputs ]
45 # Apple does not provide a static version of libSystem or crt0.o
46 # So we can’t build static binaries without extensive hacks.
47 ++ optional (!super.stdenv.hostPlatform.isDarwin) makeStaticBinaries
49 # Glibc doesn’t come with static runtimes by default.
50 # ++ optional (super.stdenv.hostPlatform.libc == "glibc") ((flip overrideInStdenv) [ self.stdenv.glibc.static ])
53 removeUnknownConfigureFlags = f: with self.lib;
54 remove "--disable-shared"
55 (remove "--enable-static" f);
59 configurePlatforms = [ ];
60 configureFlags = removeUnknownConfigureFlags (o.configureFlags or [ ]);
61 buildInputs = o.buildInputs ++ o.nativeBuildInputs or [ ];
62 propagatedNativeBuildInputs = o.propagatedBuildInputs or [ ];
65 ocamlStaticAdapter = _: super:
67 (_: p: if p ? overrideAttrs then ocamlFixPackage p else p)
70 lablgtk = null; # Currently xlibs cause infinite recursion
71 ocaml = ((super.ocaml.override { useX11 = false; }).overrideAttrs (o: {
72 configurePlatforms = [ ];
73 dontUpdateAutotoolsGnuConfigScripts = true;
74 })).overrideDerivation (o: {
76 configureFlagsArray+=("-cc" "$CC" "-as" "$AS" "-partialld" "$LD -r")
78 configureFlags = (removeUnknownConfigureFlags o.configureFlags) ++ [
80 "-host ${o.stdenv.hostPlatform.config}"
81 "-target ${o.stdenv.targetPlatform.config}"
86 llvmStaticAdapter = llvmPackages:
88 stdenv = foldl (flip id) llvmPackages.stdenv staticAdapters;
89 libcxxStdenv = foldl (flip id) llvmPackages.libcxxStdenv staticAdapters;
93 stdenv = foldl (flip id) super.stdenv staticAdapters;
95 gcc49Stdenv = foldl (flip id) super.gcc49Stdenv staticAdapters;
96 gcc6Stdenv = foldl (flip id) super.gcc6Stdenv staticAdapters;
97 gcc7Stdenv = foldl (flip id) super.gcc7Stdenv staticAdapters;
98 gcc8Stdenv = foldl (flip id) super.gcc8Stdenv staticAdapters;
99 gcc9Stdenv = foldl (flip id) super.gcc9Stdenv staticAdapters;
101 llvmPackages_5 = llvmStaticAdapter super.llvmPackages_5;
102 llvmPackages_6 = llvmStaticAdapter super.llvmPackages_6;
103 llvmPackages_7 = llvmStaticAdapter super.llvmPackages_7;
104 llvmPackages_8 = llvmStaticAdapter super.llvmPackages_8;
105 llvmPackages_9 = llvmStaticAdapter super.llvmPackages_9;
106 llvmPackages_10 = llvmStaticAdapter super.llvmPackages_10;
107 llvmPackages_11 = llvmStaticAdapter super.llvmPackages_11;
108 llvmPackages_12 = llvmStaticAdapter super.llvmPackages_12;
110 boost = super.boost.override {
111 # Don’t use new stdenv for boost because it doesn’t like the
112 # --disable-shared flag
113 stdenv = super.stdenv;
116 curl = super.curl.override {
117 # brotli doesn't build static (Mar. 2021)
118 brotliSupport = false;
119 # disable gss becuase of: undefined reference to `k5_bcmp'
123 ocaml-ng = self.lib.mapAttrs (_: set:
124 if set ? overrideScope' then set.overrideScope' ocamlStaticAdapter else set
127 openssl = super.openssl_1_1.overrideAttrs (o: {
128 # OpenSSL doesn't like the `--enable-static` / `--disable-shared` flags.
129 configureFlags = (removeUnknownConfigureFlags o.configureFlags);
132 perl = super.perl.override {
133 # Don’t use new stdenv zlib because
134 # it doesn’t like the --disable-shared flag
135 stdenv = super.stdenv;
138 zlib = super.zlib.override {
139 # Don’t use new stdenv zlib because
140 # it doesn’t like the --disable-shared flag
141 stdenv = super.stdenv;