Merge pull request #330634 from r-ryantm/auto-update/circumflex
[NixPkgs.git] / pkgs / servers / http / nix-binary-cache / nix-binary-cache-start.in
blob13b62a8829dae4741fb64b2f2c2f7e60225aea51
1 #! @shell@
3 parse_opts () {
4   while @coreutils@/test -n "$1" && @coreutils@/test "x$1" != x-- ; do 
5     case "$1" in
6       --store-dir)
7         shift;
8         echo "STORE_DIR='$1'"
9         shift;
10         ;;
11       --priority)
12         shift;
13         echo "PRIORITY=$1";
14         shift;
15         ;;
16       --compression)
17         shift;
18         echo "COMPRESSION=$1";
19         shift;
20         ;;
21       --key)
22         shift;
23         echo "KEY=${1#*:}"
24         echo "KEYNAME=${1%%:*}"
25         shift;
26         ;;
27       --nix-remote)
28         shift;
29         echo "NIX_REMOTE=$1"
30         shift;
31         ;;
32       --mass-query)
33         shift;
34         echo "MASS_QUERY=$1"
35         shift;
36         ;;
37       --port)
38         shift;
39         echo "PORT=$1"
40         shift;
41         ;;
42       --ipv6)
43         shift;
44         echo "USE_IPV6=enable"
45         ;;
46       --help)
47         cat <<EOF >&2
48 "$0": start the Nix binary cache serving the Nix store dynamically.
50 Recognized options:
52 --port           server port
53 --store-dir      served Nix store
55 --priority       binary cache priority
56 --mass-query     0 or 1 - whether binary cache expects queries for nix-env -qas
58 --compression    compression to use: bzip2 or xz
59 --key            name:/path/to/key - key to use for narinfo signing
61 --nix-remote     'daemon' or empty string '' - whether to use daemon for store
62                    operations
64 --ipv6           enable ipv6
66 --help           show help and exit
67 EOF
68         exit 1;
69         ;;
70       *) shift ;;
71     esac;
72   done
75 workingdir="$(@coreutils@/mktemp -d)"
76 cd "$workingdir"
78 PORT=8080
79 (echo "STORE_DIR=${NIX_STORE_DIR:-/nix/store}"; parse_opts "$@" 
80   ) > nix-binary-cache.conf || exit
81 . "$workingdir/nix-binary-cache.conf"
83 echo "
84 server.port = $PORT
85 server.modules = ( \"mod_cgi\", \"mod_setenv\", )
86 server.document-root = \"$workingdir\"
87 cgi.assign = ( \".cgi\" => \"@shell@\" )
88 setenv.add-request-header = ( \"NIX_BINARY_CACHE_CONFIG\" => \"$workingdir/nix-binary-cache.conf\" )
89 " > lighttpd.conf
91 test -n "$USE_IPV6" && echo "
92 \$SERVER[\"socket\"] == \"[::]:$PORT\" {
93 server.use-ipv6 = \"enable\"
95 " >> lighttpd.conf
97 cp @out@/nix-binary-cache.cgi . 
98 cp @out@/nix-binary-cache.cgi ./nix-bc.cgi 
100 ip="$(@iproute@/ip a | @gnugrep@/grep 'inet .* scope global' | @coreutils@/head -n 1)"
101 ip="${ip%%/*}"
102 ip="${ip##* }"
104 url="http://$ip:$PORT/nix-bc.cgi?"
106 echo "Working directory: $workingdir"
107 echo
108 echo "Address of the binary cache: $url"
109 echo
110 echo "Usage example: NIX_REMOTE= nix-store --option binary-caches '$url'"
111 echo
112 echo
114 @lighttpd@/lighttpd -D -f "$workingdir/lighttpd.conf"
116 cd /
117 @coreutils@/rm -rf "$workingdir"