Skip to content

Commit de25b26

Browse files
wolfgangwaltherlaurenceisla
authored andcommitted
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
1 parent 67bd352 commit de25b26

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

default.nix

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ let
6262
{ name = "postgresql-12"; postgresql = pkgs.postgresql_12.withPackages (p: [ p.postgis p.pg_safeupdate ]); }
6363
];
6464

65+
haskellPackages = pkgs.haskell.packages."${compiler}";
66+
6567
# Dynamic derivation for PostgREST
66-
postgrest = pkgs.lib.pipe (pkgs.haskell.packages."${compiler}".callCabal2nix name src { }) [
68+
postgrest = pkgs.lib.pipe (haskellPackages.callCabal2nix name src { }) [
6769
# To allow ghc-datasize to be used.
6870
lib.disableLibraryProfiling
6971
# We are never going to use dynamic haskell libraries anyway. "Dynamic" refers to how
@@ -84,9 +86,13 @@ rec {
8486

8587
# Derivation for the PostgREST Haskell package, including the executable,
8688
# libraries and documentation. We disable running the test suite on Nix
87-
# builds, as they require a database to be set up.
88-
postgrestPackage =
89-
lib.dontCheck postgrest;
89+
# builds, as they require a database to be set up. We split the binary
90+
# into a separate output, so that the default distribution via flake.nix
91+
# has a much smaller closure size.
92+
postgrestPackage = pkgs.lib.pipe postgrest [
93+
lib.dontCheck
94+
lib.enableSeparateBinOutput
95+
];
9096

9197
# Profiled dynamic executable.
9298
postgrestProfiled = pkgs.lib.pipe postgrestPackage [

flake.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
in
3434
{
3535
packages = genSystems (attrs: {
36-
default = attrs.postgrestPackage;
37-
profiled = attrs.postgrestProfiled;
36+
default = attrs.postgrestPackage.bin;
37+
profiled = attrs.postgrestProfiled.bin;
3838
} // nixpkgs.lib.optionalAttrs (attrs ? postgrestStatic) {
3939
static = attrs.postgrestStatic;
4040
});
4141

4242
apps = genSystems (attrs: {
4343
default = {
4444
type = "app";
45-
program = "${attrs.postgrestStatic or attrs.postgrestPackage}/bin/postgrest";
45+
program = "${attrs.postgrestStatic or attrs.postgrestPackage.bin}/bin/postgrest";
4646
meta.description = "REST API for any Postgres database";
4747
};
4848
});

nix/tools/withTools.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,13 @@ let
351351
rm -f result
352352
if [ -z "''${PGRST_BUILD_CABAL:-}" ]; then
353353
echo -n "Building postgrest (nix)... "
354-
nix-build -A postgrestPackage > "$tmpdir"/build.log 2>&1 || {
354+
# Using lib.getBin to also make this work with older checkouts, where .bin was not a thing, yet.
355+
nix-build -E 'with import ./. {}; pkgs.lib.getBin postgrestPackage' > "$tmpdir"/build.log 2>&1 || {
355356
echo "failed, output:"
356357
cat "$tmpdir"/build.log
357358
exit 1
358359
}
359-
PGRST_CMD=./result/bin/postgrest
360+
PGRST_CMD=$(echo ./result*/bin/postgrest)
360361
else
361362
echo -n "Building postgrest (cabal)... "
362363
postgrest-build

0 commit comments

Comments
 (0)