hswaw/laserproxy: limit nix rebuilds

Change-Id: I6d8208b46524adf6542a1164910f3b7818f47910
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1168
Reviewed-by: q3k <q3k@hackerspace.pl>
diff --git a/hswaw/laserproxy/default.nix b/hswaw/laserproxy/default.nix
index f048ef4..d6f48f1 100644
--- a/hswaw/laserproxy/default.nix
+++ b/hswaw/laserproxy/default.nix
@@ -1,4 +1,4 @@
-{ hscloud, pkgs, ... }:
+{ hscloud, lib, pkgs, ... }:
 
 # This supports building laserproxy in nix, which is used for deploying it to customs.
 # This is mildly hacky and should be reworked to function for any hscloud bazel target
@@ -9,7 +9,32 @@
 
 pkgs.buildBazelPackage rec {
   name = "laserproxy";
-  src = hscloud.root;
+
+  # Cleanup source tree to limit impact of unrelated changes to hscloud source
+  # tree on nix build cache. This should only pass over:
+  #   //BUILD, //WORKSPACE, //.bazelrc
+  #   //hscloud/go/**
+  #   //hscloud/third_party/**
+  #   //hscloud/devtools/gerrit/**  (WORKSPACE reference)
+  #   //hswaw/laserproxy/**         (our build target)
+  src = lib.cleanSourceWith {
+    filter = name: type: (
+      lib.strings.hasInfix "/go" name ||
+      lib.strings.hasInfix "/third_party" name ||
+
+      lib.strings.hasSuffix "/devtools" name ||
+      lib.strings.hasInfix "/devtools/gerrit" name ||
+
+      lib.strings.hasSuffix "/hswaw" name ||
+      lib.strings.hasInfix "/hswaw/laserproxy" name ||
+
+      lib.strings.hasSuffix "/BUILD" name ||
+      lib.strings.hasSuffix "/WORKSPACE" name ||
+      lib.strings.hasSuffix "/.bazelrc" name
+    );
+    src = hscloud.root;
+  };
+
   bazelTarget = "//hswaw/laserproxy";
   nativeBuildInputs = with pkgs; [
     git python3 postgresql go