From d788c091b6348dd897c438a1ec2ad40822170e3c Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 24 Apr 2021 02:50:53 +0200 Subject: [PATCH 1/4] common-updater-scripts: remove lib dependency So that the script can be used outside of nixpkgs. --- pkgs/common-updater/scripts/update-source-version | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkgs/common-updater/scripts/update-source-version b/pkgs/common-updater/scripts/update-source-version index 90543a9cfc6..831f65e4d90 100755 --- a/pkgs/common-updater/scripts/update-source-version +++ b/pkgs/common-updater/scripts/update-source-version @@ -99,11 +99,10 @@ if [[ -z "$oldUrl" ]]; then die "Couldn't evaluate source url from '$attr.src'!" fi -drvName=$(nix-instantiate $systemArg --eval -E "with import ./. {}; lib.getName $attr" | tr -d '"') -oldVersion=$(nix-instantiate $systemArg --eval -E "with import ./. {}; $attr.${versionKey} or (lib.getVersion $attr)" | tr -d '"') +oldVersion=$(nix-instantiate $systemArg --eval -E "with import ./. {}; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"') -if [[ -z "$drvName" || -z "$oldVersion" ]]; then - die "Couldn't evaluate name and version from '$attr.name'!" +if [[ -z "$oldVersion" ]]; then + die "Couldn't find out the old version of '$attr'!" fi if [[ "$oldVersion" = "$newVersion" ]]; then From 3c781e6d191b716e0f693d6ee214f11a1ae4f832 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 24 Apr 2021 02:56:20 +0200 Subject: [PATCH 2/4] common-updater-scripts: handle default.nix without arguments So that the script can be used outside of nixpkgs. For example, the default.nix recommended by flake-compat does not take arguments. --- pkgs/common-updater/scripts/update-source-version | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkgs/common-updater/scripts/update-source-version b/pkgs/common-updater/scripts/update-source-version index 831f65e4d90..0c914ebee2c 100755 --- a/pkgs/common-updater/scripts/update-source-version +++ b/pkgs/common-updater/scripts/update-source-version @@ -59,6 +59,9 @@ newVersion=${args[1]} newHash=${args[2]} newUrl=${args[3]} +# Third-party repositories might not accept arguments in their default.nix. +importTree="(let tree = import ./.; in if builtins.isFunction tree then tree {} else tree)" + if (( "${#args[*]}" < 2 )); then echo "$scriptName: Too few arguments" usage @@ -93,13 +96,13 @@ if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!" fi -oldUrl=$(nix-instantiate $systemArg --eval -E "with import ./. {}; builtins.elemAt ($attr.src.drvAttrs.urls or [ $attr.src.url ]) 0" | tr -d '"') +oldUrl=$(nix-instantiate $systemArg --eval -E "with $importTree; builtins.elemAt ($attr.src.drvAttrs.urls or [ $attr.src.url ]) 0" | tr -d '"') if [[ -z "$oldUrl" ]]; then die "Couldn't evaluate source url from '$attr.src'!" fi -oldVersion=$(nix-instantiate $systemArg --eval -E "with import ./. {}; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"') +oldVersion=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"') if [[ -z "$oldVersion" ]]; then die "Couldn't find out the old version of '$attr'!" @@ -114,7 +117,7 @@ if [[ "$oldVersion" = "$newVersion" ]]; then fi if [[ -n "$newRevision" ]]; then - oldRevision=$(nix-instantiate $systemArg --eval -E "with import ./. {}; $attr.src.rev" | tr -d '"') + oldRevision=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.src.rev" | tr -d '"') if [[ -z "$oldRevision" ]]; then die "Couldn't evaluate source revision from '$attr.src'!" fi From 38e20a3242a8dbd50d1c7d2dc060232edb7d907f Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 24 Apr 2021 03:42:01 +0200 Subject: [PATCH 3/4] common-updater-scripts: Replace flake source by local path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When update-source-version is run in a repo with flake-compat, it would find the files in Nix store. Let’s replace them with the local path of the repository. --- pkgs/common-updater/scripts/update-source-version | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkgs/common-updater/scripts/update-source-version b/pkgs/common-updater/scripts/update-source-version index 0c914ebee2c..15792e1502e 100755 --- a/pkgs/common-updater/scripts/update-source-version +++ b/pkgs/common-updater/scripts/update-source-version @@ -83,6 +83,14 @@ if [[ -z "$nixFile" ]]; then if [[ ! -f "$nixFile" ]]; then die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!" fi + + # flake-compat will return paths in the Nix store, we need to correct for that. + possiblyOutPath=$(nix-instantiate $systemArg --eval -E "with $importTree; outPath" | tr -d '"') + if [[ -n "$possiblyOutPath" ]]; then + outPathEscaped=$(echo "$possiblyOutPath" | sed 's#[$^*\\.[|]#\\&#g') + pwdEscaped=$(echo "$PWD" | sed 's#[$^*\\.[|]#\\&#g') + nixFile=$(echo "$nixFile" | sed "s|^$outPathEscaped|$pwdEscaped|") + fi fi oldHashAlgo=$(nix-instantiate $systemArg --eval --strict -A "$attr.src.drvAttrs.outputHashAlgo" | tr -d '"') From 3771de8ae019afbf0d37ffe390f3fec92cd2dffd Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 24 Apr 2021 04:36:58 +0200 Subject: [PATCH 4/4] common-updater-scripts: Support attribute lookup in flake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In flakes, packages are not exposed directly but instead they are declared inside “packages” or “legacyPackages” output under their host platform. flake-compat reflects this. Let’s look for an attribute also in these outputs if the direct lookup fails. --- .../scripts/update-source-version | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkgs/common-updater/scripts/update-source-version b/pkgs/common-updater/scripts/update-source-version index 15792e1502e..d5c23466ee0 100755 --- a/pkgs/common-updater/scripts/update-source-version +++ b/pkgs/common-updater/scripts/update-source-version @@ -19,6 +19,7 @@ args=() for arg in "$@"; do case $arg in --system=*) + system="${arg#*=}" systemArg="--system ${arg#*=}" ;; --version-key=*) @@ -78,6 +79,26 @@ if [[ -z "$versionKey" ]]; then versionKey=version fi +# 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 + if [[ -z "$nixFile" ]]; then nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/') if [[ ! -f "$nixFile" ]]; then