From a0866c8ea4bc66f9aacc7ad19139d57946b3df18 Mon Sep 17 00:00:00 2001
From: regnat <rg@regnat.ovh>
Date: Tue, 16 Mar 2021 13:43:08 +0100
Subject: [PATCH] Make the tests (optionnally) run in another derivation

That way we can run them without rebuilding Nix
---
 flake.nix          | 41 ++++++++++++++++++++++++++++++++++++-----
 tests/common.sh.in |  6 ++++++
 2 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/flake.nix b/flake.nix
index 3e236aaca..c2e5db53a 100644
--- a/flake.nix
+++ b/flake.nix
@@ -144,6 +144,32 @@
             echo "file installer $out/install" >> $out/nix-support/hydra-build-products
           '';
 
+      testNixVersions = pkgs: client: daemon: with commonDeps pkgs; pkgs.stdenv.mkDerivation {
+        NIX_DAEMON_PACKAGE = daemon;
+        NIX_CLIENT_PACKAGE = client;
+        name = "nix-tests-${client.version}-against-${daemon.version}";
+        inherit version;
+
+        src = self;
+
+        VERSION_SUFFIX = versionSuffix;
+
+        nativeBuildInputs = nativeBuildDeps;
+        buildInputs = buildDeps ++ awsDeps;
+        propagatedBuildInputs = propagatedDeps;
+
+        enableParallelBuilding = true;
+
+        dontBuild = true;
+        doInstallCheck = true;
+
+        installPhase = ''
+          mkdir -p $out
+        '';
+        installCheckPhase = "make installcheck";
+
+      };
+
     in {
 
       # A Nixpkgs overlay that overrides the 'nix' and
@@ -153,7 +179,7 @@
         # An older version of Nix to test against when using the daemon.
         # Currently using `nixUnstable` as the stable one doesn't respect
         # `NIX_DAEMON_SOCKET_PATH` which is needed for the tests.
-        mainstream-nix = prev.nixUnstable;
+        nixStable = prev.nix;
 
         nix = with final; with commonDeps pkgs; stdenv.mkDerivation {
           name = "nix-${version}";
@@ -163,8 +189,6 @@
 
           VERSION_SUFFIX = versionSuffix;
 
-          OUTER_NIX = mainstream-nix;
-
           outputs = [ "out" "dev" "doc" ];
 
           nativeBuildInputs = nativeBuildDeps;
@@ -441,6 +465,15 @@
       checks = forAllSystems (system: {
         binaryTarball = self.hydraJobs.binaryTarball.${system};
         perlBindings = self.hydraJobs.perlBindings.${system};
+        installTests =
+          let pkgs = nixpkgsFor.${system}; in
+          pkgs.runCommand "install-tests" {
+            againstSelf = testNixVersions pkgs pkgs.nix pkgs.pkgs.nix;
+            againstCurrentUnstable = testNixVersions pkgs pkgs.nix pkgs.nixUnstable;
+            # Disabled because the latest stable version doesn't handle
+            # `NIX_DAEMON_SOCKET_PATH` which is required for the tests to work
+            # againstLatestStable = testNixVersions pkgs pkgs.nix pkgs.nixStable;
+          } "touch $out";
       });
 
       packages = forAllSystems (system: {
@@ -493,8 +526,6 @@
         stdenv.mkDerivation {
           name = "nix";
 
-          OUTER_NIX = mainstream-nix;
-
           outputs = [ "out" "dev" "doc" ];
 
           nativeBuildInputs = nativeBuildDeps;
diff --git a/tests/common.sh.in b/tests/common.sh.in
index 277dd6dfa..d31d3fbb8 100644
--- a/tests/common.sh.in
+++ b/tests/common.sh.in
@@ -29,6 +29,12 @@ unset XDG_CACHE_HOME
 mkdir -p $TEST_HOME
 
 export PATH=@bindir@:$PATH
+if [[ -n "${NIX_CLIENT_PACKAGE:-}" ]]; then
+  export PATH="$NIX_CLIENT_PACKAGE/bin":$PATH
+fi
+if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
+  export NIX_DAEMON_COMMAND="$NIX_DAEMON_PACKAGE/bin/nix-daemon"
+fi
 coreutils=@coreutils@
 
 export dot=@dot@
-- 
GitLab