about summary refs log tree commit diff
path: root/scripts/nix-channel.in
blob: 2bf5e633e85b47d7e3e47bdfe19d7ea2cd9dcda4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#! @perl@ -w @perlFlags@

use strict;
use File::Basename;
use File::Path qw(make_path);
use Nix::Config;

my $manifestDir = $Nix::Config::manifestDir;


# Turn on caching in nix-prefetch-url.
my $channelCache = "$Nix::Config::stateDir/channel-cache";
mkdir $channelCache, 0755 unless -e $channelCache;
$ENV{'NIX_DOWNLOAD_CACHE'} = $channelCache if -W $channelCache;

# Figure out the name of the `.nix-channels' file to use.
my $home = $ENV{"HOME"} or die '$HOME not set\n';
my $channelsList = "$home/.nix-channels";
my $nixDefExpr = "$home/.nix-defexpr";

# 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";
make_path(dirname $profile, mode => 0755);
    
my %channels;


# Reads the list of channels.
sub readChannels {
    return if (!-f $channelsList);
    open CHANNELS, "<$channelsList" or die "cannot open `$channelsList': $!";
    while (<CHANNELS>) {
        chomp;
        next if /^\s*\#/;
        my ($url, $name) = split ' ', $_;
        $url =~ s/\/*$//; # remove trailing slashes
        $name = basename $url unless defined $name;
        $channels{$name} = $url;
    }
    close CHANNELS;
}


# Writes the list of channels.
sub writeChannels {
    open CHANNELS, ">$channelsList" or die "cannot open `$channelsList': $!";
    foreach my $name (keys %channels) {
        print CHANNELS "$channels{$name} $name\n";
    }
    close CHANNELS;
}


# Adds a channel.
sub addChannel {
    my ($url, $name) = @_;
    readChannels;
    $channels{$name} = $url;
    writeChannels;
}


# Remove a channel.
sub removeChannel {
    my ($name) = @_;
    readChannels;
    delete $channels{$name};
    writeChannels;

    system("$Nix::Config::binDir/nix-env --profile '$profile' -e '$name'") == 0
        or die "cannot remove channel `$name'";
}


# Fetch Nix expressions and pull manifests from the subscribed
# channels.
sub update {
    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;

    # Download each channel.
    my $exprs = "";
    foreach my $name (keys %channels) {
        my $url = $channels{$name};
        my $origUrl = $url;

        # 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;
        
        # 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";

        # Download the channel tarball.
        my $fullURL = "$url/nixexprs.tar.bz2";
        print STDERR "downloading Nix expressions from `$fullURL'...\n";
        my ($hash, $path) = `PRINT_PATH=1 QUIET=1 $Nix::Config::binDir/nix-prefetch-url '$fullURL'`;
        die "cannot fetch `$fullURL'" if $? != 0;
        chomp $path;

        # 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\"; }' ";
    }

    # 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";

    # 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
    symlink($profile, $channelLink) or die "cannot symlink `$channelLink' to `$profile'";
}


sub usageError {
    print STDERR <<EOF;
Usage:
  nix-channel --add URL [CHANNEL-NAME]
  nix-channel --remove CHANNEL-NAME
  nix-channel --list
  nix-channel --update
EOF
    exit 1;
}


usageError if scalar @ARGV == 0;


while (scalar @ARGV) {
    my $arg = shift @ARGV;

    if ($arg eq "--add") {
        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);
        last;
    }

    if ($arg eq "--remove") {
        usageError if scalar @ARGV != 1;
        removeChannel(shift @ARGV);
        last;
    }

    if ($arg eq "--list") {
        usageError if scalar @ARGV != 0;
        readChannels;
        foreach my $name (keys %channels) {
            print "$name $channels{$name}\n";
        }
        last;
    }

    elsif ($arg eq "--update") {
        usageError if scalar @ARGV != 0;
        update;
        last;
    }
    
    elsif ($arg eq "--help") {
        usageError;
    }

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