pkgs/den-http-get-updater: support list to prefetch

This commit is contained in:
Wroclaw 2025-04-07 18:37:34 +02:00
parent b188a5239c
commit 5cedeec18c
2 changed files with 90 additions and 43 deletions

View file

@ -10,14 +10,16 @@ let self = {
url = "https://github.com/nix-community/nixos-vscode-server/archive/${lock.nixos-vscode-server.revision}.tar.gz"; url = "https://github.com/nix-community/nixos-vscode-server/archive/${lock.nixos-vscode-server.revision}.tar.gz";
updateScript = pkgs.den-http-get-updater { updateScript = pkgs.den-http-get-updater {
fileLocation = lockFile; fileLocation = lockFile;
previousHash = lock.nixos-vscode-server.sha256;
previousVersion = lock.nixos-vscode-server.revision; previousVersion = lock.nixos-vscode-server.revision;
versionUrl = "https://api.github.com/repos/nix-community/nixos-vscode-server/commits"; versionUrl = "https://api.github.com/repos/nix-community/nixos-vscode-server/commits";
contentParser = "jq -rc '.[0].sha' <<< \"$newVersion\""; contentParser = "jq -rc '.[0].sha' <<< \"$newVersion\"";
prefetchList = [{
previousHash = lock.nixos-vscode-server.sha256;
prefetchUrlLocation = { prefetchUrlLocation = {
file = ./inputs.nix; file = ./inputs.nix;
attrpath = "nixos-vscode-server.url"; attrpath = "nixos-vscode-server.url";
}; };
}];
}; };
outPath = builtins.fetchTarball { outPath = builtins.fetchTarball {
inherit url; inherit url;
@ -29,13 +31,15 @@ let self = {
url = "https://github.com/NixOS/nixpkgs/archive/${lock.nixpkgs.revision}.tar.gz"; url = "https://github.com/NixOS/nixpkgs/archive/${lock.nixpkgs.revision}.tar.gz";
updateScript = pkgs.den-http-get-updater { updateScript = pkgs.den-http-get-updater {
fileLocation = lockFile; fileLocation = lockFile;
previousHash = lock.nixpkgs.sha256;
previousVersion = lock.nixpkgs.revision; previousVersion = lock.nixpkgs.revision;
versionUrl = "https://channels.nixos.org/nixos-24.11/git-revision"; versionUrl = "https://channels.nixos.org/nixos-24.11/git-revision";
prefetchList = [{
previousHash = lock.nixpkgs.sha256;
prefetchUrlLocation = { prefetchUrlLocation = {
file = ./inputs.nix; file = ./inputs.nix;
attrpath = "nixpkgs.url"; attrpath = "nixpkgs.url";
}; };
}];
}; };
outPath = builtins.fetchTarball { outPath = builtins.fetchTarball {
inherit url; inherit url;
@ -47,13 +51,15 @@ let self = {
url = "https://github.com/NixOS/nixpkgs/archive/${lock.nixpkgs-unstable.revision}.tar.gz"; url = "https://github.com/NixOS/nixpkgs/archive/${lock.nixpkgs-unstable.revision}.tar.gz";
updateScript = pkgs.den-http-get-updater { updateScript = pkgs.den-http-get-updater {
fileLocation = lockFile; fileLocation = lockFile;
previousHash = lock.nixpkgs-unstable.sha256;
previousVersion = lock.nixpkgs-unstable.revision; previousVersion = lock.nixpkgs-unstable.revision;
versionUrl = "https://channels.nixos.org/nixos-unstable/git-revision"; versionUrl = "https://channels.nixos.org/nixos-unstable/git-revision";
prefetchList = [{
previousHash = lock.nixpkgs-unstable.sha256;
prefetchUrlLocation = { prefetchUrlLocation = {
file = ./inputs.nix; file = ./inputs.nix;
attrpath = "nixpkgs-unstable.url"; attrpath = "nixpkgs-unstable.url";
}; };
}];
}; };
outPath = builtins.fetchTarball { outPath = builtins.fetchTarball {
inherit url; inherit url;
@ -65,14 +71,16 @@ let self = {
url = "https://github.com/lilyinstarlight/nixos-cosmic/archive/${lock.cosmic-modules.revision}.tar.gz"; url = "https://github.com/lilyinstarlight/nixos-cosmic/archive/${lock.cosmic-modules.revision}.tar.gz";
updateScript = pkgs.den-http-get-updater { updateScript = pkgs.den-http-get-updater {
fileLocation = lockFile; fileLocation = lockFile;
previousHash = lock.cosmic-modules.sha256;
previousVersion = lock.cosmic-modules.revision; previousVersion = lock.cosmic-modules.revision;
versionUrl = "https://api.github.com/repos/lilyinstarlight/nixos-cosmic/commits"; versionUrl = "https://api.github.com/repos/lilyinstarlight/nixos-cosmic/commits";
contentParser = "jq -rc '.[0].sha' <<< \"$newVersion\""; contentParser = "jq -rc '.[0].sha' <<< \"$newVersion\"";
prefetchList = [{
previousHash = lock.cosmic-modules.sha256;
prefetchUrlLocation = { prefetchUrlLocation = {
file = ./inputs.nix; file = ./inputs.nix;
attrpath = "cosmic-modules.url"; attrpath = "cosmic-modules.url";
}; };
}];
}; };
outPath = builtins.fetchTarball { outPath = builtins.fetchTarball {
inherit url; inherit url;

View file

@ -11,10 +11,20 @@
{ {
# location of file to modify # location of file to modify
fileLocation, fileLocation,
previousHash,
previousVersion, previousVersion,
versionUrl, versionUrl,
prefetchUrlLocation ? null,
# {
# fileLocation: string?;
# previousHash: string;
# prefetchUrlLocation: {
# file: string;
# attrpath: string[]'
# };
# }[]
#
prefetchList ? [],
# change newVersion variable in it, if the contents of the page # change newVersion variable in it, if the contents of the page
# is not plaintext version # is not plaintext version
# (json for example) # (json for example)
@ -24,23 +34,30 @@
name ? if unpack then "source" else null, name ? if unpack then "source" else null,
}: }:
assert builtins.isNull prefetchUrlLocation || lib.isAttrs prefetchUrlLocation;
assert lib.isAttrs prefetchUrlLocation && (
lib.isString prefetchUrlLocation.file or null ||
lib.isPath prefetchUrlLocation.file or null
);
assert lib.isAttrs prefetchUrlLocation && lib.isString prefetchUrlLocation.attrpath or null;
let let
realFileLocation = builtins.toString fileLocation; realFileLocation = builtins.toString fileLocation;
mark = builtins.hashString "sha256" previousHash;
mark' = lib.escapeShellArg mark; prefetchList' = lib.map (x:
prefetchUrlLocation' = lib.mapAttrs (_: lib.escapeShellArg) prefetchUrlLocation; assert builtins.isNull x.prefetchUrlLocation || lib.isAttrs x.prefetchUrlLocation;
assert lib.isAttrs x.prefetchUrlLocation && (
lib.isString x.prefetchUrlLocation.file or null ||
lib.isPath x.prefetchUrlLocation.file or null
);
assert lib.isAttrs x.prefetchUrlLocation && lib.isString x.prefetchUrlLocation.attrpath or null;
rec {
inherit fileLocation;
mark = builtins.hashString "sha256" x.previousHash;
markShellEscape = lib.escapeShellArg mark;
markShellRegexEscape = lib.escapeShellArg (lib.escapeRegex mark);
realFileLocation = builtins.toString x.fileLocation or fileLocation;
realFileLocationShellEscape = lib.escapeShellArg realFileLocation;
prefetchUrlLocationShellEscape = lib.mapAttrs (_: lib.escapeShellArg) x.prefetchUrlLocation;
previousHashShellRegexEscape = lib.escapeShellArg (lib.escapeRegex x.previousHash);
} // x) prefetchList;
realFileLocation' = lib.escapeShellArg realFileLocation; realFileLocation' = lib.escapeShellArg realFileLocation;
versionUrl' = lib.escapeShellArg versionUrl; versionUrl' = lib.escapeShellArg versionUrl;
mark'' = lib.escapeShellArg (lib.escapeRegex mark);
previousVersion'' = lib.escapeShellArg (lib.escapeRegex previousVersion); previousVersion'' = lib.escapeShellArg (lib.escapeRegex previousVersion);
nixUnpack = lib.optionalString unpack "--unpack"; nixUnpack = lib.optionalString unpack "--unpack";
@ -56,6 +73,7 @@ in
writeScript "den-http-get-updater" ('' writeScript "den-http-get-updater" (''
PATH="${lib.escapeShellArg path}" PATH="${lib.escapeShellArg path}"
prefetchFailed=
newVersion=$(curl -L "${versionUrl'}") newVersion=$(curl -L "${versionUrl'}")
if [[ "$?" != 0 ]]; then if [[ "$?" != 0 ]]; then
@ -63,15 +81,30 @@ writeScript "den-http-get-updater" (''
exit 1 exit 1
fi fi
newVersion=$(${contentParser}) newVersion=$(${contentParser})
awk -i inplace "{ awk -i inplace "{ sub(/${previousVersion''}/, \"$newVersion\") }1" "${realFileLocation'}"
sub(/${previousVersion''}/, \"$newVersion\") ''
# invalidate hash
sub(/${previousHash}/, \"${mark'}\") # invalidate hashes
}1" "${realFileLocation'}" + lib.concatStringsSep "\n" (lib.map ({
'' + lib.optionalString (!builtins.isNull prefetchUrlLocation) '' markShellEscape,
previousHash,
previousHashShellRegexEscape,
realFileLocationShellEscape,
...
}: ''
awk -i inplace "{ sub(/${previousHashShellRegexEscape}/, \"${markShellEscape}\") }1" "${realFileLocationShellEscape}"
'') prefetchList')
+ lib.concatStringsSep "\n" (lib.map ({
fileLocation,
markShellRegexEscape,
prefetchUrlLocationShellEscape,
realFileLocationShellEscape,
...
}: ''
nixUrlsResult=$(nix-instantiate --eval --json \ nixUrlsResult=$(nix-instantiate --eval --json \
"${prefetchUrlLocation'.file}" \ "${prefetchUrlLocationShellEscape.file}" \
-A "${prefetchUrlLocation'.attrpath}" -A "${prefetchUrlLocationShellEscape.attrpath}"
) )
urlsType=$(jq -rc 'type' <<< "$nixUrlsResult") urlsType=$(jq -rc 'type' <<< "$nixUrlsResult")
@ -93,14 +126,20 @@ writeScript "den-http-get-updater" (''
echo "prefetch succeeded!" echo "prefetch succeeded!"
echo "hash: $expectedHash" echo "hash: $expectedHash"
awk -i inplace "{ awk -i inplace "{
sub(/${mark''}/, \"$expectedHash\") sub(/${markShellRegexEscape}/, \"$expectedHash\")
}1" "${realFileLocation'}" }1" "${realFileLocationShellEscape}"
prefetchSucceeded= prefetchSucceeded=
break break
fi fi
done done
if [[ -n "$prefetchSucceeded" ]]; then if [[ -n "$prefetchSucceeded" ]]; then
echo "warning: prefetch failed" 1>&2 echo "warning: prefetch failed" 1>&2
prefetchFailed=1
fi
'') (lib.filter (x: !builtins.isNull x.prefetchUrlLocation) prefetchList'))
+ ''
if [[ -n "$prefetchFailed" ]]; then
exit 1 exit 1
fi fi
'') '')