Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 runCommand,
4 srcOnly,
5 hello,
6 emptyDirectory,
7 zlib,
8 git,
9 withCFlags,
10 stdenv,
11 testers,
12}:
13
14let
15 # Extract (effective) arguments passed to stdenv.mkDerivation and compute the
16 # arguments we would need to pass to srcOnly manually in order to get the same
17 # as `srcOnly drv`, i.e. the arguments passed to stdenv.mkDerivation plus the
18 # used stdenv itself.
19 getEquivAttrs =
20 drv:
21 let
22 drv' = drv.overrideAttrs (
23 _finalAttrs: prevAttrs: {
24 passthru = prevAttrs.passthru or { } // {
25 passedAttrs = prevAttrs;
26 };
27 }
28 );
29 in
30 drv'.passedAttrs // { inherit (drv') stdenv; };
31
32 canEvalDrv = drv: (builtins.tryEval drv.drvPath).success;
33
34 emptySrc = srcOnly emptyDirectory;
35 zlibSrc = srcOnly zlib;
36
37 # It can be invoked in a number of ways. Let's make sure they're equivalent.
38 zlibSrcEquiv = srcOnly (getEquivAttrs zlib);
39 # zlibSrcFreeform = # ???;
40 helloSrc = srcOnly hello;
41 helloSrcEquiv = srcOnly (getEquivAttrs hello);
42
43 gitSrc = srcOnly git;
44 gitSrcEquiv = srcOnly (getEquivAttrs git);
45
46 # The srcOnly <drv> invocation leaks a lot of attrs into the srcOnly derivation,
47 # so for comparing with the freeform invocation, we need to make a selection.
48 # Otherwise, we'll be comparing against whatever attribute the fancy hello drv
49 # has.
50 helloDrvSimple = stdenv.mkDerivation {
51 inherit (hello)
52 name
53 pname
54 version
55 src
56 patches
57 ;
58 };
59 helloDrvSimpleSrc = srcOnly helloDrvSimple;
60 helloDrvSimpleSrcFreeform = srcOnly {
61 inherit (helloDrvSimple)
62 name
63 pname
64 version
65 src
66 patches
67 stdenv
68 ;
69 };
70
71 # Test the issue reported in https://github.com/NixOS/nixpkgs/issues/269539
72 stdenvAdapterDrv =
73 let
74 drv = (withCFlags [ "-Werror" "-Wall" ] stdenv).mkDerivation {
75 name = "drv-using-stdenv-adapter";
76 };
77 in
78 # Confirm the issue we are trying to avoid exists
79 assert !(canEvalDrv (srcOnly drv.drvAttrs));
80 drv;
81 stdenvAdapterDrvSrc = srcOnly stdenvAdapterDrv;
82 stdenvAdapterDrvSrcEquiv = srcOnly (
83 getEquivAttrs stdenvAdapterDrv
84 // {
85 # The upside of using overrideAttrs is that any stdenv adapter related
86 # modifications are only applied once. Using the adapter here again would
87 # mean applying it twice in total (since withCFlags functions more or less
88 # like an automatic overrideAttrs).
89 inherit stdenv;
90 }
91 );
92
93 # Issue similar to https://github.com/NixOS/nixpkgs/issues/269539
94 structuredAttrsDrv =
95 let
96 drv = stdenv.mkDerivation {
97 name = "drv-using-structured-attrs";
98 src = emptyDirectory;
99
100 env.NIX_DEBUG = true;
101 __structuredAttrs = true;
102 };
103 in
104 # Confirm the issue we are trying to avoid exists
105 assert !(canEvalDrv (srcOnly drv.drvAttrs));
106 drv;
107 structuredAttrsDrvSrc = srcOnly structuredAttrsDrv;
108 structuredAttrsDrvSrcEquiv = srcOnly (getEquivAttrs structuredAttrsDrv);
109
110in
111
112runCommand "srcOnly-tests"
113 {
114 moreTests = [
115 (testers.testEqualDerivation "zlibSrcEquiv == zlibSrc" zlibSrcEquiv zlibSrc)
116 # (testers.testEqualDerivation
117 # "zlibSrcFreeform == zlibSrc"
118 # zlibSrcFreeform
119 # zlibSrc)
120 (testers.testEqualDerivation "helloSrcEquiv == helloSrc" helloSrcEquiv helloSrc)
121 (testers.testEqualDerivation "helloSrcEquiv == helloSrc" helloSrcEquiv helloSrc)
122 (testers.testEqualDerivation "gitSrcEquiv == gitSrc" gitSrcEquiv gitSrc)
123 (testers.testEqualDerivation "helloDrvSimpleSrcFreeform == helloDrvSimpleSrc"
124 helloDrvSimpleSrcFreeform
125 helloDrvSimpleSrc
126 )
127 (testers.testEqualDerivation "stdenvAdapterDrvSrcEquiv == stdenvAdapterDrvSrc"
128 stdenvAdapterDrvSrcEquiv
129 stdenvAdapterDrvSrc
130 )
131 (testers.testEqualDerivation "structuredAttrsDrvSrcEquiv == structuredAttrsDrvSrc"
132 structuredAttrsDrvSrcEquiv
133 structuredAttrsDrvSrc
134 )
135 ];
136 }
137 ''
138 # Test that emptySrc is empty
139 if [ -n "$(ls -A ${emptySrc})" ]; then
140 echo "emptySrc is not empty"
141 exit 1
142 fi
143
144 # Test that zlibSrc is not empty
145 if [ -z "$(ls -A ${zlibSrc})" ]; then
146 echo "zlibSrc is empty"
147 exit 1
148 fi
149
150 # Make $out exist to avoid build failure
151 mkdir -p $out
152 ''