Skip to content

Commit

Permalink
Speed up 'nix build' by caching Generated.Types modules
Browse files Browse the repository at this point in the history
The Generated.Types only needs to be rebuild when the Application/Schema.sql has changed
  • Loading branch information
mpscholten committed Sep 9, 2024
1 parent 04773f8 commit b5e5cda
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 32 additions & 2 deletions NixSupport/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
, rtsFlags ? ""
, appName ? "app"
, optimizationLevel ? "2"
, filter
}:

let
Expand All @@ -31,13 +32,43 @@ let
jobsBinary = if optimized
then "build/bin/RunJobsOptimized"
else "build/bin/RunJobs";

odir = if optimized then "RunOptimizedProdServer" else "RunUnoptimizedProdServer";

schemaObjectFiles =
let
self = projectPath;
in
pkgs.stdenv.mkDerivation {
name = appName + "-schema";
buildPhase = ''
mkdir -p build/Generated
build-generated-code
export IHP=${ihp}/lib/IHP
ghc -O${if optimized then optimizationLevel else "0"} $(make print-ghc-options) --make build/Generated/Types.hs -odir build/${odir} -hidir build/${odir}
cp -r build $out
'';
src = filter { root = self; include = ["Application/Schema.sql" "Makefile"]; };
nativeBuildInputs =
[ (ghc.ghcWithPackages (p: [ p.ihp-ide ])) # Needed for build-generated-code
]
;
dontInstall = true;
dontFixup = false;
};
in
pkgs.stdenv.mkDerivation {
name = appName;
buildPhase = ''
runHook preBuild
mkdir -p build
mkdir -p build/Generated build/${odir}
cp -r ${schemaObjectFiles}/${odir} build/
cp -r ${schemaObjectFiles}/Generated build/
chmod -R +w build/${odir}/*
# When npm install is executed by the project's makefile it will fail with:
#
Expand Down Expand Up @@ -111,7 +142,6 @@ in
nativeBuildInputs = builtins.concatLists [
[ pkgs.makeWrapper
pkgs.cacert # Needed for npm install to work from within the IHP build process
ghc.ihp-ide # Needed for build-generated-code
]
(if includeDevTools then [(pkgs.postgresql_13.withPackages postgresExtensions)] else [])
];
Expand Down
2 changes: 2 additions & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ ihpFlake:
rtsFlags = cfg.rtsFlags;
optimizationLevel = cfg.optimizationLevel;
appName = cfg.appName;
filter = ihpFlake.inputs.nix-filter.lib;
};

unoptimized-prod-server = import "${ihp}/NixSupport/default.nix" {
Expand All @@ -162,6 +163,7 @@ ihpFlake:
rtsFlags = cfg.rtsFlags;
optimizationLevel = "0";
appName = cfg.appName;
filter = ihpFlake.inputs.nix-filter.lib;
};

unoptimized-docker-image = pkgs.dockerTools.buildImage {
Expand Down

0 comments on commit b5e5cda

Please sign in to comment.