nixpkgs/pkgs/servers/http/nix-binary-cache/nix-binary-cache.cgi.in

106 lines
2.2 KiB
Plaintext
Raw Normal View History

#! @shell@
STORE_DIR="${NIX_STORE_DIR:-/nix/store}"
MASS_QUERY=0
PRIORITY=75
COMPRESSION=bzip2
KEY=
KEYNAME=na
export NIX_REMOTE=daemon
config="${NIX_BINARY_CACHE_CONFIG:-${HTTP_NIX_BINARY_CACHE_CONFIG:-/etc/nix/nix-binary-cache.cgi.conf}}"
2015-01-03 13:55:56 +00:00
config="$(cd "$(@coreutils@/dirname "$config")";
@coreutils@/pwd)/$(@coreutils@/basename "$config")"
@coreutils@/test -e "$config" && . "$config"
header(){
echo "Content-Type: text/plain; charset=utf-8"
2015-01-03 13:55:56 +00:00
echo
}
header404(){
echo "Status: 404 Not Found"
echo
}
clean_path() {
@gnused@/sed -re "s@^$STORE_DIR/?@@" | @findutils@/xargs
}
storeq(){
@nix@/nix-store -q "$@"
}
sign(){
2015-01-03 13:55:56 +00:00
test -n "$1" &&
@coreutils@/sha256sum | @gnused@/sed -e 's/ .*//' |
@openssl@/openssl rsautl -sign -inkey "$@" | @coreutils@/base64 -w 0
}
2015-01-03 13:55:56 +00:00
case "$QUERY_STRING" in
"")
header
echo "Hello, this is a dynamically-generated Nix binary cache"
;;
/debug)
header
set
;;
/nix-cache-info)
header
echo "StoreDir: $STORE_DIR"
echo "WantMassQuery: $MASS_QUERY"
echo "Priority: $PRIORITY"
;;
*.narinfo)
hash=${QUERY_STRING%.narinfo}
hash=${hash#/}
2014-12-20 18:49:02 +00:00
path="$(echo "$STORE_DIR/$hash-"* | @coreutils@/sort | @coreutils@/head -n 1)"
if [ -n "$path" ] && [ -e "$path" ]; then
header
info="$(
echo "StorePath: $path"
echo "URL: $(@coreutils@/basename "$path"
).nar.$COMPRESSION"
echo "Compression: $COMPRESSION"
echo "NarHash: $(storeq --hash "$path")"
echo "NarSize: $(storeq --size "$path")"
2015-01-03 13:55:56 +00:00
echo "References: $(storeq --references "$path" |
@coreutils@/tac | clean_path )"
2015-01-03 13:55:56 +00:00
echo "Deriver: $(storeq --deriver "$path" |
clean_path )"
)"
signature="$(echo "$info" | sign "$KEY")"
echo "$info"
echo "Signature: 1;$KEYNAME;$signature"
else
2015-01-03 13:55:56 +00:00
header404
exit 1
fi
;;
*.nar.xz)
path="$STORE_DIR${QUERY_STRING%.nar.xz}"
if [ -n "$path" ] && [ -e "$path" ]; then
header
@nix@/nix-store --dump "$path" | @xz@/xz
else
2015-01-03 13:55:56 +00:00
header404
exit 1
fi
;;
*.nar.bzip2)
path="$STORE_DIR${QUERY_STRING%.nar.bzip2}"
echo "$path" >&2;
if [ -n "$path" ] && [ -e "$path" ]; then
header
@nix@/nix-store --dump "$path" | @bzip2@/bzip2
else
2015-01-03 13:55:56 +00:00
header404
exit 1
fi
;;
esac