2016-12-31 12:31:56 +00:00
#!/usr/bin/env bash
set -e
2021-01-09 10:04:13 +00:00
scriptName=update-source-version # do not use the .wrapped name
2019-03-02 21:11:54 +00:00
2016-12-31 12:31:56 +00:00
die() {
2019-03-02 21:11:54 +00:00
echo "$scriptName: error: $1" >&2
2016-12-31 12:31:56 +00:00
exit 1
}
2019-03-02 21:11:54 +00:00
usage() {
echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]"
2021-12-31 05:54:09 +00:00
echo " [--version-key=<version-key>] [--source-key=<source-key>]"
echo " [--system=<system>] [--file=<file-to-update>] [--rev=<revision>]"
2019-04-12 16:22:06 +00:00
echo " [--ignore-same-hash] [--print-changes]"
2019-03-02 21:11:54 +00:00
}
args=()
for arg in "$@"; do
case $arg in
--system=*)
2021-04-24 02:36:58 +00:00
system="${arg#*=}"
2019-03-02 21:11:54 +00:00
systemArg="--system ${arg#*=}"
;;
--version-key=*)
versionKey="${arg#*=}"
;;
2021-12-31 05:54:09 +00:00
--source-key=*)
sourceKey="${arg#*=}"
;;
2019-03-02 21:11:54 +00:00
--file=*)
nixFile="${arg#*=}"
2020-01-30 20:25:01 +00:00
if [[ ! -f "$nixFile" ]]; then
2019-03-02 21:11:54 +00:00
die "Could not find provided file $nixFile"
fi
;;
2020-10-19 17:54:53 +00:00
--rev=*)
newRevision="${arg#*=}"
;;
2019-04-06 23:40:18 +00:00
--ignore-same-hash)
ignoreSameHash="true"
;;
2019-04-12 16:22:06 +00:00
--print-changes)
printChanges="true"
;;
2019-03-02 21:11:54 +00:00
--help)
usage
exit 0
;;
--*)
2020-01-30 20:25:01 +00:00
echo "$scriptName: Unknown argument: $arg"
2019-03-02 21:11:54 +00:00
usage
exit 1
;;
*)
args["${#args[*]}"]=$arg
;;
esac
done
attr=${args[0]}
newVersion=${args[1]}
newHash=${args[2]}
newUrl=${args[3]}
2021-04-24 00:56:20 +00: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 20:25:01 +00:00
if (( "${#args[*]}" < 2 )); then
2019-03-02 21:11:54 +00:00
echo "$scriptName: Too few arguments"
usage
exit 1
fi
2020-01-30 20:25:01 +00:00
if (( "${#args[*]}" > 4 )); then
2019-03-02 21:11:54 +00:00
echo "$scriptName: Too many arguments"
usage
exit 1
fi
2018-11-16 14:04:54 +00:00
2020-01-30 20:25:01 +00:00
if [[ -z "$versionKey" ]]; then
2018-11-16 14:04:54 +00:00
versionKey=version
fi
2016-12-31 12:31:56 +00:00
2021-12-31 05:54:09 +00:00
if [[ -z "$sourceKey" ]]; then
sourceKey=src
fi
2021-04-24 02:36:58 +00: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 20:25:01 +00:00
if [[ -z "$nixFile" ]]; then
2019-03-02 21:11:54 +00:00
nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/')
2020-01-30 20:25:01 +00:00
if [[ ! -f "$nixFile" ]]; then
2019-03-02 21:11:54 +00:00
die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!"
fi
2021-04-24 01:42:01 +00:00
# flake-compat will return paths in the Nix store, we need to correct for that.
2021-12-31 06:01:16 +00:00
possiblyOutPath=$(nix-instantiate $systemArg --eval -E "with $importTree; outPath" 2>/dev/null | tr -d '"')
2021-04-24 01:42:01 +00: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 12:31:56 +00:00
fi
2021-12-31 05:54:09 +00: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 12:31:56 +00:00
2020-01-30 20:25:01 +00:00
if [[ -z "$oldHashAlgo" || -z "$oldHash" ]]; then
2021-12-31 05:54:09 +00:00
die "Couldn't evaluate old source hash from '$attr.$sourceKey'!"
2016-12-31 12:31:56 +00:00
fi
2020-01-30 20:25:01 +00:00
if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then
2016-12-31 12:31:56 +00:00
die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!"
fi
2021-12-31 05:54:09 +00:00
oldUrl=$(nix-instantiate $systemArg --eval -E "with $importTree; builtins.elemAt ($attr.$sourceKey.drvAttrs.urls or [ $attr.$sourceKey.url ]) 0" | tr -d '"')
2018-03-14 14:24:34 +00:00
2020-01-30 20:25:01 +00:00
if [[ -z "$oldUrl" ]]; then
2021-12-31 05:54:09 +00:00
die "Couldn't evaluate source url from '$attr.$sourceKey'!"
2018-03-14 14:24:34 +00:00
fi
2021-04-24 00:56:20 +00:00
oldVersion=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"')
2016-12-31 12:31:56 +00:00
2021-04-24 00:50:53 +00:00
if [[ -z "$oldVersion" ]]; then
die "Couldn't find out the old version of '$attr'!"
2016-12-31 12:31:56 +00:00
fi
2020-01-30 20:25:01 +00:00
if [[ "$oldVersion" = "$newVersion" ]]; then
2019-03-02 21:11:54 +00:00
echo "$scriptName: New version same as old version, nothing to do." >&2
2019-04-12 16:22:06 +00:00
if [ -n "$printChanges" ]; then
printf '[]\n'
fi
2016-12-31 12:31:56 +00:00
exit 0
fi
2020-10-19 17:54:53 +00:00
if [[ -n "$newRevision" ]]; then
2021-12-31 05:54:09 +00:00
oldRevision=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.$sourceKey.rev" | tr -d '"')
2020-10-19 17:54:53 +00:00
if [[ -z "$oldRevision" ]]; then
2021-12-31 05:54:09 +00:00
die "Couldn't evaluate source revision from '$attr.$sourceKey'!"
2020-10-19 17:54:53 +00:00
fi
fi
2017-11-19 01:33:06 +00:00
# Escape regex metacharacter that are allowed in store path names
2019-04-12 16:54:13 +00:00
oldVersionEscaped=$(echo "$oldVersion" | sed -re 's|[.+]|\\&|g')
oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+]|\\&|g')
2016-12-31 12:31:56 +00:00
2020-01-30 20:25:01 +00:00
if [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
2019-04-12 16:54:13 +00:00
pattern="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|"
2020-01-30 20:25:01 +00:00
elif [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
2019-04-12 16:54:13 +00:00
pattern="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|"
2016-12-31 12:31:56 +00:00
else
die "Couldn't figure out where out where to patch in new version in '$attr'!"
fi
2020-01-30 20:08:55 +00: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 12:31:56 +00:00
case "$oldHashAlgo" in
2020-01-30 20:08:55 +00:00
# Lengths of hex-encoded hashes
2016-12-31 12:31:56 +00:00
sha256) hashLength=64 ;;
sha512) hashLength=128 ;;
*) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
esac
# Make a temporary all-zeroes hash of $hashLength characters
tempHash=$(printf '%0*d' "$hashLength" 0)
2020-01-30 20:08:55 +00:00
if [[ -n "$sri" ]]; then
# SRI hashes only support base64
# SRI hashes need to declare the hash type as part of the hash
2021-10-01 16:37:21 +00:00
tempHash="$(nix hash to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null \
|| nix to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null)" \
|| die "Failed to convert hash to SRI representation!"
2020-01-30 20:08:55 +00:00
fi
2020-02-20 06:18:36 +00: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 20:58:48 +00:00
# Replace new version
sed -i.bak "$nixFile" -re "$pattern"
if cmp -s "$nixFile" "$nixFile.bak"; then
die "Failed to replace version '$oldVersion' to '$newVersion' in '$attr'!"
fi
# Replace new URL
if [[ -n "$newUrl" ]]; then
sed -i "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
if cmp -s "$nixFile" "$nixFile.bak"; then
die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!"
fi
fi
2020-02-20 06:18:36 +00:00
sed -i "$nixFile" -re "s|\"$oldHashEscaped\"|\"$tempHash\"|"
2016-12-31 12:31:56 +00:00
if cmp -s "$nixFile" "$nixFile.bak"; then
die "Failed to replace source hash of '$attr' to a temporary hash!"
fi
2020-10-19 17:54:53 +00:00
# Replace new revision, if given
if [[ -n "$newRevision" ]]; then
sed -i "$nixFile" -re "s|\"$oldRevision\"|\"$newRevision\"|"
if cmp -s "$nixFile" "$nixFile.bak"; then
die "Failed to replace source revision '$oldRevision' to '$newRevision' in '$attr'!"
fi
fi
2016-12-31 12:31:56 +00:00
# If new hash not given on the command line, recalculate it ourselves.
2020-01-30 20:25:01 +00:00
if [[ -z "$newHash" ]]; then
2021-12-31 05:54:09 +00:00
nix-build $systemArg --no-out-link -A "$attr.$sourceKey" 2>"$attr.fetchlog" >/dev/null || true
2016-12-31 12:31:56 +00:00
# FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed
2020-02-19 20:05:22 +00:00
newHash=$(sed '1,/hash mismatch in fixed-output derivation/d' "$attr.fetchlog" | grep --perl-regexp --only-matching 'got: +.+[:-]\K.+')
2016-12-31 12:31:56 +00:00
2020-02-19 20:05:22 +00:00
if [[ -n "$sri" ]]; then
# nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type
2021-10-01 16:37:21 +00:00
newHash="$(nix hash to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null \
|| nix to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null)" \
|| die "Failed to convert hash to SRI representation!"
2020-02-19 20:05:22 +00:00
fi
2020-01-30 20:08:55 +00:00
fi
2020-01-30 20:25:01 +00:00
if [[ -z "$newHash" ]]; then
2016-12-31 12:31:56 +00:00
cat "$attr.fetchlog" >&2
2021-12-31 05:54:09 +00:00
die "Couldn't figure out new hash of '$attr.$sourceKey'!"
2016-12-31 12:31:56 +00:00
fi
2020-01-30 20:25:01 +00:00
if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then
2017-11-19 01:33:31 +00:00
mv "$nixFile.bak" "$nixFile"
2021-12-31 05:54:09 +00: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 01:33:31 +00:00
fi
2020-02-20 06:18:36 +00:00
sed -i "$nixFile" -re "s|\"$tempHashEscaped\"|\"$newHash\"|"
2016-12-31 12:31:56 +00:00
if cmp -s "$nixFile" "$nixFile.bak"; then
die "Failed to replace temporary source hash of '$attr' to the final source hash!"
fi
rm -f "$nixFile.bak"
rm -f "$attr.fetchlog"
2019-04-12 16:22:06 +00:00
if [ -n "$printChanges" ]; then
printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile"
fi