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