Skip to content

Instantly share code, notes, and snippets.

@phaer
Created August 13, 2022 09:21
Show Gist options
  • Save phaer/e8db16234407513ef8de647378c8ef33 to your computer and use it in GitHub Desktop.
Save phaer/e8db16234407513ef8de647378c8ef33 to your computer and use it in GitHub Desktop.
Fetch a file from a nix binary cache via HTTP, given its store path
#!/usr/bin/env bash
# TODO: run in nix shell gitlab:abstract-binary/nix-nar-rs, nix-shell does not support
# flakes, nix shell no shebangs atm.
STORE_PATH="/nix/store/4q7ij8ivy09vbhini8j0gzd7f77z0bwn-disk-image"
FILE_NAME="nixos.root.qcow2"
CACHE_URL="https://nix-dabei.cachix.org"
HASH="$(echo "$STORE_PATH" | sed 's|^/nix/store/\([^-]*\)-.*$|\1|')"
NAR_INFO_URL="$CACHE_URL/$HASH.narinfo"
NAR_URL="$CACHE_URL/$(curl -s "$NAR_INFO_URL" | awk '$1 == "URL:" {print $2}')"
curl "$NAR_URL" \
| xz -d \
> "$HASH.nar"
nix-nar cat "$HASH.nar" "/$FILE_NAME" \
> "$FILE_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment