Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1#!/usr/bin/env bash 2set -e 3 4scriptName=update-source-version # do not use the .wrapped name 5 6die() { 7 echo "$scriptName: error: $1" >&2 8 exit 1 9} 10 11usage() { 12 echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]" 13 echo " [--version-key=<version-key>] [--source-key=<source-key>]" 14 echo " [--system=<system>] [--file=<file-to-update>] [--rev=<revision>]" 15 echo " [--ignore-same-hash] [--print-changes]" 16} 17 18args=() 19 20for arg in "$@"; do 21 case $arg in 22 --system=*) 23 system="${arg#*=}" 24 systemArg="--system ${arg#*=}" 25 ;; 26 --version-key=*) 27 versionKey="${arg#*=}" 28 ;; 29 --source-key=*) 30 sourceKey="${arg#*=}" 31 ;; 32 --file=*) 33 nixFile="${arg#*=}" 34 if [[ ! -f "$nixFile" ]]; then 35 die "Could not find provided file $nixFile" 36 fi 37 ;; 38 --rev=*) 39 newRevision="${arg#*=}" 40 ;; 41 --ignore-same-hash) 42 ignoreSameHash="true" 43 ;; 44 --print-changes) 45 printChanges="true" 46 ;; 47 --help) 48 usage 49 exit 0 50 ;; 51 --*) 52 echo "$scriptName: Unknown argument: $arg" 53 usage 54 exit 1 55 ;; 56 *) 57 args["${#args[*]}"]=$arg 58 ;; 59 esac 60done 61 62attr=${args[0]} 63newVersion=${args[1]} 64newHash=${args[2]} 65newUrl=${args[3]} 66 67# Third-party repositories might not accept arguments in their default.nix. 68importTree="(let tree = import ./.; in if builtins.isFunction tree then tree {} else tree)" 69 70if (( "${#args[*]}" < 2 )); then 71 echo "$scriptName: Too few arguments" 72 usage 73 exit 1 74fi 75 76if (( "${#args[*]}" > 4 )); then 77 echo "$scriptName: Too many arguments" 78 usage 79 exit 1 80fi 81 82if [[ -z "$versionKey" ]]; then 83 versionKey=version 84fi 85 86if [[ -z "$sourceKey" ]]; then 87 sourceKey=src 88fi 89 90# Allow finding packages among flake outputs in repos using flake-compat. 91pname=$(nix-instantiate $systemArg --eval --strict -A "$attr.name" || echo) 92if [[ -z "$pname" ]]; then 93 if [[ -z "$system" ]]; then 94 system=$(nix-instantiate --eval -E 'builtins.currentSystem' | tr -d '"') 95 fi 96 97 pname=$(nix-instantiate $systemArg --eval --strict -A "packages.$system.$attr.name" || echo) 98 if [[ -n "$pname" ]]; then 99 attr="packages.$system.$attr" 100 else 101 pname=$(nix-instantiate $systemArg --eval --strict -A "legacyPackages.$system.$attr.name" || echo) 102 if [[ -n "$pname" ]]; then 103 attr="legacyPackages.$system.$attr" 104 else 105 die "Could not find attribute '$attr'!" 106 fi 107 fi 108fi 109 110if [[ -z "$nixFile" ]]; then 111 nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/') 112 if [[ ! -f "$nixFile" ]]; then 113 die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!" 114 fi 115 116 # flake-compat will return paths in the Nix store, we need to correct for that. 117 possiblyOutPath=$(nix-instantiate $systemArg --eval -E "with $importTree; outPath" 2>/dev/null | tr -d '"') 118 if [[ -n "$possiblyOutPath" ]]; then 119 outPathEscaped=$(echo "$possiblyOutPath" | sed 's#[$^*\\.[|]#\\&#g') 120 pwdEscaped=$(echo "$PWD" | sed 's#[$^*\\.[|]#\\&#g') 121 nixFile=$(echo "$nixFile" | sed "s|^$outPathEscaped|$pwdEscaped|") 122 fi 123fi 124 125oldHashAlgo=$(nix-instantiate $systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHashAlgo" | tr -d '"') 126oldHash=$(nix-instantiate $systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHash" | tr -d '"') 127 128if [[ -z "$oldHashAlgo" || -z "$oldHash" ]]; then 129 die "Couldn't evaluate old source hash from '$attr.$sourceKey'!" 130fi 131 132if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then 133 die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!" 134fi 135 136oldVersion=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"') 137 138if [[ -z "$oldVersion" ]]; then 139 die "Couldn't find out the old version of '$attr'!" 140fi 141 142if [[ "$oldVersion" = "$newVersion" ]]; then 143 echo "$scriptName: New version same as old version, nothing to do." >&2 144 if [ -n "$printChanges" ]; then 145 printf '[]\n' 146 fi 147 exit 0 148fi 149 150if [[ -n "$newRevision" ]]; then 151 oldRevision=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.$sourceKey.rev" | tr -d '"') 152 if [[ -z "$oldRevision" ]]; then 153 die "Couldn't evaluate source revision from '$attr.$sourceKey'!" 154 fi 155fi 156 157# Escape regex metacharacter that are allowed in store path names 158oldVersionEscaped=$(echo "$oldVersion" | sed -re 's|[.+]|\\&|g') 159 160if [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then 161 pattern="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|" 162elif [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then 163 pattern="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|" 164else 165 die "Couldn't figure out where out where to patch in new version in '$attr'!" 166fi 167 168if [[ "$oldHash" =~ ^(sha256|sha512)[:-] ]]; then 169 # Handle the possible SRI-style hash attribute (in the form ${type}${separator}${hash}) 170 # True SRI uses dash as a separator and only supports base64, whereas Nix’s SRI-style format uses a colon and supports all the same encodings like regular hashes (16/32/64). 171 # To keep this program reasonably simple, we will upgrade Nix’s format to SRI. 172 oldHashAlgo="${BASH_REMATCH[1]}" 173 sri=true 174elif [[ "$oldHashAlgo" = "null" ]]; then 175 # Some fetcher functions support SRI-style `hash` attribute in addition to legacy type-specific attributes. When `hash` is used `outputHashAlgo` is null so let’s complain when SRI-style hash value was not detected. 176 die "Unable to figure out hashing scheme from '$oldHash' in '$attr'!" 177fi 178 179case "$oldHashAlgo" in 180 # Choose a temporary hash for given algorithm. 181 # Not using all-zeroes hash, since that is sometimes 182 # used for clean-up when updating multi-source packages. 183 # Created by hashing “update-source-version” string. 184 sha256) tempHash=AzH1rZFqEH8sovZZfJykvsEmCedEZWigQFHWHl6/PdE= ;; 185 sha512) tempHash=KFj9Fvco4AuCgLJIGRnVzyssRf7VGP2oi5CkH6ADvj75ow3am3h8pxefOgQlO+i33Q/BBnG/ST/F7B/0BvWHxw== ;; 186 *) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;; 187esac 188 189if [[ -n "$sri" ]]; then 190 # SRI hashes only support base64 191 # SRI hashes need to declare the hash type as part of the hash 192 tempHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null \ 193 || nix to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null)" \ 194 || die "Failed to convert hash to SRI representation!" 195fi 196 197# Escape regex metacharacter that are allowed in hashes (+) 198oldHashEscaped=$(echo "$oldHash" | sed -re 's|[+]|\\&|g') 199tempHashEscaped=$(echo "$tempHash" | sed -re 's|[+]|\\&|g') 200 201# Replace new version 202sed -i.cmp "$nixFile" -re "$pattern" 203if cmp -s "$nixFile" "$nixFile.cmp"; then 204 die "Failed to replace version '$oldVersion' to '$newVersion' in '$attr'!" 205fi 206 207# Replace new URL 208if [[ -n "$newUrl" ]]; then 209 oldUrl=$(nix-instantiate $systemArg --eval -E "with $importTree; builtins.elemAt ($attr.$sourceKey.drvAttrs.urls or [ $attr.$sourceKey.url ]) 0" | tr -d '"') 210 if [[ -z "$oldUrl" ]]; then 211 die "Couldn't evaluate source url from '$attr.$sourceKey'!" 212 fi 213 214 # Escape regex metacharacter that are allowed in store path names 215 oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+]|\\&|g') 216 217 sed -i.cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|" 218 if cmp -s "$nixFile" "$nixFile.cmp"; then 219 die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!" 220 fi 221fi 222 223sed -i.cmp "$nixFile" -re "s|\"$oldHashEscaped\"|\"$tempHash\"|" 224if cmp -s "$nixFile" "$nixFile.cmp"; then 225 die "Failed to replace source hash of '$attr' to a temporary hash!" 226fi 227 228# Replace new revision, if given 229if [[ -n "$newRevision" ]]; then 230 sed -i.cmp "$nixFile" -re "s|\"$oldRevision\"|\"$newRevision\"|" 231 if cmp -s "$nixFile" "$nixFile.cmp"; then 232 die "Failed to replace source revision '$oldRevision' to '$newRevision' in '$attr'!" 233 fi 234fi 235 236# If new hash not given on the command line, recalculate it ourselves. 237if [[ -z "$newHash" ]]; then 238 nix-build $systemArg --no-out-link -A "$attr.$sourceKey" 2>"$attr.fetchlog" >/dev/null || true 239 # FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed 240 newHash=$( 241 sed '1,/hash mismatch in fixed-output derivation/d' "$attr.fetchlog" \ 242 | grep --perl-regexp --only-matching 'got: +.+[:-]\K.+' \ 243 || true # handled below 244 ) 245 246 if [[ -n "$newHash" && -n "$sri" ]]; then 247 # nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type 248 newHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null \ 249 || nix to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null)" \ 250 || die "Failed to convert hash to SRI representation!" 251 fi 252fi 253 254if [[ -z "$newHash" ]]; then 255 cat "$attr.fetchlog" >&2 256 die "Couldn't figure out new hash of '$attr.$sourceKey'!" 257fi 258 259if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then 260 die "Both the old and new source hashes of '$attr.$sourceKey' were equivalent. Please fix the package's source URL to be dependent on '\${version}'!" 261fi 262 263sed -i.cmp "$nixFile" -re "s|\"$tempHashEscaped\"|\"$newHash\"|" 264if cmp -s "$nixFile" "$nixFile.cmp"; then 265 die "Failed to replace temporary source hash of '$attr' to the final source hash!" 266fi 267 268rm -f "$nixFile.cmp" 269rm -f "$attr.fetchlog" 270 271if [ -n "$printChanges" ]; then 272 printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile" 273fi