From 52255d1825488e09977c3bae628d7d3d1e7fbbc7 Mon Sep 17 00:00:00 2001
From: Tom Prince <tom.prince@private.storage>
Date: Mon, 7 Feb 2022 14:34:18 -0700
Subject: [PATCH] Add a derivation that builds all three grids.

This is the derivation I use when running `nix store diff-closures` for the
weekly nixpkgs update.

The derivation also includes some attributes that are useful for exploring the
various grid configurations in the nix repl.
---
 default.nix       |  5 +++++
 morph/default.nix | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 morph/default.nix

diff --git a/default.nix b/default.nix
index 8b605281..35bd691f 100644
--- a/default.nix
+++ b/default.nix
@@ -11,4 +11,9 @@
   # Run some unit tests of the Nix that ties all of these things together (ie,
   # PrivateStorageio-internal library functionality).
   unit-tests = pkgs.callPackage ./nixos/unit-tests.nix { };
+
+  # Build all grids into a single derivation. The derivation also has several
+  # attributes that are useful for exploring the configuration in a repl or
+  # with eval.
+  morph = pkgs.callPackage ./morph {};
 }
diff --git a/morph/default.nix b/morph/default.nix
new file mode 100644
index 00000000..52cf7d10
--- /dev/null
+++ b/morph/default.nix
@@ -0,0 +1,32 @@
+{ pkgs ? import ../nixpkgs.nix {} }:
+let
+  lib = pkgs.lib;
+  gridlib = import ./lib;
+  inherit (gridlib.pkgs) ourpkgs;
+  grids-path = "${builtins.toString ./.}/grid";
+  grid-configs = lib.mapAttrs (n: v: grids-path + "/${n}/grid.nix") (lib.filterAttrs (n: v: v == "directory") (builtins.readDir grids-path));
+  # It would be useful if morph exposed this as a function.
+  # https://github.com/DBCDK/morph/pull/166
+  morph-eval = networkExpr: (import "${pkgs.morph.lib}/eval-machines.nix") { inherit networkExpr; };
+  grids = lib.mapAttrs (n: v: (morph-eval v)) grid-configs;
+  # Derivation with symlinks to the morph output for each grid.
+  output = pkgs.runCommand "privatestorage-morph"
+    { preferLocalBuild = true; allowSubstitutes = false; passthru = { inherit gridlib ourpkgs grids; }; }
+    ''
+      mkdir $out
+      ${lib.concatStringsSep "\n" (
+      lib.mapAttrsToList (
+        name: morph:
+          let
+            output = morph.machines {
+              # It would be nice if we didn't need to write this data to a file.
+              # https://github.com/DBCDK/morph/pull/186
+              argsFile = pkgs.writeText "args" (builtins.toJSON { Names = lib.attrNames morph.nodes; });
+            };
+          in
+            ''
+              ln -s ${output} $out/${lib.escapeShellArg name}
+            ''
+      ) grids
+    )}'';
+in output
-- 
GitLab