2012-01-05 22:07:12 +01:00
|
|
|
#! @perl@ -w @perlFlags@
|
2004-04-21 16:54:05 +02:00
|
|
|
|
|
|
|
use strict;
|
2012-04-14 18:38:52 +02:00
|
|
|
use File::Basename;
|
2012-05-01 22:35:18 +02:00
|
|
|
use File::Path qw(mkpath);
|
2012-01-03 02:51:38 +01:00
|
|
|
use Nix::Config;
|
2004-04-21 16:54:05 +02:00
|
|
|
|
2012-01-03 02:51:38 +01:00
|
|
|
my $manifestDir = $Nix::Config::manifestDir;
|
2006-05-08 22:00:28 +02:00
|
|
|
|
2004-04-21 16:54:05 +02:00
|
|
|
|
2007-08-10 02:28:44 +02:00
|
|
|
# Turn on caching in nix-prefetch-url.
|
2012-01-03 02:51:38 +01:00
|
|
|
my $channelCache = "$Nix::Config::stateDir/channel-cache";
|
2007-08-10 02:28:44 +02:00
|
|
|
mkdir $channelCache, 0755 unless -e $channelCache;
|
2007-08-22 16:52:22 +02:00
|
|
|
$ENV{'NIX_DOWNLOAD_CACHE'} = $channelCache if -W $channelCache;
|
2007-08-10 02:28:44 +02:00
|
|
|
|
2004-04-21 16:54:05 +02:00
|
|
|
# Figure out the name of the `.nix-channels' file to use.
|
2012-04-14 18:38:52 +02:00
|
|
|
my $home = $ENV{"HOME"} or die '$HOME not set\n';
|
2004-04-21 16:54:05 +02:00
|
|
|
my $channelsList = "$home/.nix-channels";
|
2007-09-17 18:08:24 +02:00
|
|
|
my $nixDefExpr = "$home/.nix-defexpr";
|
2004-04-21 16:54:05 +02:00
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Figure out the name of the channels profile.
|
|
|
|
my $userName = getpwuid($<) or die "cannot figure out user name";
|
|
|
|
my $profile = "$Nix::Config::stateDir/profiles/per-user/$userName/channels";
|
2012-05-01 22:35:18 +02:00
|
|
|
mkpath(dirname $profile, 0, 0755);
|
2012-04-14 18:38:52 +02:00
|
|
|
|
|
|
|
my %channels;
|
2004-04-21 16:54:05 +02:00
|
|
|
|
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Reads the list of channels.
|
2004-04-21 16:54:05 +02:00
|
|
|
sub readChannels {
|
|
|
|
return if (!-f $channelsList);
|
2004-11-04 21:20:39 +01:00
|
|
|
open CHANNELS, "<$channelsList" or die "cannot open `$channelsList': $!";
|
2004-04-21 16:54:05 +02:00
|
|
|
while (<CHANNELS>) {
|
|
|
|
chomp;
|
2006-09-25 13:11:16 +02:00
|
|
|
next if /^\s*\#/;
|
2012-04-14 18:38:52 +02:00
|
|
|
my ($url, $name) = split ' ', $_;
|
|
|
|
$url =~ s/\/*$//; # remove trailing slashes
|
|
|
|
$name = basename $url unless defined $name;
|
|
|
|
$channels{$name} = $url;
|
2004-04-21 16:54:05 +02:00
|
|
|
}
|
|
|
|
close CHANNELS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Writes the list of channels.
|
2004-04-21 16:54:05 +02:00
|
|
|
sub writeChannels {
|
2004-11-04 21:20:39 +01:00
|
|
|
open CHANNELS, ">$channelsList" or die "cannot open `$channelsList': $!";
|
2012-04-14 18:38:52 +02:00
|
|
|
foreach my $name (keys %channels) {
|
|
|
|
print CHANNELS "$channels{$name} $name\n";
|
2004-04-21 16:54:05 +02:00
|
|
|
}
|
|
|
|
close CHANNELS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Adds a channel.
|
2004-04-21 16:54:05 +02:00
|
|
|
sub addChannel {
|
2012-04-14 18:38:52 +02:00
|
|
|
my ($url, $name) = @_;
|
2004-04-21 16:54:05 +02:00
|
|
|
readChannels;
|
2012-04-14 18:38:52 +02:00
|
|
|
$channels{$name} = $url;
|
2004-04-21 16:54:05 +02:00
|
|
|
writeChannels;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Remove a channel.
|
2005-02-17 11:06:12 +01:00
|
|
|
sub removeChannel {
|
2012-04-14 18:38:52 +02:00
|
|
|
my ($name) = @_;
|
2005-02-17 11:06:12 +01:00
|
|
|
readChannels;
|
2012-04-14 18:38:52 +02:00
|
|
|
delete $channels{$name};
|
2005-02-17 11:06:12 +01:00
|
|
|
writeChannels;
|
2012-04-14 18:38:52 +02:00
|
|
|
|
|
|
|
system("$Nix::Config::binDir/nix-env --profile '$profile' -e '$name'") == 0
|
|
|
|
or die "cannot remove channel `$name'";
|
2005-02-17 11:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Fetch Nix expressions and pull manifests from the subscribed
|
2004-04-21 16:54:05 +02:00
|
|
|
# channels.
|
|
|
|
sub update {
|
|
|
|
readChannels;
|
|
|
|
|
2009-11-13 11:08:31 +01:00
|
|
|
# Create the manifests directory if it doesn't exist.
|
2012-01-03 02:51:38 +01:00
|
|
|
mkdir $manifestDir, 0755 unless -e $manifestDir;
|
2009-11-13 11:08:31 +01:00
|
|
|
|
2012-02-02 13:25:56 +01:00
|
|
|
# Do we have write permission to the manifests directory?
|
|
|
|
die "$0: you do not have write permission to `$manifestDir'!\n" unless -W $manifestDir;
|
2006-05-08 22:00:28 +02:00
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Download each channel.
|
|
|
|
my $exprs = "";
|
|
|
|
foreach my $name (keys %channels) {
|
|
|
|
my $url = $channels{$name};
|
2012-04-18 13:40:18 +02:00
|
|
|
my $origUrl = "$url/MANIFEST";
|
2012-04-14 21:04:22 +02:00
|
|
|
|
|
|
|
# Check if $url is a redirect. If so, follow it now to ensure
|
|
|
|
# consistency if the redirection is changed between
|
|
|
|
# downloading the manifest and the tarball.
|
|
|
|
my $headers = `$Nix::Config::curl --silent --head '$url'`;
|
|
|
|
die "$0: unable to check `$url'\n" if $? != 0;
|
|
|
|
$headers =~ s/\r//g;
|
|
|
|
$url = $1 if $headers =~ /^Location:\s*(.*)\s*$/m;
|
2012-04-14 18:38:52 +02:00
|
|
|
|
|
|
|
# Pull the channel manifest.
|
2012-04-16 19:52:31 +02:00
|
|
|
$ENV{'NIX_ORIG_URL'} = $origUrl;
|
2012-02-02 13:25:56 +01:00
|
|
|
system("$Nix::Config::binDir/nix-pull", "--skip-wrong-store", "$url/MANIFEST") == 0
|
2012-04-14 18:38:52 +02:00
|
|
|
or die "cannot pull manifest from `$url'\n";
|
2007-05-02 01:16:38 +02:00
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Download the channel tarball.
|
2004-04-21 16:54:05 +02:00
|
|
|
my $fullURL = "$url/nixexprs.tar.bz2";
|
2012-04-14 18:38:52 +02:00
|
|
|
print STDERR "downloading Nix expressions from `$fullURL'...\n";
|
|
|
|
my ($hash, $path) = `PRINT_PATH=1 QUIET=1 $Nix::Config::binDir/nix-prefetch-url '$fullURL'`;
|
2005-04-07 16:35:44 +02:00
|
|
|
die "cannot fetch `$fullURL'" if $? != 0;
|
|
|
|
chomp $path;
|
2004-10-20 16:42:38 +02:00
|
|
|
|
2012-04-14 21:05:28 +02:00
|
|
|
# If the URL contains a version number, append it to the name
|
|
|
|
# attribute (so that "nix-env -q" on the channels profile
|
|
|
|
# shows something useful).
|
|
|
|
my $cname = $name;
|
|
|
|
$cname .= $1 if basename($url) =~ /(-\d.*)$/;
|
|
|
|
|
|
|
|
$exprs .= "'f: f { name = \"$cname\"; channelName = \"$name\"; src = builtins.storePath \"$path\"; }' ";
|
2012-04-14 18:38:52 +02:00
|
|
|
}
|
2004-04-21 16:54:05 +02:00
|
|
|
|
2012-04-14 18:38:52 +02:00
|
|
|
# Unpack the channel tarballs into the Nix store and install them
|
|
|
|
# into the channels profile.
|
|
|
|
print STDERR "unpacking channels...\n";
|
|
|
|
system("$Nix::Config::binDir/nix-env --profile '$profile' " .
|
|
|
|
"-f '<nix/unpack-channel.nix>' -i -E $exprs --quiet") == 0
|
|
|
|
or die "cannot unpack the channels";
|
2005-02-17 11:06:12 +01:00
|
|
|
|
2007-09-17 18:08:24 +02:00
|
|
|
# Make the channels appear in nix-env.
|
|
|
|
unlink $nixDefExpr if -l $nixDefExpr; # old-skool ~/.nix-defexpr
|
|
|
|
mkdir $nixDefExpr or die "cannot create directory `$nixDefExpr'" if !-e $nixDefExpr;
|
|
|
|
my $channelLink = "$nixDefExpr/channels";
|
|
|
|
unlink $channelLink; # !!! not atomic
|
2012-04-14 18:38:52 +02:00
|
|
|
symlink($profile, $channelLink) or die "cannot symlink `$channelLink' to `$profile'";
|
2005-02-17 11:06:12 +01:00
|
|
|
}
|
2004-10-20 16:42:38 +02:00
|
|
|
|
2005-02-17 11:06:12 +01:00
|
|
|
|
|
|
|
sub usageError {
|
|
|
|
print STDERR <<EOF;
|
|
|
|
Usage:
|
2012-04-14 18:38:52 +02:00
|
|
|
nix-channel --add URL [CHANNEL-NAME]
|
|
|
|
nix-channel --remove CHANNEL-NAME
|
2005-02-17 11:06:12 +01:00
|
|
|
nix-channel --list
|
|
|
|
nix-channel --update
|
|
|
|
EOF
|
|
|
|
exit 1;
|
2004-04-21 16:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-07 16:35:44 +02:00
|
|
|
usageError if scalar @ARGV == 0;
|
|
|
|
|
|
|
|
|
2004-04-21 16:54:05 +02:00
|
|
|
while (scalar @ARGV) {
|
|
|
|
my $arg = shift @ARGV;
|
|
|
|
|
|
|
|
if ($arg eq "--add") {
|
2012-04-14 18:38:52 +02:00
|
|
|
usageError if scalar @ARGV < 1 || scalar @ARGV > 2;
|
|
|
|
my $url = shift @ARGV;
|
|
|
|
my $name = shift @ARGV;
|
|
|
|
unless (defined $name) {
|
|
|
|
$name = basename $url;
|
|
|
|
$name =~ s/-unstable//;
|
|
|
|
$name =~ s/-stable//;
|
|
|
|
}
|
|
|
|
addChannel($url, $name);
|
2004-04-21 16:54:05 +02:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
2005-02-17 11:06:12 +01:00
|
|
|
if ($arg eq "--remove") {
|
|
|
|
usageError if scalar @ARGV != 1;
|
2012-04-14 18:38:52 +02:00
|
|
|
removeChannel(shift @ARGV);
|
2005-02-17 11:06:12 +01:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg eq "--list") {
|
|
|
|
usageError if scalar @ARGV != 0;
|
|
|
|
readChannels;
|
2012-04-14 18:38:52 +02:00
|
|
|
foreach my $name (keys %channels) {
|
|
|
|
print "$name $channels{$name}\n";
|
2005-02-17 11:06:12 +01:00
|
|
|
}
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
2004-04-21 16:54:05 +02:00
|
|
|
elsif ($arg eq "--update") {
|
2005-02-17 11:06:12 +01:00
|
|
|
usageError if scalar @ARGV != 0;
|
2004-04-21 16:54:05 +02:00
|
|
|
update;
|
|
|
|
last;
|
|
|
|
}
|
2005-02-17 11:06:12 +01:00
|
|
|
|
|
|
|
elsif ($arg eq "--help") {
|
|
|
|
usageError;
|
|
|
|
}
|
2004-04-21 16:54:05 +02:00
|
|
|
|
|
|
|
else {
|
2005-02-17 11:06:12 +01:00
|
|
|
die "unknown argument `$arg'; try `--help'";
|
2004-04-21 16:54:05 +02:00
|
|
|
}
|
|
|
|
}
|