biglybt: 3.5.0.0 -> 3.6.0.0
[NixPkgs.git] / pkgs / servers / http / nix-binary-cache / nix-binary-cache.cgi.in
blob890e68765b4093aed95e6e289f908a6396e85e6a
1 #! @shell@
3 STORE_DIR="${NIX_STORE_DIR:-/nix/store}"
4 MASS_QUERY=0
5 PRIORITY=75
6 COMPRESSION=bzip2
7 KEY=
8 KEYNAME=na
10 export NIX_REMOTE=daemon
12 config="${NIX_BINARY_CACHE_CONFIG:-${HTTP_NIX_BINARY_CACHE_CONFIG:-/etc/nix/nix-binary-cache.cgi.conf}}"
13 config="$(cd "$(@coreutils@/dirname "$config")";
14   @coreutils@/pwd)/$(@coreutils@/basename "$config")"
15 @coreutils@/test -e "$config" && . "$config"
17 header(){
18         echo "Content-Type: text/plain; charset=utf-8"
19         echo
22 header404(){
23         echo "Status: 404 Not Found"
24         echo
27 clean_path() {
28         @gnused@/sed -re "s@^$STORE_DIR/?@@" | @findutils@/xargs
31 storeq(){
32         @nix@/nix-store -q "$@"
35 sign(){
36         test -n "$1" &&
37           @coreutils@/sha256sum | @gnused@/sed -e 's/ .*//' |
38           @openssl@/openssl rsautl -sign -inkey "$@" | @coreutils@/base64 -w 0
41 case "$QUERY_STRING" in
42         "")
43                 header
44                 echo "Hello, this is a dynamically-generated Nix binary cache"
45                 ;;
46         /debug)
47                 header
48                 set
49                 ;;
50         /nix-cache-info)
51                 header
52                 echo "StoreDir: $STORE_DIR"
53                 echo "WantMassQuery: $MASS_QUERY"
54                 echo "Priority: $PRIORITY"
55                 ;;
56         *.narinfo)
57                 hash=${QUERY_STRING%.narinfo}
58                 hash=${hash#/}
59                 path="$(echo "$STORE_DIR/$hash-"* | @coreutils@/sort | @coreutils@/head -n 1)"
60                 if [ -n "$path" ] && [ -e "$path" ]; then
61                         header
62                         info="$(
63                         echo "StorePath: $path"
64                         echo "URL: $(@coreutils@/basename "$path"
65                           ).nar.$COMPRESSION"
66                         echo "Compression: $COMPRESSION"
67                         echo "NarHash: $(storeq --hash "$path")"
68                         echo "NarSize: $(storeq --size "$path")"
69                         echo "References: $(storeq --references "$path" |
70                           @coreutils@/tac | clean_path )"
71                         echo "Deriver: $(storeq --deriver "$path" |
72                           clean_path )"
73                         )"
74                         signature="$(echo "$info" | sign "$KEY")"
76                         echo "$info"
77                         echo "Signature: 1;$KEYNAME;$signature"
79                 else
80                         header404
81                         exit 1
82                 fi
83                 ;;
84         *.nar.xz)
85                 path="$STORE_DIR${QUERY_STRING%.nar.xz}"
86                 if [ -n "$path" ] && [ -e "$path" ]; then
87                         header
88                         @nix@/nix-store --dump "$path" | @xz@/xz
89                 else
90                         header404
91                         exit 1
92                 fi
93                 ;;
94         *.nar.bzip2)
95                 path="$STORE_DIR${QUERY_STRING%.nar.bzip2}"
96                 echo "$path" >&2;
97                 if [ -n "$path" ] && [ -e "$path" ]; then
98                         header
99                         @nix@/nix-store --dump "$path" | @bzip2@/bzip2
100                 else
101                         header404
102                         exit 1
103                 fi
104                 ;;
105 esac