generate-hashes: simplify target/subtarget parsing

The openwrt download site has secret json files like these with the
list of targets and subtargets:

https://downloads.openwrt.org/releases/21.02.3/targets/?json-targets

Use these rather than scanning the git repository, to avoid the two
getting out of sync.
This commit is contained in:
Thomas Nixon 2022-06-08 00:21:53 +01:00
parent 30d056862d
commit d28a2c402c
3 changed files with 21 additions and 54 deletions

View File

@ -101,12 +101,11 @@ in
## Refreshing hashes ## Refreshing hashes
Checksums of the `sha256sums` files on downloads.openwrt.org are add Checksums of the `sha256sums` files on downloads.openwrt.org are added
to this repository for a few recent releases. For updating, modify to this repository for a few recent releases. To update them, run:
`release` in `generate-hashes.nix`, then run:
```bash ```bash
nix-shell -p nixFlakes nix-shell -p nixFlakes
nix run .#generate-hashes nix run .#generate-hashes 21.02.3 # for example
git add hashes/*.nix git add hashes/*.nix
``` ```

View File

@ -32,7 +32,6 @@
# `nix run .#generate-hashes` # `nix run .#generate-hashes`
packages.x86_64-linux.generate-hashes = import ./generate-hashes.nix { packages.x86_64-linux.generate-hashes = import ./generate-hashes.nix {
pkgs = nixpkgs.legacyPackages.x86_64-linux; pkgs = nixpkgs.legacyPackages.x86_64-linux;
inherit openwrt;
}; };
packages.x86_64-linux.example-image = import ./example.nix { packages.x86_64-linux.example-image = import ./example.nix {

View File

@ -1,52 +1,24 @@
{ pkgs ? import <nixpkgs> {} { pkgs ? import <nixpkgs> {} }:
, openwrt
}:
with pkgs; with pkgs;
let
release = "21.02.3";
linuxTargets = builtins.attrNames (
lib.filterAttrs (name: type: type == "directory" && name != "generic") (
builtins.readDir (openwrt + "/target/linux")
)
);
linuxTargetDefs = builtins.foldl' (linuxTargetDefs: target:
linuxTargetDefs // {
"${target}" =
builtins.foldl' (targetDefs: line:
let
m = builtins.match "([[:upper:]]+)[[:space:]]*:=[[:space:]]*(.+)" line;
in
if builtins.isList m && builtins.length m == 2
then targetDefs // {
${builtins.elemAt m 0} = builtins.elemAt m 1;
}
else targetDefs
) {} (
lib.splitString "\n" (
builtins.replaceStrings [ "\\\n" ] [ "" ] (
builtins.readFile (openwrt + "/target/linux/${target}/Makefile")
)
)
);
}
) {} linuxTargets;
defaultFeeds = [ "base" "packages" "routing" "telephony" ];
in
writeScriptBin "generate-hashes" '' writeScriptBin "generate-hashes" ''
#! ${runtimeShell} #! ${runtimeShell}
PATH=${lib.makeBinPath [ jq curl nix ]}:$PATH PATH=${lib.makeBinPath [ jq curl nix ]}:$PATH
RELEASE=21.02.3
FEEDS="base packages routing telephony"
if [ $# -gt 0 ]; then
RELEASE=$1
fi
UPSTREAM_URL=https://downloads.openwrt.org
RELEASE_URL=$UPSTREAM_URL/releases/$RELEASE
hash() { hash() {
TARGET=$1 TARGET=$1
VARIANT=$2 VARIANT=$2
BASEURL=https://downloads.openwrt.org/releases/${release}/targets/$TARGET/$VARIANT BASEURL=$RELEASE_URL/targets/$TARGET/$VARIANT
SUM=$(nix-prefetch-url --type sha256 $BASEURL/sha256sums 2>/dev/null) SUM=$(nix-prefetch-url --type sha256 $BASEURL/sha256sums 2>/dev/null)
if [ -n "$SUM" ]; then if [ -n "$SUM" ]; then
echo " \"$TARGET\".\"$VARIANT\" = {" echo " \"$TARGET\".\"$VARIANT\" = {"
@ -54,8 +26,8 @@ hash() {
ARCH=$(curl -s $BASEURL/profiles.json | jq -r .arch_packages) ARCH=$(curl -s $BASEURL/profiles.json | jq -r .arch_packages)
[ $? -ne 0 ] && echo "failed to fetch or parse $BASEURL/profiles.json" > /dev/stderr [ $? -ne 0 ] && echo "failed to fetch or parse $BASEURL/profiles.json" > /dev/stderr
if [ -n "$ARCH" ]; then if [ -n "$ARCH" ]; then
for FEED in ${lib.escapeShellArgs defaultFeeds}; do for FEED in $FEEDS; do
PACKAGES=$(nix-prefetch-url --type sha256 https://downloads.openwrt.org/releases/${release}/packages/$ARCH/$FEED/Packages 2>/dev/null) PACKAGES=$(nix-prefetch-url --type sha256 $RELEASE_URL/packages/$ARCH/$FEED/Packages 2>/dev/null)
echo " feedsSha256.$FEED = \"$PACKAGES\";" echo " feedsSha256.$FEED = \"$PACKAGES\";"
done done
fi fi
@ -67,12 +39,9 @@ mkdir -p hashes
( (
echo "{" echo "{"
${lib.concatMapStrings (target: curl -s $RELEASE_URL/targets/?json-targets | jq -r .[] | while IFS=/ read TARGET VARIANT; do
lib.concatMapStrings (variant: '' hash $TARGET $VARIANT
hash ${target} ${variant} done
'')
(lib.splitString " " (linuxTargetDefs.${target}.SUBTARGETS or "generic"))
) linuxTargets}
echo "}" echo "}"
) > hashes/${release}.nix ) > hashes/$RELEASE.nix
'' ''