summary refs log tree commit diff
path: root/pkgs/build-support/test-equal-derivation.nix
blob: 5d2185ce1652515e20f0f03868d13fe9f3ad009a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{ lib, runCommand, emptyFile, nix-diff }:

/*
  Checks that two packages produce the exact same build instructions.

  This can be used to make sure that a certain difference of configuration,
  such as the presence of an overlay does not cause a cache miss.

  When the derivations are equal, the return value is an empty file.
  Otherwise, the build log explains the difference via `nix-diff`.

  Example:

      testEqualDerivation
        "The hello package must stay the same when enabling checks."
        hello
        (hello.overrideAttrs(o: { doCheck = true; }))

*/
assertion: a: b:
let
  drvA = builtins.unsafeDiscardOutputDependency a.drvPath or (throw "testEqualDerivation second argument must be a package");
  drvB = builtins.unsafeDiscardOutputDependency b.drvPath or (throw "testEqualDerivation third argument must be a package");
  name =
    if a?name
    then lib.strings.sanitizeDerivationName "testEqualDerivation-${a.name}"
    else "testEqualDerivation";
in
if drvA == drvB then
  emptyFile
else
  runCommand name
    {
      inherit assertion drvA drvB;
      nativeBuildInputs = [ nix-diff ];
    } ''
      echo "$assertion"
      echo "However, the derivations differ:"
      echo
      echo nix-diff $drvA $drvB
      nix-diff $drvA $drvB
      exit 1
    ''