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

View file

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