4 scriptName
=update-source-version
# do not use the .wrapped name
7 echo "$scriptName: error: $1" >&2
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]"
17 echo "The <version> positional argument is also optional when passing --ignore-same-version."
27 systemArg
="--system ${arg#*=}"
30 versionKey
="${arg#*=}"
37 if [[ ! -f "$nixFile" ]]; then
38 die
"Could not find provided file $nixFile"
42 newRevision
="${arg#*=}"
47 --ignore-same-version)
48 ignoreSameVersion
="true"
58 echo "$scriptName: Unknown argument: $arg"
63 args
["${#args[*]}"]=$arg
73 # Third-party repositories might not accept arguments in their default.nix.
74 importTree
="(let tree = import ./.; in if builtins.isFunction tree then tree {} else tree)"
76 if [[ -z "$ignoreSameVersion" ]]; then
82 if (( "${#args[*]}" < $requiredArgs )); then
83 echo "$scriptName: Too few arguments"
88 if (( "${#args[*]}" > 4 )); then
89 echo "$scriptName: Too many arguments"
94 if [[ -z "$versionKey" ]]; then
98 if [[ -z "$sourceKey" ]]; then
102 # Allow finding packages among flake outputs in repos using flake-compat.
103 pname
=$
(nix-instantiate
$systemArg --eval --strict -A "$attr.name" ||
echo)
104 if [[ -z "$pname" ]]; then
105 if [[ -z "$system" ]]; then
106 system
=$
(nix-instantiate
--eval -E 'builtins.currentSystem' |
tr -d '"')
109 pname
=$
(nix-instantiate
$systemArg --eval --strict -A "packages.$system.$attr.name" ||
echo)
110 if [[ -n "$pname" ]]; then
111 attr
="packages.$system.$attr"
113 pname
=$
(nix-instantiate
$systemArg --eval --strict -A "legacyPackages.$system.$attr.name" ||
echo)
114 if [[ -n "$pname" ]]; then
115 attr
="legacyPackages.$system.$attr"
117 die
"Could not find attribute '$attr'!"
122 if [[ -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!"
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|")
137 oldHashAlgo
=$
(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/-.*//")
138 oldHash
=$
(nix-instantiate
$systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHash" |
tr -d '"')
140 if [[ -z "$oldHashAlgo" ||
-z "$oldHash" ]]; then
141 die
"Couldn't evaluate old source hash from '$attr.$sourceKey'!"
144 if [[ $
(grep --count "$oldHash" "$nixFile") != 1 ]]; then
145 die
"Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!"
148 oldVersion
=$
(nix-instantiate
$systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" |
tr -d '"')
150 if [[ -z "$oldVersion" ]]; then
151 die
"Couldn't find out the old version of '$attr'!"
154 if [[ -n "$ignoreSameVersion" && -z "$newVersion" ]]; then
155 newVersion
="$oldVersion"
158 if [[ -z "$ignoreSameVersion" && "$oldVersion" = "$newVersion" ]]; then
159 echo "$scriptName: New version same as old version, nothing to do." >&2
160 if [ -n "$printChanges" ]; then
166 if [[ -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'!"
173 # Escape regex metacharacter that are allowed in store path names
174 oldVersionEscaped
=$
(echo "$oldVersion" |
sed -re 's|[.+]|\\&|g')
176 if [[ $
(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
177 pattern
="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|"
178 elif [[ $
(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
179 pattern
="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|"
181 die
"Couldn't figure out where out where to patch in new version in '$attr'!"
184 if [[ "$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]}"
190 elif [[ "$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'!"
195 case "$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
=AzH1rZFqEH8sovZZfJykvsEmCedEZWigQFHWHl
6/PdE
= ;;
201 sha512
) tempHash
=KFj9Fvco4AuCgLJIGRnVzyssRf7VGP2oi5CkH6ADvj75ow3am3h8pxefOgQlO
+i33Q
/BBnG
/ST
/F7B
/0BvWHxw
== ;;
202 *) die
"Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
205 if [[ -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!"
213 # Escape regex metacharacter that are allowed in hashes (+)
214 oldHashEscaped
=$
(echo "$oldHash" |
sed -re 's|[+]|\\&|g')
215 tempHashEscaped
=$
(echo "$tempHash" |
sed -re 's|[+]|\\&|g')
217 if [[ "$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'!"
226 if [[ -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'!"
232 # Escape regex metacharacter that are allowed in store path names
233 oldUrlEscaped
=$
(echo "$oldUrl" |
sed -re 's|[${}.+]|\\&|g')
235 sed -i.
cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
236 if cmp -s "$nixFile" "$nixFile.cmp"; then
237 die
"Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!"
241 sed -i.
cmp "$nixFile" -re "s|\"$oldHashEscaped\"|\"$tempHash\"|"
242 if cmp -s "$nixFile" "$nixFile.cmp"; then
243 die
"Failed to replace source hash of '$attr' to a temporary hash!"
246 # Replace new revision, if given
247 if [[ -n "$newRevision" ]]; then
248 sed -i.
cmp "$nixFile" -re "s|\"$oldRevision\"|\"$newRevision\"|"
249 if cmp -s "$nixFile" "$nixFile.cmp"; then
250 die
"Failed to replace source revision '$oldRevision' to '$newRevision' in '$attr'!"
254 # If new hash not given on the command line, recalculate it ourselves.
255 if [[ -z "$newHash" ]]; then
256 nix-build
$systemArg --no-out-link -A "$attr.$sourceKey" 2>"$attr.fetchlog" >/dev
/null || true
257 # FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed
259 sed '1,/hash mismatch in fixed-output derivation/d' "$attr.fetchlog" \
260 |
grep --perl-regexp --only-matching 'got: +.+[:-]\K.+' \
261 || true
# handled below
264 if [[ -n "$newHash" && -n "$sri" ]]; then
265 # nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type
266 newHash
="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null \
267 || nix to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null)" \
268 || die
"Failed to convert hash to SRI representation!"
272 if [[ -z "$newHash" ]]; then
273 cat "$attr.fetchlog" >&2
274 die
"Couldn't figure out new hash of '$attr.$sourceKey'!"
277 if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then
278 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}'!"
281 sed -i.
cmp "$nixFile" -re "s|\"$tempHashEscaped\"|\"$newHash\"|"
282 if cmp -s "$nixFile" "$nixFile.cmp"; then
283 die
"Failed to replace temporary source hash of '$attr' to the final source hash!"
287 rm -f "$attr.fetchlog"
289 if [ -n "$printChanges" ]; then
290 printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile"