From 5637253811a092f6fa9b92556b7d53d6b31b94e9 Mon Sep 17 00:00:00 2001
From: Tom Prince <tom.prince@private.storage>
Date: Mon, 17 Jan 2022 12:19:08 -0700
Subject: [PATCH] Speed up evaluation of grids by evaluating custom packages
 once.

The current code evaluates our custom packages once for each node, which adds
signifcant amount of time to evaluate a grid. We can reduce this, by adding the
custom package set as an attribute to the nixpkgs set we pass to morph.

This doesn't change how we refer to those packages, as we continue to expose the
custom package set as a module attribute.

These are the times to evaluate all three grids (on a partially loaded system),
when there was nothing new to build:

```
before:
real	2m27.837s
user	3m35.528s
sys	0m3.722s

after:
real	1m12.748s
user	1m34.047s
sys	0m3.346s
```
---
 morph/lib/default.nix      |  9 ++++++++-
 nixos/modules/packages.nix |  4 +++-
 nixos/system-tests.nix     | 12 ++++++++----
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/morph/lib/default.nix b/morph/lib/default.nix
index c43fa6ea..f236b8ca 100644
--- a/morph/lib/default.nix
+++ b/morph/lib/default.nix
@@ -26,6 +26,13 @@
         "megacli"
       ];
     };
-    overlays = [];
+    # Expose `nixos/pkgs` as an attribute of our package set.
+    # This is is primarly consumed by `nixos/modules/packages.nix`, which
+    # then exposes it as a module argument. We do this here, so that
+    # the package set only needs to be evaluted once for the grid, rather
+    # than once for each host.
+    overlays = [
+      (self: super: { ourpkgs = self.callPackage ../../nixos/pkgs {}; })
+    ];
   };
 }
diff --git a/nixos/modules/packages.nix b/nixos/modules/packages.nix
index c4390dc0..5879e6d6 100644
--- a/nixos/modules/packages.nix
+++ b/nixos/modules/packages.nix
@@ -1,7 +1,9 @@
 # A NixOS module which exposes custom packages to other modules.
 { pkgs, ...}:
 let
-  ourpkgs = pkgs.callPackage ../../nixos/pkgs {};
+  # Get our custom packages; either from the nixpkgs attribute added via an
+  # overlay in `morph/lib/default.nix`, or by importing them directly.
+  ourpkgs = pkgs.ourpkgs or (pkgs.callPackage ../pkgs {});
 in {
   config = {
     # Expose `nixos/pkgs` as a new module argument `ourpkgs`.
diff --git a/nixos/system-tests.nix b/nixos/system-tests.nix
index 218132fe..819b5c73 100644
--- a/nixos/system-tests.nix
+++ b/nixos/system-tests.nix
@@ -1,7 +1,11 @@
 # The overall system test suite for PrivateStorageio NixOS configuration.
 { pkgs }:
-{
-  private-storage = pkgs.nixosTest ./tests/private-storage.nix;
-  spending = pkgs.nixosTest ./tests/spending.nix;
-  tahoe = pkgs.nixosTest ./tests/tahoe.nix;
+let
+  # Add custom packages as an attribute, so it they only need to be evalutated once.
+  # See the comment in `morph/lib/default.nix` for details.
+  pkgs' = pkgs.extend (self: super: { ourpkgs = self.callPackage ./pkgs {}; });
+in {
+  private-storage = pkgs'.nixosTest ./tests/private-storage.nix;
+  spending = pkgs'.nixosTest ./tests/spending.nix;
+  tahoe = pkgs'.nixosTest ./tests/tahoe.nix;
 }
-- 
GitLab