Skip to content

Commit

Permalink
nix-channel: Use binary caches advertised by channels
Browse files Browse the repository at this point in the history
Channels can now advertise a binary cache by creating a file
<channel-url>/binary-cache-url.  The channel unpacker puts these in
its "binary-caches" subdirectory.  Thus, the URLS of the binary caches
for the channels added by root appear in
/nix/var/nix/profiles/per-user/eelco/channels/binary-caches/*.  The
binary cache substituter reads these and adds them to the list of
binary caches.
  • Loading branch information
edolstra committed Aug 1, 2012
1 parent 79bba37 commit 5170c56
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
10 changes: 7 additions & 3 deletions corepkgs/unpack-channel.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ let
''
mkdir $out
cd $out
${bzip2} -d < $src | ${tar} xf - --warning=no-timestamp
${bzip2} -d < $src | ${tar} xf - --warning=no-timestamp
mv * $out/$channelName
if [ -n "$binaryCacheURL" ]; then
mkdir $out/binary-caches
echo -n "$binaryCacheURL" > $out/binary-caches/$channelName
fi
'';

in

{ name, channelName, src }:
{ name, channelName, src, binaryCacheURL ? "" }:

derivation {
system = builtins.currentSystem;
builder = shell;
args = [ "-e" builder ];
inherit name channelName src;
inherit name channelName src binaryCacheURL;

PATH = "${nixBinDir}:${coreutils}";

Expand Down
12 changes: 12 additions & 0 deletions doc/manual/conf-file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ build-use-chroot = /dev /proc /bin</programlisting>
</varlistentry>


<varlistentry><term><literal>binary-caches-files</literal></term>

<listitem><para>A list of names of files that will be read to
obtain additional binary cache URLs. The default is
<literal>/nix/var/nix/profiles/per-user/root/channels/binary-caches/*</literal>,
which ensures that Nix will use the binary caches corresponding to
the channels installed by root. Do not set this option to read
files created by untrusted users!</para></listitem>

</varlistentry>


<varlistentry><term><literal>trusted-binary-caches</literal></term>

<listitem><para>A list of URLs of binary caches, separated by
Expand Down
10 changes: 10 additions & 0 deletions scripts/download-from-binary-cache.pl.in
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ sub getAvailableCaches {
($Nix::Config::config{"binary-caches"}
// ($Nix::Config::storeDir eq "/nix/store" ? "http://nixos.org/binary-cache" : ""));

my $urlsFiles = $Nix::Config::config{"binary-cache-files"}
// "/nix/var/nix/profiles/per-user/root/channels/binary-caches/*";
foreach my $urlFile (glob $urlsFiles) {
next unless -f $urlFile;
open FILE, "<$urlFile" or die "cannot open ‘$urlFile’\n";
my $url = <FILE>; chomp $url;
close FILE;
push @urls, strToList($url);
}

# Allow Nix daemon users to override the binary caches to a subset
# of those listed in the config file. Note that ‘untrusted-*’
# denotes options passed by the client.
Expand Down
37 changes: 20 additions & 17 deletions scripts/nix-channel.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ my $nixDefExpr = "$home/.nix-defexpr";
my $userName = getpwuid($<) or die "cannot figure out user name";
my $profile = "$Nix::Config::stateDir/profiles/per-user/$userName/channels";
mkpath(dirname $profile, 0, 0755);

my %channels;


Expand Down Expand Up @@ -77,20 +77,14 @@ sub removeChannel {
# channels.
sub update {
my @channelNames = @_;

readChannels;

# Create the manifests directory if it doesn't exist.
mkdir $manifestDir, 0755 unless -e $manifestDir;

# Do we have write permission to the manifests directory?
die "$0: you do not have write permission to `$manifestDir'!\n" unless -W $manifestDir;
readChannels;

# Download each channel.
my $exprs = "";
foreach my $name (keys %channels) {
next if scalar @channelNames > 0 && ! grep { $_ eq $name } @{channelNames};

my $url = $channels{$name};
my $origUrl = "$url/MANIFEST";

Expand All @@ -101,11 +95,20 @@ sub update {
die "$0: unable to check `$url'\n" if $? != 0;
$headers =~ s/\r//g;
$url = $1 if $headers =~ /^Location:\s*(.*)\s*$/m;

# Pull the channel manifest.
$ENV{'NIX_ORIG_URL'} = $origUrl;
system("$Nix::Config::binDir/nix-pull", "--skip-wrong-store", "$url/MANIFEST") == 0
or die "cannot pull manifest from `$url'\n";

# Check if the channel advertises a binary cache.
my $binaryCacheURL = `$Nix::Config::curl --silent '$url'/binary-cache-url`;
my $extraAttrs = "";
if ($? == 0 && $binaryCacheURL ne "") {
$extraAttrs .= "binaryCacheURL = \"$binaryCacheURL\"; ";
} else {
# No binary cache, so pull the channel manifest.
mkdir $manifestDir, 0755 unless -e $manifestDir;
die "$0: you do not have write permission to `$manifestDir'!\n" unless -W $manifestDir;
$ENV{'NIX_ORIG_URL'} = $origUrl;
system("$Nix::Config::binDir/nix-pull", "--skip-wrong-store", "$url/MANIFEST") == 0
or die "cannot pull manifest from `$url'\n";
}

# Download the channel tarball.
my $fullURL = "$url/nixexprs.tar.bz2";
Expand All @@ -120,7 +123,7 @@ sub update {
my $cname = $name;
$cname .= $1 if basename($url) =~ /(-\d.*)$/;

$exprs .= "'f: f { name = \"$cname\"; channelName = \"$name\"; src = builtins.storePath \"$path\"; }' ";
$exprs .= "'f: f { name = \"$cname\"; channelName = \"$name\"; src = builtins.storePath \"$path\"; $extraAttrs }' ";
}

# Unpack the channel tarballs into the Nix store and install them
Expand Down Expand Up @@ -189,7 +192,7 @@ while (scalar @ARGV) {
update(@ARGV);
last;
}

elsif ($arg eq "--help") {
usageError;
}
Expand All @@ -198,7 +201,7 @@ while (scalar @ARGV) {
print "nix-channel (Nix) $Nix::Config::version\n";
exit 0;
}

else {
die "unknown argument `$arg'; try `--help'";
}
Expand Down

0 comments on commit 5170c56

Please sign in to comment.