From e537acb643a863735ae36d16b77ad92665a66cba Mon Sep 17 00:00:00 2001
From: Tom Prince <tom.prince@private.storage>
Date: Mon, 6 Sep 2021 16:08:40 -0600
Subject: [PATCH] Expose all local packages as new module argument.

---
 morph/lib/base.nix                      |  4 ++++
 nixos/modules/issuer.nix                |  5 ++---
 nixos/modules/packages.nix              |  8 ++++++++
 nixos/modules/private-storage.nix       |  5 +----
 nixos/modules/tests/private-storage.nix | 16 ++++++++--------
 nixos/modules/tests/tahoe.nix           |  9 ++++-----
 nixos/pkgs/default.nix                  | 24 ++++++++++++++++++++++++
 7 files changed, 51 insertions(+), 20 deletions(-)
 create mode 100644 nixos/modules/packages.nix
 create mode 100644 nixos/pkgs/default.nix

diff --git a/morph/lib/base.nix b/morph/lib/base.nix
index 809e3556..66ed55ee 100644
--- a/morph/lib/base.nix
+++ b/morph/lib/base.nix
@@ -19,4 +19,8 @@
       '';
     };
   };
+
+  imports = [
+    ../../nixos/modules/packages.nix
+  ];
 }
diff --git a/nixos/modules/issuer.nix b/nixos/modules/issuer.nix
index 0433c4f0..00e9c0c8 100644
--- a/nixos/modules/issuer.nix
+++ b/nixos/modules/issuer.nix
@@ -1,13 +1,12 @@
 # A NixOS module which can run a Ristretto-based issuer for PrivateStorage
 # ZKAPs.
-{ lib, pkgs, config, ... }: let
+{ lib, pkgs, ourpkgs, config, ... }: let
   cfg = config.services.private-storage-issuer;
-  zkapissuer = pkgs.callPackage ../pkgs/zkapissuer { };
 in {
   options = {
     services.private-storage-issuer.enable = lib.mkEnableOption "PrivateStorage ZKAP Issuer Service";
     services.private-storage-issuer.package = lib.mkOption {
-      default = zkapissuer.components.exes."PaymentServer-exe";
+      default = ourpkgs.zkapissuer.components.exes."PaymentServer-exe";
       type = lib.types.package;
       example = lib.literalExample "pkgs.zkapissuer.components.exes.\"PaymentServer-exe\"";
       description = ''
diff --git a/nixos/modules/packages.nix b/nixos/modules/packages.nix
new file mode 100644
index 00000000..d6518dcf
--- /dev/null
+++ b/nixos/modules/packages.nix
@@ -0,0 +1,8 @@
+# A NixOS module which exposes custom packages to other modules.
+{ pkgs, ...}:
+{
+  config = {
+    # Expose `nixos/pkgs` as a new module argument `ourpkgs`.
+    _module.args.ourpkgs = pkgs.callPackage ../../nixos/pkgs {};
+  };
+}
diff --git a/nixos/modules/private-storage.nix b/nixos/modules/private-storage.nix
index d3bc9e61..c119a3d3 100644
--- a/nixos/modules/private-storage.nix
+++ b/nixos/modules/private-storage.nix
@@ -1,6 +1,6 @@
 # A NixOS module which can instantiate a Tahoe-LAFS storage server in the
 # preferred configuration for the Private Storage grid.
-{ pkgs, lib, config, ... }:
+{ pkgs, ourpkgs, lib, config, ... }:
 let
   # Grab the configuration for this module for convenient access below.
   cfg = config.services.private-storage;
@@ -8,9 +8,6 @@ let
   # TODO: This path copied from tahoe.nix.
   tahoe-base = "/var/db/tahoe-lafs";
 
-  # Our own nixpkgs fork:
-  ourpkgs = import ../../nixpkgs-ps.nix {};
-
   # The full path to the directory where the storage server will write
   # incident reports.
   incidents-dir = "${tahoe-base}/${storage-node-name}/logs/incidents";
diff --git a/nixos/modules/tests/private-storage.nix b/nixos/modules/tests/private-storage.nix
index 3e8009b0..0d5fc75b 100644
--- a/nixos/modules/tests/private-storage.nix
+++ b/nixos/modules/tests/private-storage.nix
@@ -83,21 +83,19 @@ let
       command = builtins.concatStringsSep " " argv;
     in
       "${node}.succeed('set -eo pipefail; ${command} | systemd-cat')";
-
-  pspkgs = import ../../../nixpkgs-ps.nix { };
-
 in {
   # https://nixos.org/nixos/manual/index.html#sec-nixos-tests
   # https://nixos.mayflower.consulting/blog/2019/07/11/leveraging-nixos-tests-in-your-project/
   nodes = rec {
     # Get a machine where we can run a Tahoe-LAFS client node.
     client =
-      { config, pkgs, ... }:
-      { environment.systemPackages = [
+      { config, pkgs, ourpkgs, ... }:
+      { imports = [ ../packages.nix ];
+        environment.systemPackages = [
           pkgs.daemonize
           # A Tahoe-LAFS configuration capable of using the right storage
           # plugin.
-          pspkgs.privatestorage
+          ourpkgs.privatestorage
           # Support for the tests we'll run.
           (pkgs.python3.withPackages (ps: [ ps.requests ps.hyperlink ]))
         ];
@@ -110,7 +108,8 @@ in {
     storage =
       { config, pkgs, ... }:
       { imports =
-        [ ../private-storage.nix
+        [ ../packages.nix
+          ../private-storage.nix
           ../ssh.nix
         ];
         services.private-storage = {
@@ -128,7 +127,8 @@ in {
     issuer =
     { config, pkgs, ... }:
     { imports =
-      [ ../issuer.nix
+      [ ../packages.nix
+        ../issuer.nix
         ../ssh.nix
       ];
       services.private-storage.sshUsers = sshUsers;
diff --git a/nixos/modules/tests/tahoe.nix b/nixos/modules/tests/tahoe.nix
index a582accf..624a9691 100644
--- a/nixos/modules/tests/tahoe.nix
+++ b/nixos/modules/tests/tahoe.nix
@@ -1,15 +1,14 @@
 { ... }:
-  let
-    pspkgs = import ../../../nixpkgs-ps.nix { };
-  in {
+  {
   nodes = {
-    storage = { config, pkgs, ... }: {
+    storage = { config, pkgs, ourpkgs, ... }: {
       imports = [
+        ../packages.nix
         ../tahoe.nix
       ];
 
       services.tahoe.nodes.storage = {
-        package = pspkgs.privatestorage;
+        package = ourpkgs.privatestorage;
         sections = {
           node = {
             nickname = "storage";
diff --git a/nixos/pkgs/default.nix b/nixos/pkgs/default.nix
new file mode 100644
index 00000000..3d534430
--- /dev/null
+++ b/nixos/pkgs/default.nix
@@ -0,0 +1,24 @@
+# Expose all our locally defined packages as attributes.
+# In `gridlib.base`, we expose this as a new `ourpkgs` module argument.
+# To access this directly, you can call this as::
+#
+#    pkgs.callPackage ./nixos/pkgs
+{buildPlatform, hostPlatform, callPackage}:
+let
+  # Our own nixpkgs fork:
+  ourpkgs = import ../../nixpkgs-ps.nix {
+    # Ensure that the fork is configured for the same system
+    # as we were called with.
+    localSystem = buildPlatform;
+    crossSystem = hostPlatform;
+    # Ensure that configuration of the system where this runs
+    # doesn't leak into what we build.
+    # See https://github.com/NixOS/nixpkgs/issues/62513
+    config = {};
+    overlays = [];
+  };
+in
+{
+  zkapissuer = callPackage ./zkapissuer {};
+  inherit (ourpkgs) privatestorage leasereport;
+}
-- 
GitLab