From ea02729ebd784729d5028e5051512c7b3f92ab3e Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sun, 4 Feb 2024 17:37:51 +0100 Subject: [PATCH] nix: Use -split-sections with pkgsStatic to make static executable smaller again This works around https://github.com/NixOS/nixpkgs/issues/286285 to use -split-sections in a cross-compiling scenario. This will reduce the size of the static executable and also remove the remaining references to /nix/store/.. reducing closure size dramatically. This also fixes the docker image blowing up in size since we switched to pkgsStatic. --- default.nix | 10 ++++++++-- nix/split-sections-cross.patch | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 nix/split-sections-cross.patch diff --git a/default.nix b/default.nix index 6cf227b66a..426464a256 100644 --- a/default.nix +++ b/default.nix @@ -28,6 +28,12 @@ let sha256 = nixpkgsVersion.tarballHash; }; + nixpkgs-patched = (import nixpkgs { inherit overlays system; }).applyPatches { + name = "nixpkgs-patched"; + src = nixpkgs; + patches = [ nix/split-sections-cross.patch ]; + }; + allOverlays = import nix/overlays; @@ -46,7 +52,7 @@ let # Evaluated expression of the Nixpkgs repository. pkgs = - import nixpkgs { inherit overlays system; }; + import nixpkgs-patched { inherit overlays system; }; postgresqlVersions = [ @@ -82,7 +88,7 @@ let inherit (pkgs.haskell) lib; in rec { - inherit nixpkgs pkgs; + inherit nixpkgs-patched pkgs; # Derivation for the PostgREST Haskell package, including the executable, # libraries and documentation. We disable running the test suite on Nix diff --git a/nix/split-sections-cross.patch b/nix/split-sections-cross.patch new file mode 100644 index 0000000000..22d1fc59dc --- /dev/null +++ b/nix/split-sections-cross.patch @@ -0,0 +1,14 @@ +This works around https://github.com/NixOS/nixpkgs/issues/286285 by passing --enable-split-sections instead of ghc-options. +--- +--- a/pkgs/development/haskell-modules/generic-builder.nix ++++ b/pkgs/development/haskell-modules/generic-builder.nix +@@ -245,8 +245,7 @@ let + (enableFeature doBenchmark "benchmarks") + "--enable-library-vanilla" # TODO: Should this be configurable? + (enableFeature enableLibraryForGhci "library-for-ghci") +- ] ++ optionals (enableDeadCodeElimination && (lib.versionOlder "8.0.1" ghc.version)) [ +- "--ghc-option=-split-sections" ++ (enableFeature enableDeadCodeElimination "split-sections") + ] ++ optionals dontStrip [ + "--disable-library-stripping" + "--disable-executable-stripping"