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