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