From de25b26f3e83d2db9582859bb6f825b91bffcefa Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 21 Jun 2025 22:22:54 +0200 Subject: [PATCH] nix: reduce closure size of default package in flake.nix Splitting the executable in a separate output avoids distributing all of the libraries and documentation, which are not needed when just running PostgREST. Reduces closure size from 4.3G to 73.9M for the flake exported packages. Resolves #4149 --- default.nix | 14 ++++++++++---- flake.nix | 6 +++--- nix/tools/withTools.nix | 5 +++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/default.nix b/default.nix index e56dfbf39..3cb18c1b7 100644 --- a/default.nix +++ b/default.nix @@ -62,8 +62,10 @@ let { name = "postgresql-12"; postgresql = pkgs.postgresql_12.withPackages (p: [ p.postgis p.pg_safeupdate ]); } ]; + haskellPackages = pkgs.haskell.packages."${compiler}"; + # Dynamic derivation for PostgREST - postgrest = pkgs.lib.pipe (pkgs.haskell.packages."${compiler}".callCabal2nix name src { }) [ + postgrest = pkgs.lib.pipe (haskellPackages.callCabal2nix name src { }) [ # To allow ghc-datasize to be used. lib.disableLibraryProfiling # We are never going to use dynamic haskell libraries anyway. "Dynamic" refers to how @@ -84,9 +86,13 @@ rec { # Derivation for the PostgREST Haskell package, including the executable, # libraries and documentation. We disable running the test suite on Nix - # builds, as they require a database to be set up. - postgrestPackage = - lib.dontCheck postgrest; + # builds, as they require a database to be set up. We split the binary + # into a separate output, so that the default distribution via flake.nix + # has a much smaller closure size. + postgrestPackage = pkgs.lib.pipe postgrest [ + lib.dontCheck + lib.enableSeparateBinOutput + ]; # Profiled dynamic executable. postgrestProfiled = pkgs.lib.pipe postgrestPackage [ diff --git a/flake.nix b/flake.nix index 24f4c50f2..16ccc1d80 100644 --- a/flake.nix +++ b/flake.nix @@ -33,8 +33,8 @@ in { packages = genSystems (attrs: { - default = attrs.postgrestPackage; - profiled = attrs.postgrestProfiled; + default = attrs.postgrestPackage.bin; + profiled = attrs.postgrestProfiled.bin; } // nixpkgs.lib.optionalAttrs (attrs ? postgrestStatic) { static = attrs.postgrestStatic; }); @@ -42,7 +42,7 @@ apps = genSystems (attrs: { default = { type = "app"; - program = "${attrs.postgrestStatic or attrs.postgrestPackage}/bin/postgrest"; + program = "${attrs.postgrestStatic or attrs.postgrestPackage.bin}/bin/postgrest"; meta.description = "REST API for any Postgres database"; }; }); diff --git a/nix/tools/withTools.nix b/nix/tools/withTools.nix index f11f92d40..82de1e4d6 100644 --- a/nix/tools/withTools.nix +++ b/nix/tools/withTools.nix @@ -351,12 +351,13 @@ let rm -f result if [ -z "''${PGRST_BUILD_CABAL:-}" ]; then echo -n "Building postgrest (nix)... " - nix-build -A postgrestPackage > "$tmpdir"/build.log 2>&1 || { + # Using lib.getBin to also make this work with older checkouts, where .bin was not a thing, yet. + nix-build -E 'with import ./. {}; pkgs.lib.getBin postgrestPackage' > "$tmpdir"/build.log 2>&1 || { echo "failed, output:" cat "$tmpdir"/build.log exit 1 } - PGRST_CMD=./result/bin/postgrest + PGRST_CMD=$(echo ./result*/bin/postgrest) else echo -n "Building postgrest (cabal)... " postgrest-build