nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# This derivation confirms that the version of hpack used by stack in Nixpkgs
2# is the exact same version as the upstream stack release.
3#
4# It is important to make sure the version of hpack used by stack in Nixpkgs
5# matches with the version of hpack used by the upstream stack release. This
6# is because hpack works slightly differently based on the version, and it can
7# be frustrating to use hpack in a team setting when members are using different
8# versions. See for more info:
9# https://github.com/NixOS/nixpkgs/issues/223390 krank:ignore-line
10#
11# This test is written as a fixed-output derivation, because we need to access
12# accesses the internet to download the upstream stack release.
13
14{
15 cacert,
16 curl,
17 lib,
18 stack,
19 stdenv,
20}:
21
22let
23 # Find the hpack derivation that is a dependency of stack. Throw exception
24 # if hpack cannot be found.
25 hpack =
26 lib.findFirst (v: v.pname or "" == "hpack") (throw "could not find stack's hpack dependency")
27 stack.passthru.getCabalDeps.executableHaskellDepends;
28
29 # This is a statically linked version of stack, so it should be usable within
30 # the Nixpkgs builder (at least on x86_64-linux).
31 stackDownloadUrl = "https://github.com/commercialhaskell/stack/releases/download/v${stack.version}/stack-${stack.version}-linux-x86_64.tar.gz";
32
33 # This test code has been explicitly pulled out of the derivation below so
34 # that it can be hashed and added to the `name` of the derivation. This is
35 # so that this test derivation won't be cached if the body of the test is
36 # modified.
37 #
38 # WARNING: When modifying this script, make sure you don't introduce any
39 # paths to the Nix store within it. We only want this derivation to be re-run
40 # when the stack version (or the version of its hpack dependency) changes in
41 # Nixpkgs.
42 testScript = ''
43 curl=(
44 curl
45 --location
46 --max-redirs 20
47 --retry 3
48 --disable-epsv
49 --cookie-jar cookies
50 --user-agent "nixpkgs stack version test/1.0"
51 --insecure
52 )
53
54 # Fetch the statically-linked upstream Stack binary.
55 echo "Trying to download a statically linked stack binary from ${stackDownloadUrl} to ./stack.tar.gz ..."
56 "''${curl[@]}" "${stackDownloadUrl}" > ./stack.tar.gz
57 tar xf ./stack.tar.gz
58
59 upstream_stack_version_output="$(./stack-${stack.version}-linux-x86_64/stack --version)"
60 echo "upstream \`stack --version\` output: $upstream_stack_version_output"
61
62 nixpkgs_stack_version_output="$(stack --version)"
63 echo "nixpkgs \`stack --version\` output: $nixpkgs_stack_version_output"
64
65 # Confirm that the upstream stack version is the same as the stack version
66 # in Nixpkgs. This check isn't strictly necessary, but it is a good sanity
67 # check.
68
69 if [[ "$upstream_stack_version_output" =~ "Version "([0-9]+((\.[0-9]+)+)) ]]; then
70 upstream_stack_version="''${BASH_REMATCH[1]}"
71
72 echo "parsed upstream stack version: $upstream_stack_version"
73 echo "stack version from nixpkgs: ${stack.version}"
74
75 if [[ "${stack.version}" != "$upstream_stack_version" ]]; then
76 echo "ERROR: stack version in Nixpkgs (${stack.version}) does not match the upstream version for some reason: $upstream_stack_version"
77 exit 1
78 fi
79 else
80 echo "ERROR: Upstream stack version cannot be found in --version output: $upstream_stack_version"
81 exit 1
82 fi
83
84 # Confirm that the hpack version used in the upstream stack release is the
85 # same as the hpack version used by the Nixpkgs stack binary.
86
87 if [[ "$upstream_stack_version_output" =~ hpack-([0-9]+((\.[0-9]+)+)) ]]; then
88 upstream_hpack_version="''${BASH_REMATCH[1]}"
89
90 echo "parsed upstream stack's hpack version: $upstream_hpack_version"
91 echo "Nixpkgs stack's hpack version: ${hpack.version}"
92
93 if [[ "${hpack.version}" != "$upstream_hpack_version" ]]; then
94 echo "ERROR: stack's hpack version in Nixpkgs (${hpack.version}) does not match the upstream stack's hpack version: $upstream_hpack_version"
95 echo "The stack derivation in Nixpkgs needs to be fixed up so that it depends on hpack-$upstream_hpack_version, instead of ${hpack.name}"
96 exit 1
97 fi
98 else
99 echo "ERROR: Upstream stack's hpack version cannot be found in --version output: $upstream_hpack_version"
100 exit 1
101 fi
102
103 # Output a string with a known hash.
104 echo "success" > $out
105 '';
106
107 testScriptHash = builtins.hashString "sha256" testScript;
108in
109
110stdenv.mkDerivation {
111
112 # This name is very important.
113 #
114 # The idea here is that want this derivation to be re-run everytime the
115 # version of stack (or the version of its hpack dependency) changes in
116 # Nixpkgs. We also want to re-run this derivation whenever the test script
117 # is changed.
118 #
119 # Nix/Hydra will re-run derivations if their name changes (even if they are a
120 # FOD and they have the same hash).
121 #
122 # The name of this derivation contains the stack version string, the hpack
123 # version string, and a hash of the test script. So Nix will know to
124 # re-run this version when (and only when) one of those values change.
125 name = "upstream-stack-hpack-version-test-${stack.name}-${hpack.name}-${testScriptHash}";
126
127 # This is the sha256 hash for the string "success", which is output upon this
128 # test succeeding.
129 outputHash = "sha256-gbK9TqmMjbZlVPvI12N6GmmhMPMx/rcyt1yqtMSGj9U=";
130 outputHashMode = "flat";
131 outputHashAlgo = "sha256";
132
133 nativeBuildInputs = [
134 curl
135 stack
136 ];
137
138 impureEnvVars = lib.fetchers.proxyImpureEnvVars;
139
140 buildCommand = ''
141 # Make sure curl can access HTTPS sites, like GitHub.
142 #
143 # Note that we absolutely don't want the Nix store path of the cacert
144 # derivation in the testScript, because we don't want to rebuild this
145 # derivation when only the cacert derivation changes.
146 export SSL_CERT_FILE="${cacert}/etc/ssl/certs/ca-bundle.crt"
147 ''
148 + testScript;
149
150 meta = {
151 description = "Test that the stack in Nixpkgs uses the same version of Hpack as the upstream stack release";
152 maintainers = with lib.maintainers; [ cdepillabout ];
153
154 # This derivation internally runs a statically-linked version of stack from
155 # upstream. This statically-linked version of stack is only available for
156 # x86_64-linux, so this test can only be run on x86_64-linux.
157 platforms = [ "x86_64-linux" ];
158 };
159}