@@ -11,6 +11,7 @@ die() {
1111usage() {
1212 echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]"
1313 echo " [--version-key=<version-key>] [--system=<system>] [--file=<file-to-update>]"
14+ echo " [--ignore-same-hash]"
1415}
1516
1617args=()
@@ -25,7 +26,7 @@ for arg in "$@"; do
2526 ;;
2627 --file=*)
2728 nixFile="${arg#*=}"
28− if [ ! -f "$nixFile" ]; then
29+ if [[ ! -f "$nixFile" ]]; then
2930 die "Could not find provided file $nixFile"
3031 fi
3132 ;;
@@ -37,7 +38,7 @@ for arg in "$@"; do
3738 exit 0
3839 ;;
3940 --*)
40− echo "$scriptName: Unknown argument: " $arg
41+ echo "$scriptName: Unknown argument: $arg"
4142 usage
4243 exit 1
4344 ;;
@@ -52,25 +53,25 @@ newVersion=${args[1]}
5253newHash=${args[2]}
5354newUrl=${args[3]}
5455
55−if [ "${#args[*]}" -lt 2 ]; then
56+if (( "${#args[*]}" < 2 )); then
5657 echo "$scriptName: Too few arguments"
5758 usage
5859 exit 1
5960fi
6061
61−if [ "${#args[*]}" -gt 4 ]; then
62+if (( "${#args[*]}" > 4 )); then
6263 echo "$scriptName: Too many arguments"
6364 usage
6465 exit 1
6566fi
6667
67−if [ -z "$versionKey" ]; then
68+if [[ -z "$versionKey" ]]; then
6869 versionKey=version
6970fi
7071
71−if [ -z "$nixFile" ]; then
72+if [[ -z "$nixFile" ]]; then
7273 nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/')
73− if [ ! -f "$nixFile" ]; then
74+ if [[ ! -f "$nixFile" ]]; then
7475 die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!"
7576 fi
7677fi
@@ -78,28 +79,28 @@ fi
7879oldHashAlgo=$(nix-instantiate $systemArg --eval --strict -A "$attr.src.drvAttrs.outputHashAlgo" | tr -d '"')
7980oldHash=$(nix-instantiate $systemArg --eval --strict -A "$attr.src.drvAttrs.outputHash" | tr -d '"')
8081
81−if [ -z "$oldHashAlgo" -o -z "$oldHash" ]; then
82+if [[ -z "$oldHashAlgo" || -z "$oldHash" ]]; then
8283 die "Couldn't evaluate old source hash from '$attr.src'!"
8384fi
8485
85−if [ $(grep -c "$oldHash" "$nixFile") != 1 ]; then
86+if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then
8687 die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!"
8788fi
8889
8990oldUrl=$(nix-instantiate $systemArg --eval -E "with import ./. {}; builtins.elemAt ($attr.src.drvAttrs.urls or [ $attr.src.url ]) 0" | tr -d '"')
9091
91−if [ -z "$oldUrl" ]; then
92+if [[ -z "$oldUrl" ]]; then
9293 die "Couldn't evaluate source url from '$attr.src'!"
9394fi
9495
9596drvName=$(nix-instantiate $systemArg --eval -E "with import ./. {}; lib.getName $attr" | tr -d '"')
9697oldVersion=$(nix-instantiate $systemArg --eval -E "with import ./. {}; $attr.${versionKey} or (lib.getVersion $attr)" | tr -d '"')
9798
98−if [ -z "$drvName" -o -z "$oldVersion" ]; then
99+if [[ -z "$drvName" || -z "$oldVersion" ]]; then
99100 die "Couldn't evaluate name and version from '$attr.name'!"
100101fi
101102
102−if [ "$oldVersion" = "$newVersion" ]; then
103+if [[ "$oldVersion" = "$newVersion" ]]; then
103104 echo "$scriptName: New version same as old version, nothing to do." >&2
104105 exit 0
105106fi
@@ -108,14 +109,41 @@ fi
108109oldVersionEscaped=$(echo "$oldVersion" | sed -re 's|[.+]|\\&|g')
109110oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+]|\\&|g')
110111
111−if [ $(grep -c -E "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]; then
112+if [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
112113 pattern="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|"
113−elif [ $(grep -c -E "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]; then
114+elif [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
114115 pattern="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|"
115116else
116117 die "Couldn't figure out where out where to patch in new version in '$attr'!"
117118fi
118119
120+if [[ "$oldHash" =~ ^(sha256|sha512)[:-] ]]; then
121+ # Handle the possible SRI-style hash attribute (in the form ${type}${separator}${hash})
122+ # 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).
123+ # To keep this program reasonably simple, we will upgrade Nix’s format to SRI.
124+ oldHashAlgo="${BASH_REMATCH[1]}"
125+ sri=true
126+elif [[ "$oldHashAlgo" = "null" ]]; then
127+ # 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.
128+ die "Unable to figure out hashing scheme from '$oldHash' in '$attr'!"
129+fi
130+
131+case "$oldHashAlgo" in
132+ # Lengths of hex-encoded hashes
133+ sha256) hashLength=64 ;;
134+ sha512) hashLength=128 ;;
135+ *) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
136+esac
137+
138+# Make a temporary all-zeroes hash of $hashLength characters
139+tempHash=$(printf '%0*d' "$hashLength" 0)
140+
141+if [[ -n "$sri" ]]; then
142+ # SRI hashes only support base64
143+ # SRI hashes need to declare the hash type as part of the hash
144+ tempHash="$(nix to-sri --type "$oldHashAlgo" "$tempHash")"
145+fi
146+
119147# Replace new version
120148sed -i.bak "$nixFile" -re "$pattern"
121149if cmp -s "$nixFile" "$nixFile.bak"; then
@@ -123,7 +151,7 @@ if cmp -s "$nixFile" "$nixFile.bak"; then
123151fi
124152
125153# Replace new URL
126−if [ -n "$newUrl" ]; then
154+if [[ -n "$newUrl" ]]; then
127155 sed -i "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
128156
129157 if cmp -s "$nixFile" "$nixFile.bak"; then
@@ -131,33 +159,29 @@ if [ -n "$newUrl" ]; then
131159 fi
132160fi
133161
134−case "$oldHashAlgo" in
135− sha256) hashLength=64 ;;
136− sha512) hashLength=128 ;;
137− *) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
138−esac
139−
140−# Make a temporary all-zeroes hash of $hashLength characters
141−tempHash=$(printf '%0*d' "$hashLength" 0)
142−
143162sed -i "$nixFile" -re "s|\"$oldHash\"|\"$tempHash\"|"
144163if cmp -s "$nixFile" "$nixFile.bak"; then
145164 die "Failed to replace source hash of '$attr' to a temporary hash!"
146165fi
147166
148167# If new hash not given on the command line, recalculate it ourselves.
149−if [ -z "$newHash" ]; then
168+if [[ -z "$newHash" ]]; then
150169 nix-build $systemArg --no-out-link -A "$attr.src" 2>"$attr.fetchlog" >/dev/null || true
151170 # FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed
152− newHash=$(egrep -v "killing process|dependencies couldn't be built|wanted: " "$attr.fetchlog" | tail -n2 | sed "s~output path .* has .* hash ‘\(.*\)’ when .* was expected\|fixed-output derivation produced path '.*' with .* hash '\(.*\)' instead of the expected hash '.*'\| got: .*:\(.*\)~\1\2\3~" | head -n1)
171+ newHash=$(grep --extended-regexp --invert-match "killing process|dependencies couldn't be built|wanted: " "$attr.fetchlog" | tail -n2 | sed "s~output path .* has .* hash ‘\(.*\)’ when .* was expected\|fixed-output derivation produced path '.*' with .* hash '\(.*\)' instead of the expected hash '.*'\| got: .*:\(.*\)~\1\2\3~" | head -n1)
172+fi
173+
174+if [[ -n "$sri" ]]; then
175+ # nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type
176+ newHash="$(nix to-sri --type "$oldHashAlgo" "$newHash")"
153177fi
154178
155−if [ -z "$newHash" ]; then
179+if [[ -z "$newHash" ]]; then
156180 cat "$attr.fetchlog" >&2
157181 die "Couldn't figure out new hash of '$attr.src'!"
158182fi
159183
160−if [ -z "${ignoreSameHash}" ] && [ "$oldVersion" != "$newVersion" ] && [ "$oldHash" = "$newHash" ]; then
184+if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then
161185 mv "$nixFile.bak" "$nixFile"
162186 die "Both the old and new source hashes of '$attr.src' were equivalent. Please fix the package's source URL to be dependent on '\${version}'!"
163187fi