2016-12-31 13:31:56 +01:00
#!/usr/bin/env bash
set -e
2021-01-09 11:04:13 +01:00
scriptName=update-source-version # do not use the .wrapped name
2019-03-02 22:11:54 +01:00
2016-12-31 13:31:56 +01:00
die() {
2019-03-02 22:11:54 +01:00
echo "$scriptName: error: $1" >&2
2016-12-31 13:31:56 +01:00
exit 1
}
2019-03-02 22:11:54 +01:00
usage() {
echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]"
2021-12-31 06:54:09 +01:00
echo " [--version-key=<version-key>] [--source-key=<source-key>]"
echo " [--system=<system>] [--file=<file-to-update>] [--rev=<revision>]"
2019-04-12 18:22:06 +02:00
echo " [--ignore-same-hash] [--print-changes]"
2019-03-02 22:11:54 +01:00
}
args=()
for arg in "$@"; do
case $arg in
--system=*)
2021-04-24 04:36:58 +02:00
system="${arg#*=}"
2019-03-02 22:11:54 +01:00
systemArg="--system ${arg#*=}"
;;
--version-key=*)
versionKey="${arg#*=}"
;;
2021-12-31 06:54:09 +01:00
--source-key=*)
sourceKey="${arg#*=}"
;;
2019-03-02 22:11:54 +01:00
--file=*)
nixFile="${arg#*=}"
2020-01-30 21:25:01 +01:00
if [[ ! -f "$nixFile" ]]; then
2019-03-02 22:11:54 +01:00
die "Could not find provided file $nixFile"
fi
;;
2020-10-19 19:54:53 +02:00
--rev=*)
newRevision="${arg#*=}"
;;
2019-04-07 01:40:18 +02:00
--ignore-same-hash)
ignoreSameHash="true"
;;
2019-04-12 18:22:06 +02:00
--print-changes)
printChanges="true"
;;
2019-03-02 22:11:54 +01:00
--help)
usage
exit 0
;;
--*)
2020-01-30 21:25:01 +01:00
echo "$scriptName: Unknown argument: $arg"
2019-03-02 22:11:54 +01:00
usage
exit 1
;;
*)
args["${#args[*]}"]=$arg
;;
esac
done
attr=${args[0]}
newVersion=${args[1]}
newHash=${args[2]}
newUrl=${args[3]}
2021-04-24 02:56:20 +02:00
# Third-party repositories might not accept arguments in their default.nix.
importTree="(let tree = import ./.; in if builtins.isFunction tree then tree {} else tree)"
2020-01-30 21:25:01 +01:00
if (( "${#args[*]}" < 2 )); then
2019-03-02 22:11:54 +01:00
echo "$scriptName: Too few arguments"
usage
exit 1
fi
2020-01-30 21:25:01 +01:00
if (( "${#args[*]}" > 4 )); then
2019-03-02 22:11:54 +01:00
echo "$scriptName: Too many arguments"
usage
exit 1
fi
2018-11-16 15:04:54 +01:00
2020-01-30 21:25:01 +01:00
if [[ -z "$versionKey" ]]; then
2018-11-16 15:04:54 +01:00
versionKey=version
fi
2016-12-31 13:31:56 +01:00
2021-12-31 06:54:09 +01:00
if [[ -z "$sourceKey" ]]; then
sourceKey=src
fi
2021-04-24 04:36:58 +02:00
# Allow finding packages among flake outputs in repos using flake-compat.
pname=$(nix-instantiate $systemArg --eval --strict -A "$attr.name" || echo)
if [[ -z "$pname" ]]; then
if [[ -z "$system" ]]; then
system=$(nix-instantiate --eval -E 'builtins.currentSystem' | tr -d '"')
fi
pname=$(nix-instantiate $systemArg --eval --strict -A "packages.$system.$attr.name" || echo)
if [[ -n "$pname" ]]; then
attr="packages.$system.$attr"
else
pname=$(nix-instantiate $systemArg --eval --strict -A "legacyPackages.$system.$attr.name" || echo)
if [[ -n "$pname" ]]; then
attr="legacyPackages.$system.$attr"
else
die "Could not find attribute '$attr'!"
fi
fi
fi
2020-01-30 21:25:01 +01:00
if [[ -z "$nixFile" ]]; then
2019-03-02 22:11:54 +01:00
nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/')
2020-01-30 21:25:01 +01:00
if [[ ! -f "$nixFile" ]]; then
2019-03-02 22:11:54 +01:00
die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!"
fi
2021-04-24 03:42:01 +02:00
# flake-compat will return paths in the Nix store, we need to correct for that.
2021-12-31 07:01:16 +01:00
possiblyOutPath=$(nix-instantiate $systemArg --eval -E "with $importTree; outPath" 2>/dev/null | tr -d '"')
2021-04-24 03:42:01 +02:00
if [[ -n "$possiblyOutPath" ]]; then
outPathEscaped=$(echo "$possiblyOutPath" | sed 's#[$^*\\.[|]#\\&#g')
pwdEscaped=$(echo "$PWD" | sed 's#[$^*\\.[|]#\\&#g')
nixFile=$(echo "$nixFile" | sed "s|^$outPathEscaped|$pwdEscaped|")
fi
2016-12-31 13:31:56 +01:00
fi
2021-12-31 06:54:09 +01:00
oldHashAlgo=$(nix-instantiate $systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHashAlgo" | tr -d '"')
oldHash=$(nix-instantiate $systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHash" | tr -d '"')
2016-12-31 13:31:56 +01:00
2020-01-30 21:25:01 +01:00
if [[ -z "$oldHashAlgo" || -z "$oldHash" ]]; then
2021-12-31 06:54:09 +01:00
die "Couldn't evaluate old source hash from '$attr.$sourceKey'!"
2016-12-31 13:31:56 +01:00
fi
2020-01-30 21:25:01 +01:00
if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then
2016-12-31 13:31:56 +01:00
die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!"
fi
2021-04-24 02:56:20 +02:00
oldVersion=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"')
2016-12-31 13:31:56 +01:00
2021-04-24 02:50:53 +02:00
if [[ -z "$oldVersion" ]]; then
die "Couldn't find out the old version of '$attr'!"
2016-12-31 13:31:56 +01:00
fi
2020-01-30 21:25:01 +01:00
if [[ "$oldVersion" = "$newVersion" ]]; then
2019-03-02 22:11:54 +01:00
echo "$scriptName: New version same as old version, nothing to do." >&2
2019-04-12 18:22:06 +02:00
if [ -n "$printChanges" ]; then
printf '[]\n'
fi
2016-12-31 13:31:56 +01:00
exit 0
fi
2020-10-19 19:54:53 +02:00
if [[ -n "$newRevision" ]]; then
2021-12-31 06:54:09 +01:00
oldRevision=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.$sourceKey.rev" | tr -d '"')
2020-10-19 19:54:53 +02:00
if [[ -z "$oldRevision" ]]; then
2021-12-31 06:54:09 +01:00
die "Couldn't evaluate source revision from '$attr.$sourceKey'!"
2020-10-19 19:54:53 +02:00
fi
fi
2017-11-19 02:33:06 +01:00
# Escape regex metacharacter that are allowed in store path names
2019-04-12 18:54:13 +02:00
oldVersionEscaped=$(echo "$oldVersion" | sed -re 's|[.+]|\\&|g')
2016-12-31 13:31:56 +01:00
2020-01-30 21:25:01 +01:00
if [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
2019-04-12 18:54:13 +02:00
pattern="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|"
2020-01-30 21:25:01 +01:00
elif [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
2019-04-12 18:54:13 +02:00
pattern="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|"
2016-12-31 13:31:56 +01:00
else
die "Couldn't figure out where out where to patch in new version in '$attr'!"
fi
2020-01-30 21:08:55 +01:00
if [[ "$oldHash" =~ ^(sha256|sha512)[:-] ]]; then
# Handle the possible SRI-style hash attribute (in the form ${type}${separator}${hash})
# 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).
# To keep this program reasonably simple, we will upgrade Nix’ s format to SRI.
oldHashAlgo="${BASH_REMATCH[1]}"
sri=true
elif [[ "$oldHashAlgo" = "null" ]]; then
# 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.
die "Unable to figure out hashing scheme from '$oldHash' in '$attr'!"
fi
2016-12-31 13:31:56 +01:00
case "$oldHashAlgo" in
2022-09-16 18:29:05 +02:00
# Choose a temporary hash for given algorithm.
# Not using all-zeroes hash, since that is sometimes
# used for clean-up when updating multi-source packages.
# Created by hashing “update-source-version” string.
sha256) tempHash=AzH1rZFqEH8sovZZfJykvsEmCedEZWigQFHWHl6/PdE= ;;
sha512) tempHash=KFj9Fvco4AuCgLJIGRnVzyssRf7VGP2oi5CkH6ADvj75ow3am3h8pxefOgQlO+i33Q/BBnG/ST/F7B/0BvWHxw== ;;
2016-12-31 13:31:56 +01:00
*) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
esac
2020-01-30 21:08:55 +01:00
if [[ -n "$sri" ]]; then
# SRI hashes only support base64
# SRI hashes need to declare the hash type as part of the hash
2022-07-07 22:59:41 +02:00
tempHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null \
2021-10-01 18:37:21 +02:00
|| nix to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null)" \
|| die "Failed to convert hash to SRI representation!"
2020-01-30 21:08:55 +01:00
fi
2020-02-20 07:18:36 +01:00
# Escape regex metacharacter that are allowed in hashes (+)
oldHashEscaped=$(echo "$oldHash" | sed -re 's|[+]|\\&|g')
tempHashEscaped=$(echo "$tempHash" | sed -re 's|[+]|\\&|g')
2020-01-30 21:58:48 +01:00
# Replace new version
2022-09-08 00:37:38 +02:00
sed -i.cmp "$nixFile" -re "$pattern"
if cmp -s "$nixFile" "$nixFile.cmp"; then
2020-01-30 21:58:48 +01:00
die "Failed to replace version '$oldVersion' to '$newVersion' in '$attr'!"
fi
# Replace new URL
if [[ -n "$newUrl" ]]; then
2022-09-08 01:23:50 +02:00
oldUrl=$(nix-instantiate $systemArg --eval -E "with $importTree; builtins.elemAt ($attr.$sourceKey.drvAttrs.urls or [ $attr.$sourceKey.url ]) 0" | tr -d '"')
if [[ -z "$oldUrl" ]]; then
die "Couldn't evaluate source url from '$attr.$sourceKey'!"
fi
# Escape regex metacharacter that are allowed in store path names
oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+]|\\&|g')
2022-09-08 00:37:38 +02:00
sed -i.cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
if cmp -s "$nixFile" "$nixFile.cmp"; then
2020-01-30 21:58:48 +01:00
die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!"
fi
fi
2022-09-08 00:37:38 +02:00
sed -i.cmp "$nixFile" -re "s|\"$oldHashEscaped\"|\"$tempHash\"|"
if cmp -s "$nixFile" "$nixFile.cmp"; then
2016-12-31 13:31:56 +01:00
die "Failed to replace source hash of '$attr' to a temporary hash!"
fi
2020-10-19 19:54:53 +02:00
# Replace new revision, if given
if [[ -n "$newRevision" ]]; then
2022-09-08 00:37:38 +02:00
sed -i.cmp "$nixFile" -re "s|\"$oldRevision\"|\"$newRevision\"|"
if cmp -s "$nixFile" "$nixFile.cmp"; then
2020-10-19 19:54:53 +02:00
die "Failed to replace source revision '$oldRevision' to '$newRevision' in '$attr'!"
fi
fi
2016-12-31 13:31:56 +01:00
# If new hash not given on the command line, recalculate it ourselves.
2020-01-30 21:25:01 +01:00
if [[ -z "$newHash" ]]; then
2021-12-31 06:54:09 +01:00
nix-build $systemArg --no-out-link -A "$attr.$sourceKey" 2>"$attr.fetchlog" >/dev/null || true
2016-12-31 13:31:56 +01:00
# FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed
2022-09-24 14:52:38 +02:00
newHash=$(
sed '1,/hash mismatch in fixed-output derivation/d' "$attr.fetchlog" \
| grep --perl-regexp --only-matching 'got: +.+[:-]\K.+' \
|| true # handled below
)
2016-12-31 13:31:56 +01:00
2022-09-24 14:52:38 +02:00
if [[ -n "$newHash" && -n "$sri" ]]; then
2020-02-19 21:05:22 +01:00
# nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type
2022-07-07 22:59:41 +02:00
newHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null \
2021-10-01 18:37:21 +02:00
|| nix to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null)" \
|| die "Failed to convert hash to SRI representation!"
2020-02-19 21:05:22 +01:00
fi
2020-01-30 21:08:55 +01:00
fi
2020-01-30 21:25:01 +01:00
if [[ -z "$newHash" ]]; then
2016-12-31 13:31:56 +01:00
cat "$attr.fetchlog" >&2
2021-12-31 06:54:09 +01:00
die "Couldn't figure out new hash of '$attr.$sourceKey'!"
2016-12-31 13:31:56 +01:00
fi
2020-01-30 21:25:01 +01:00
if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then
2021-12-31 06:54:09 +01:00
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}'!"
2017-11-19 02:33:31 +01:00
fi
2022-09-08 00:37:38 +02:00
sed -i.cmp "$nixFile" -re "s|\"$tempHashEscaped\"|\"$newHash\"|"
if cmp -s "$nixFile" "$nixFile.cmp"; then
2016-12-31 13:31:56 +01:00
die "Failed to replace temporary source hash of '$attr' to the final source hash!"
fi
2022-09-08 00:37:38 +02:00
rm -f "$nixFile.cmp"
2016-12-31 13:31:56 +01:00
rm -f "$attr.fetchlog"
2019-04-12 18:22:06 +02:00
if [ -n "$printChanges" ]; then
printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile"
fi