2003-11-19 18:27:16 +01:00
|
|
|
use strict;
|
|
|
|
use Cwd;
|
2004-04-06 13:42:28 +02:00
|
|
|
use IO::Handle;
|
|
|
|
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
|
|
|
my $out = $ENV{"out"};
|
|
|
|
mkdir "$out", 0755 || die "error creating $out";
|
2003-11-19 18:27:16 +01:00
|
|
|
|
|
|
|
|
2006-09-25 17:07:45 +02:00
|
|
|
my $symlinks = 0;
|
|
|
|
|
2007-04-28 01:28:44 +02:00
|
|
|
my %priorities;
|
|
|
|
|
2006-09-25 17:07:45 +02:00
|
|
|
|
2003-11-19 18:27:16 +01:00
|
|
|
# For each activated package, create symlinks.
|
|
|
|
|
|
|
|
sub createLinks {
|
2004-04-06 13:42:28 +02:00
|
|
|
my $srcDir = shift;
|
|
|
|
my $dstDir = shift;
|
2007-04-28 01:28:44 +02:00
|
|
|
my $priority = shift;
|
2004-04-06 13:42:28 +02:00
|
|
|
|
|
|
|
my @srcFiles = glob("$srcDir/*");
|
|
|
|
|
|
|
|
foreach my $srcFile (@srcFiles) {
|
|
|
|
my $baseName = $srcFile;
|
|
|
|
$baseName =~ s/^.*\///g; # strip directory
|
|
|
|
my $dstFile = "$dstDir/$baseName";
|
2006-09-25 17:07:45 +02:00
|
|
|
|
2010-05-03 15:13:38 +02:00
|
|
|
# The files below are special-cased so that they don't show up
|
|
|
|
# in user profiles, either because they are useless, or
|
|
|
|
# because they would cause pointless collisions (e.g., each
|
|
|
|
# Python package brings its own
|
|
|
|
# `$out/lib/pythonX.Y/site-packages/easy-install.pth'.)
|
2006-09-25 17:07:45 +02:00
|
|
|
# Urgh, hacky...
|
2010-05-03 15:13:38 +02:00
|
|
|
if ($srcFile =~ /\/propagated-build-inputs$/ ||
|
2004-09-19 17:53:37 +02:00
|
|
|
$srcFile =~ /\/nix-support$/ ||
|
2005-03-07 14:27:56 +01:00
|
|
|
$srcFile =~ /\/perllocal.pod$/ ||
|
2005-07-22 22:37:39 +02:00
|
|
|
$srcFile =~ /\/info\/dir$/ ||
|
2004-09-19 17:53:37 +02:00
|
|
|
$srcFile =~ /\/log$/)
|
2004-04-06 13:42:28 +02:00
|
|
|
{
|
2004-10-25 18:54:56 +02:00
|
|
|
# Do nothing.
|
2012-12-04 14:20:36 +01:00
|
|
|
}
|
2004-04-06 13:42:28 +02:00
|
|
|
|
|
|
|
elsif (-d $srcFile) {
|
|
|
|
|
|
|
|
lstat $dstFile;
|
2012-12-04 14:20:36 +01:00
|
|
|
|
2004-04-06 13:42:28 +02:00
|
|
|
if (-d _) {
|
2007-04-28 01:28:44 +02:00
|
|
|
createLinks($srcFile, $dstFile, $priority);
|
2004-04-06 13:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
elsif (-l _) {
|
|
|
|
my $target = readlink $dstFile or die;
|
|
|
|
if (!-d $target) {
|
2013-08-10 23:36:16 +02:00
|
|
|
die "collision between directory `$srcFile' and non-directory `$target'";
|
2004-04-06 13:42:28 +02:00
|
|
|
}
|
|
|
|
unlink $dstFile or die "error unlinking `$dstFile': $!";
|
2012-12-04 14:20:36 +01:00
|
|
|
mkdir $dstFile, 0755 ||
|
2004-04-06 13:42:28 +02:00
|
|
|
die "error creating directory `$dstFile': $!";
|
2007-05-02 21:38:02 +02:00
|
|
|
createLinks($target, $dstFile, $priorities{$dstFile});
|
2007-04-28 01:28:44 +02:00
|
|
|
createLinks($srcFile, $dstFile, $priority);
|
2003-11-19 18:27:16 +01:00
|
|
|
}
|
2004-04-06 13:42:28 +02:00
|
|
|
|
|
|
|
else {
|
|
|
|
symlink($srcFile, $dstFile) ||
|
|
|
|
die "error creating link `$dstFile': $!";
|
2007-04-28 01:28:44 +02:00
|
|
|
$priorities{$dstFile} = $priority;
|
2006-09-25 17:07:45 +02:00
|
|
|
$symlinks++;
|
2004-04-06 13:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-28 01:28:44 +02:00
|
|
|
else {
|
|
|
|
|
|
|
|
if (-l $dstFile) {
|
2006-09-25 17:07:45 +02:00
|
|
|
my $target = readlink $dstFile;
|
2007-04-28 01:28:44 +02:00
|
|
|
my $prevPriority = $priorities{$dstFile};
|
2013-08-10 23:36:16 +02:00
|
|
|
die ( "collision between `$srcFile' and `$target'; "
|
2012-12-04 14:45:32 +01:00
|
|
|
. "use `nix-env --set-flag "
|
2007-04-28 01:28:44 +02:00
|
|
|
. "priority NUMBER PKGNAME' to change the priority of "
|
2012-12-04 14:45:32 +01:00
|
|
|
. "one of the conflicting packages\n" )
|
2007-04-28 01:28:44 +02:00
|
|
|
if $prevPriority == $priority;
|
|
|
|
next if $prevPriority < $priority;
|
|
|
|
unlink $dstFile or die;
|
2006-09-25 17:07:45 +02:00
|
|
|
}
|
2012-12-04 14:20:36 +01:00
|
|
|
|
2004-04-06 13:42:28 +02:00
|
|
|
symlink($srcFile, $dstFile) ||
|
|
|
|
die "error creating link `$dstFile': $!";
|
2007-04-28 01:28:44 +02:00
|
|
|
$priorities{$dstFile} = $priority;
|
2006-09-25 17:07:45 +02:00
|
|
|
$symlinks++;
|
2003-11-19 18:27:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-06 13:42:28 +02:00
|
|
|
|
2003-11-19 18:27:16 +01:00
|
|
|
my %done;
|
2006-09-25 17:07:45 +02:00
|
|
|
my %postponed;
|
2003-11-19 18:27:16 +01:00
|
|
|
|
2005-03-07 14:27:56 +01:00
|
|
|
sub addPkg;
|
2003-11-19 18:27:16 +01:00
|
|
|
sub addPkg {
|
2004-04-06 13:42:28 +02:00
|
|
|
my $pkgDir = shift;
|
2007-04-28 01:28:44 +02:00
|
|
|
my $priority = shift;
|
2003-11-19 18:27:16 +01:00
|
|
|
|
2004-04-06 13:42:28 +02:00
|
|
|
return if (defined $done{$pkgDir});
|
|
|
|
$done{$pkgDir} = 1;
|
2003-11-19 18:27:16 +01:00
|
|
|
|
2006-09-25 17:07:45 +02:00
|
|
|
# print "symlinking $pkgDir\n";
|
2007-04-28 01:28:44 +02:00
|
|
|
createLinks("$pkgDir", "$out", $priority);
|
2005-03-07 14:27:56 +01:00
|
|
|
|
2006-09-25 17:11:59 +02:00
|
|
|
my $propagatedFN = "$pkgDir/nix-support/propagated-user-env-packages";
|
2005-03-07 14:27:56 +01:00
|
|
|
if (-e $propagatedFN) {
|
|
|
|
open PROP, "<$propagatedFN" or die;
|
|
|
|
my $propagated = <PROP>;
|
|
|
|
close PROP;
|
|
|
|
my @propagated = split ' ', $propagated;
|
|
|
|
foreach my $p (@propagated) {
|
2006-09-25 17:07:45 +02:00
|
|
|
$postponed{$p} = 1 unless defined $done{$p};
|
2005-03-07 14:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-19 18:27:16 +01:00
|
|
|
}
|
|
|
|
|
2004-04-06 13:42:28 +02:00
|
|
|
|
2007-04-28 01:28:44 +02:00
|
|
|
# Convert the stuff we get from the environment back into a coherent
|
|
|
|
# data type.
|
2012-12-04 14:45:32 +01:00
|
|
|
my @pkgs;
|
2012-12-04 14:20:36 +01:00
|
|
|
my @derivations = split ' ', $ENV{"derivations"};
|
|
|
|
while (scalar @derivations) {
|
|
|
|
my $active = shift @derivations;
|
|
|
|
my $priority = shift @derivations;
|
|
|
|
my $outputs = shift @derivations;
|
|
|
|
for (my $n = 0; $n < $outputs; $n++) {
|
|
|
|
my $path = shift @derivations;
|
2012-12-04 14:45:32 +01:00
|
|
|
push @pkgs,
|
|
|
|
{ path => $path
|
|
|
|
, active => $active ne "false"
|
2012-12-04 14:20:36 +01:00
|
|
|
, priority => int($priority) };
|
|
|
|
}
|
2007-04-28 01:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Symlink to the packages that have been installed explicitly by the
|
2012-12-04 14:45:32 +01:00
|
|
|
# user. Process in priority order to reduce unnecessary
|
|
|
|
# symlink/unlink steps.
|
|
|
|
@pkgs = sort { $a->{priority} <=> $b->{priority} || $a->{path} cmp $b->{path} } @pkgs;
|
|
|
|
foreach my $pkg (@pkgs) {
|
2007-04-28 01:28:44 +02:00
|
|
|
#print $pkg, " ", $pkgs{$pkg}->{priority}, "\n";
|
2012-12-04 14:45:32 +01:00
|
|
|
addPkg($pkg->{path}, $pkg->{priority}) if $pkg->{active};
|
2006-09-25 17:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Symlink to the packages that have been "propagated" by packages
|
|
|
|
# installed by the user (i.e., package X declares that it want Y
|
|
|
|
# installed as well). We do these later because they have a lower
|
|
|
|
# priority in case of collisions.
|
2007-04-28 01:28:44 +02:00
|
|
|
my $priorityCounter = 1000; # don't care about collisions
|
2006-09-25 17:07:45 +02:00
|
|
|
while (scalar(keys %postponed) > 0) {
|
|
|
|
my @pkgDirs = keys %postponed;
|
|
|
|
%postponed = ();
|
|
|
|
foreach my $pkgDir (sort @pkgDirs) {
|
2007-04-28 01:28:44 +02:00
|
|
|
addPkg($pkgDir, $priorityCounter++);
|
2006-09-25 17:07:45 +02:00
|
|
|
}
|
2003-11-19 18:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-25 17:07:45 +02:00
|
|
|
print STDERR "created $symlinks symlinks in user environment\n";
|
|
|
|
|
|
|
|
|
2010-04-21 17:08:58 +02:00
|
|
|
symlink($ENV{"manifest"}, "$out/manifest.nix") or die "cannot create manifest";
|