Skip to content

Commit f8125da

Browse files
committed
feat(nix): Invalidate the maven cache when pom.xml changes
Credits for this approach go to Peter Kolloch. See his blog entry about this idea: https://blog.eigenvalue.net/nix-rerunning-fixed-output-derivations/
1 parent bac167d commit f8125da

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

default.nix

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,22 @@ pkgs.stdenvNoCC.mkDerivation rec {
5757
# Maven needs to download necessary dependencies which is impure because it
5858
# requires network access. Hence, we download all dependencies as a
5959
# fixed-output derivation. This also serves as a nice cache.
60+
#
61+
# We use the hash of the input files to invalidate the maven cache whenever
62+
# the input files change. This is purely for easing maintenance. In case a
63+
# maintainer forgets (or simply doesn't know) to change the output hash to
64+
# force a rebuild (and obtain the new, correct hash) this usually (without
65+
# this naming hack) result in use of a stall cash, essentially behaving like
66+
# an unreproducable derivation where a second build results in a different
67+
# output. By changing the name of the fixed output derivation whenever
68+
# `mavenRepoSrc` changes, we prevent this stall cache as the resulting store
69+
# path will never exist (except when the cache is valid).
70+
mavenRepoSrc = pkgs.lib.sourceByRegex ./. ["^pom.xml$" "^local-maven-repo(/.*)?$"];
71+
mavenRepoSrcName = with pkgs.lib; last (splitString "/" mavenRepoSrc.outPath);
6072
mavenRepo = pkgs.stdenv.mkDerivation {
61-
pname = "${pname}-mavenRepo";
73+
pname = "${pname}-mavenRepo-${mavenRepoSrcName}";
6274
inherit version;
63-
src = pkgs.lib.sourceByRegex ./. ["^pom.xml$" "^local-maven-repo(/.*)?$"];
75+
src = mavenRepoSrc;
6476

6577
nativeBuildInputs = [pkgs.maven];
6678

0 commit comments

Comments
 (0)