From 42fbde03835b90c52e630ed0c2211217e2f77181 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 18 Oct 2017 12:48:31 +0200 Subject: [PATCH] Use "nix cat-store" to serve files from the Nix store This makes downloading/viewing build results work with binary cache stores. For good performance, this should be used in conjunction with https://github.com/NixOS/nix/commit/ca580bec35ea4d1984e36864158d7be99cfcb34b, i.e. you should set store_uri to something like s3://my-cache?local-nar-cache=/tmp/nar-cache to cache NARs between requests. --- src/lib/Hydra/Controller/Build.pm | 40 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/lib/Hydra/Controller/Build.pm b/src/lib/Hydra/Controller/Build.pm index f3b6f49f..b96d0f2f 100644 --- a/src/lib/Hydra/Controller/Build.pm +++ b/src/lib/Hydra/Controller/Build.pm @@ -204,8 +204,6 @@ sub download : Chained('buildChain') PathPart { } my $storePath = $1; - notFound($c, "File " . $product->path . " does not exist.") unless -e $product->path; - return $c->res->redirect(defaultUriForProduct($self, $c, $product, @path)) if scalar @path == 0 && ($product->name || $product->defaultpath); @@ -221,22 +219,32 @@ sub download : Chained('buildChain') PathPart { my $path = $product->path; $path .= "/" . join("/", @path) if scalar @path > 0; - # Make sure the file is in the Nix store. - $path = checkPath($self, $c, $path); + if (isLocalStore) { - # If this is a directory but no "/" is attached, then redirect. - if (-d $path && substr($c->request->uri, -1) ne "/") { - return $c->res->redirect($c->request->uri . "/"); + notFound($c, "File " . $product->path . " does not exist.") unless -e $product->path; + + # Make sure the file is in the Nix store. + $path = checkPath($self, $c, $path); + + # If this is a directory but no "/" is attached, then redirect. + if (-d $path && substr($c->request->uri, -1) ne "/") { + return $c->res->redirect($c->request->uri . "/"); + } + + $path = "$path/index.html" if -d $path && -e "$path/index.html"; + + notFound($c, "File $path does not exist.") if !-e $path; + + notFound($c, "Path $path is a directory.") if -d $path; + + $c->serve_static_file($path); + $c->response->headers->last_modified($c->stash->{build}->stoptime); + + } else { + $c->stash->{'plain'} = { data => readNixFile($path) }; + $c->response->content_type('application/octet-stream'); + $c->forward('Hydra::View::Plain'); } - - $path = "$path/index.html" if -d $path && -e "$path/index.html"; - - notFound($c, "File $path does not exist.") if !-e $path; - - notFound($c, "Path $path is a directory.") if -d $path; - - $c->serve_static_file($path); - $c->response->headers->last_modified($c->stash->{build}->stoptime); }