2006-11-28 16:46:12 +00:00
|
|
|
#! @perl@ -w
|
|
|
|
|
|
|
|
use strict;
|
2010-04-14 19:26:31 +00:00
|
|
|
use Cwd 'abs_path';
|
2006-11-28 16:46:12 +00:00
|
|
|
use IO::Handle;
|
|
|
|
use File::Path;
|
|
|
|
use File::Basename;
|
2014-11-23 19:42:14 +00:00
|
|
|
use File::Compare;
|
2015-08-24 22:37:54 +00:00
|
|
|
use JSON::PP;
|
2006-11-28 16:46:12 +00:00
|
|
|
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
2021-08-15 14:30:49 +00:00
|
|
|
$SIG{__WARN__} = sub { warn "warning: ", @_ };
|
|
|
|
$SIG{__DIE__} = sub { die "error: ", @_ };
|
|
|
|
|
2006-11-28 16:46:12 +00:00
|
|
|
my $out = $ENV{"out"};
|
2021-08-20 12:47:47 +00:00
|
|
|
my $extraPrefix = $ENV{"extraPrefix"};
|
2006-11-28 16:46:12 +00:00
|
|
|
|
|
|
|
my @pathsToLink = split ' ', $ENV{"pathsToLink"};
|
|
|
|
|
|
|
|
sub isInPathsToLink {
|
|
|
|
my $path = shift;
|
|
|
|
$path = "/" if $path eq "";
|
|
|
|
foreach my $elem (@pathsToLink) {
|
2010-08-09 16:08:02 +00:00
|
|
|
return 1 if
|
2015-08-24 22:37:54 +00:00
|
|
|
$elem eq "/" ||
|
2010-08-09 16:08:02 +00:00
|
|
|
(substr($path, 0, length($elem)) eq $elem
|
|
|
|
&& (($path eq $elem) || (substr($path, length($elem), 1) eq "/")));
|
2006-11-28 16:46:12 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:00:30 +00:00
|
|
|
# Returns whether a path in one of the linked packages may contain
|
|
|
|
# files in one of the elements of pathsToLink.
|
|
|
|
sub hasPathsToLink {
|
|
|
|
my $path = shift;
|
|
|
|
foreach my $elem (@pathsToLink) {
|
|
|
|
return 1 if
|
|
|
|
$path eq "" ||
|
|
|
|
(substr($elem, 0, length($path)) eq $path
|
|
|
|
&& (($path eq $elem) || (substr($elem, length($path), 1) eq "/")));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:41:20 +00:00
|
|
|
# Similar to `lib.isStorePath`
|
|
|
|
sub isStorePath {
|
|
|
|
my $path = shift;
|
|
|
|
my $storePath = "@storeDir@";
|
|
|
|
|
|
|
|
return substr($path, 0, 1) eq "/" && dirname($path) eq $storePath;
|
|
|
|
}
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
# For each activated package, determine what symlinks to create.
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
my %symlinks;
|
2015-08-24 22:37:54 +00:00
|
|
|
|
2016-07-09 20:47:15 +00:00
|
|
|
# Add all pathsToLink and all parent directories.
|
|
|
|
#
|
|
|
|
# For "/a/b/c" that will include
|
|
|
|
# [ "", "/a", "/a/b", "/a/b/c" ]
|
|
|
|
#
|
|
|
|
# That ensures the whole directory tree needed by pathsToLink is
|
|
|
|
# created as directories and not symlinks.
|
|
|
|
$symlinks{""} = ["", 0];
|
2015-08-25 09:11:51 +00:00
|
|
|
for my $p (@pathsToLink) {
|
2016-07-09 20:47:15 +00:00
|
|
|
my @parts = split '/', $p;
|
|
|
|
|
|
|
|
my $cur = "";
|
|
|
|
for my $x (@parts) {
|
|
|
|
$cur = $cur . "/$x";
|
|
|
|
$cur = "" if $cur eq "/";
|
|
|
|
$symlinks{$cur} = ["", 0];
|
|
|
|
}
|
2015-08-25 09:11:51 +00:00
|
|
|
}
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
sub findFiles;
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
sub findFilesInDir {
|
2014-11-23 19:42:14 +00:00
|
|
|
my ($relName, $target, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
opendir DIR, "$target" or die "cannot open `$target': $!";
|
|
|
|
my @names = readdir DIR or die;
|
|
|
|
closedir DIR;
|
2015-08-24 22:37:54 +00:00
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
foreach my $name (@names) {
|
|
|
|
next if $name eq "." || $name eq "..";
|
2014-11-23 19:42:14 +00:00
|
|
|
findFiles("$relName/$name", "$target/$name", $name, $ignoreCollisions, $checkCollisionContents, $priority);
|
2010-07-08 12:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 22:37:54 +00:00
|
|
|
|
2014-11-23 19:42:14 +00:00
|
|
|
sub checkCollision {
|
|
|
|
my ($path1, $path2) = @_;
|
|
|
|
|
2021-08-15 14:30:49 +00:00
|
|
|
if (! -e $path1 || ! -e $path2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-23 19:42:14 +00:00
|
|
|
my $stat1 = (stat($path1))[2];
|
|
|
|
my $stat2 = (stat($path2))[2];
|
|
|
|
|
|
|
|
if ($stat1 != $stat2) {
|
|
|
|
warn "different permissions in `$path1' and `$path2': "
|
|
|
|
. sprintf("%04o", $stat1 & 07777) . " <-> "
|
|
|
|
. sprintf("%04o", $stat2 & 07777);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compare($path1, $path2) == 0;
|
|
|
|
}
|
|
|
|
|
2021-08-15 14:30:49 +00:00
|
|
|
sub prependDangling {
|
|
|
|
my $path = shift;
|
|
|
|
return (-l $path && ! -e $path ? "dangling symlink " : "") . "`$path'";
|
|
|
|
}
|
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
sub findFiles {
|
2014-11-23 19:42:14 +00:00
|
|
|
my ($relName, $target, $baseName, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2010-07-08 12:52:17 +00:00
|
|
|
|
2019-02-20 09:41:20 +00:00
|
|
|
# The store path must not be a file
|
|
|
|
if (-f $target && isStorePath $target) {
|
|
|
|
die "The store path $target is a file and can't be merged into an environment using pkgs.buildEnv!";
|
2019-02-07 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
# Urgh, hacky...
|
|
|
|
return if
|
|
|
|
$relName eq "/propagated-build-inputs" ||
|
|
|
|
$relName eq "/nix-support" ||
|
2023-07-09 19:47:31 +00:00
|
|
|
$relName =~ /info\/dir$/ ||
|
2010-07-08 12:52:17 +00:00
|
|
|
( $relName =~ /^\/share\/mime\// && !( $relName =~ /^\/share\/mime\/packages/ ) ) ||
|
|
|
|
$baseName eq "perllocal.pod" ||
|
2019-09-16 16:00:30 +00:00
|
|
|
$baseName eq "log" ||
|
|
|
|
! (hasPathsToLink($relName) || isInPathsToLink($relName));
|
2010-07-08 12:52:17 +00:00
|
|
|
|
2015-08-24 22:37:54 +00:00
|
|
|
my ($oldTarget, $oldPriority) = @{$symlinks{$relName} // [undef, undef]};
|
2010-07-08 12:52:17 +00:00
|
|
|
|
2015-08-24 22:37:54 +00:00
|
|
|
# If target doesn't exist, create it. If it already exists as a
|
|
|
|
# symlink to a file (not a directory) in a lower-priority package,
|
|
|
|
# overwrite it.
|
|
|
|
if (!defined $oldTarget || ($priority < $oldPriority && ($oldTarget ne "" && ! -d $oldTarget))) {
|
2021-08-15 14:30:49 +00:00
|
|
|
# If target is a dangling symlink, emit a warning.
|
|
|
|
if (-l $target && ! -e $target) {
|
|
|
|
my $link = readlink $target;
|
|
|
|
warn "creating dangling symlink `$out$extraPrefix/$relName' -> `$target' -> `$link'\n";
|
|
|
|
}
|
2015-08-24 22:37:54 +00:00
|
|
|
$symlinks{$relName} = [$target, $priority];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:27:05 +00:00
|
|
|
# If target already exists and both targets resolves to the same path, skip
|
2021-08-15 14:30:49 +00:00
|
|
|
if (
|
|
|
|
defined $oldTarget && $oldTarget ne "" &&
|
|
|
|
defined abs_path($target) && defined abs_path($oldTarget) &&
|
|
|
|
abs_path($target) eq abs_path($oldTarget)
|
|
|
|
) {
|
2020-06-10 01:27:05 +00:00
|
|
|
# Prefer the target that is not a symlink, if any
|
|
|
|
if (-l $oldTarget && ! -l $target) {
|
|
|
|
$symlinks{$relName} = [$target, $priority];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:37:54 +00:00
|
|
|
# If target already exists as a symlink to a file (not a
|
|
|
|
# directory) in a higher-priority package, skip.
|
|
|
|
if (defined $oldTarget && $priority > $oldPriority && $oldTarget ne "" && ! -d $oldTarget) {
|
2010-07-08 12:52:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2021-08-15 14:30:49 +00:00
|
|
|
# If target is supposed to be a directory but it isn't, die with an error message
|
|
|
|
# instead of attempting to recurse into it, only to fail then.
|
|
|
|
# This happens e.g. when pathsToLink contains a non-directory path.
|
|
|
|
if ($oldTarget eq "" && ! -d $target) {
|
|
|
|
die "not a directory: `$target'\n";
|
|
|
|
}
|
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
unless (-d $target && ($oldTarget eq "" || -d $oldTarget)) {
|
2021-08-15 14:30:49 +00:00
|
|
|
# Prepend "dangling symlink" to paths if applicable.
|
|
|
|
my $targetRef = prependDangling($target);
|
|
|
|
my $oldTargetRef = prependDangling($oldTarget);
|
|
|
|
|
2014-11-23 22:11:06 +00:00
|
|
|
if ($ignoreCollisions) {
|
2021-08-15 14:30:49 +00:00
|
|
|
warn "collision between $targetRef and $oldTargetRef\n" if $ignoreCollisions == 1;
|
2010-07-08 12:52:17 +00:00
|
|
|
return;
|
2014-11-23 22:11:06 +00:00
|
|
|
} elsif ($checkCollisionContents && checkCollision($oldTarget, $target)) {
|
|
|
|
return;
|
2010-07-08 12:52:17 +00:00
|
|
|
} else {
|
2021-08-15 14:30:49 +00:00
|
|
|
die "collision between $targetRef and $oldTargetRef\n";
|
2006-11-28 16:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-08 12:52:17 +00:00
|
|
|
|
2014-11-23 19:42:14 +00:00
|
|
|
findFilesInDir($relName, $oldTarget, $ignoreCollisions, $checkCollisionContents, $oldPriority) unless $oldTarget eq "";
|
|
|
|
findFilesInDir($relName, $target, $ignoreCollisions, $checkCollisionContents, $priority);
|
2015-08-24 22:37:54 +00:00
|
|
|
|
|
|
|
$symlinks{$relName} = ["", $priority]; # denotes directory
|
2006-11-28 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my %done;
|
|
|
|
my %postponed;
|
|
|
|
|
2015-08-24 22:37:54 +00:00
|
|
|
sub addPkg {
|
2014-11-23 19:42:14 +00:00
|
|
|
my ($pkgDir, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2006-11-28 16:46:12 +00:00
|
|
|
|
|
|
|
return if (defined $done{$pkgDir});
|
|
|
|
$done{$pkgDir} = 1;
|
|
|
|
|
2014-11-23 19:42:14 +00:00
|
|
|
findFiles("", $pkgDir, "", $ignoreCollisions, $checkCollisionContents, $priority);
|
2006-11-28 16:46:12 +00:00
|
|
|
|
|
|
|
my $propagatedFN = "$pkgDir/nix-support/propagated-user-env-packages";
|
|
|
|
if (-e $propagatedFN) {
|
|
|
|
open PROP, "<$propagatedFN" or die;
|
|
|
|
my $propagated = <PROP>;
|
|
|
|
close PROP;
|
|
|
|
my @propagated = split ' ', $propagated;
|
|
|
|
foreach my $p (@propagated) {
|
|
|
|
$postponed{$p} = 1 unless defined $done{$p};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 13:11:33 +00:00
|
|
|
# Read packages list.
|
|
|
|
my $pkgs;
|
|
|
|
|
|
|
|
if (exists $ENV{"pkgsPath"}) {
|
|
|
|
open FILE, $ENV{"pkgsPath"};
|
|
|
|
$pkgs = <FILE>;
|
|
|
|
close FILE;
|
|
|
|
} else {
|
|
|
|
$pkgs = $ENV{"pkgs"}
|
|
|
|
}
|
2006-11-28 16:46:12 +00:00
|
|
|
|
2015-08-24 22:37:54 +00:00
|
|
|
# Symlink to the packages that have been installed explicitly by the
|
|
|
|
# user.
|
2015-10-15 13:11:33 +00:00
|
|
|
for my $pkg (@{decode_json $pkgs}) {
|
2015-08-24 22:37:54 +00:00
|
|
|
for my $path (@{$pkg->{paths}}) {
|
2014-11-23 19:42:14 +00:00
|
|
|
addPkg($path,
|
|
|
|
$ENV{"ignoreCollisions"} eq "1",
|
|
|
|
$ENV{"checkCollisionContents"} eq "1",
|
|
|
|
$pkg->{priority})
|
|
|
|
if -e $path;
|
2015-08-24 22:37:54 +00:00
|
|
|
}
|
2006-11-28 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Symlink to the packages that have been "propagated" by packages
|
2015-08-24 22:37:54 +00:00
|
|
|
# installed by the user (i.e., package X declares that it wants Y
|
2006-11-28 16:46:12 +00:00
|
|
|
# installed as well). We do these later because they have a lower
|
|
|
|
# priority in case of collisions.
|
2015-08-24 22:37:54 +00:00
|
|
|
my $priorityCounter = 1000; # don't care about collisions
|
2006-11-28 16:46:12 +00:00
|
|
|
while (scalar(keys %postponed) > 0) {
|
|
|
|
my @pkgDirs = keys %postponed;
|
|
|
|
%postponed = ();
|
|
|
|
foreach my $pkgDir (sort @pkgDirs) {
|
2016-01-27 11:44:56 +00:00
|
|
|
addPkg($pkgDir, 2, $ENV{"checkCollisionContents"} eq "1", $priorityCounter++);
|
2006-11-28 16:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 12:52:17 +00:00
|
|
|
|
|
|
|
# Create the symlinks.
|
|
|
|
my $nrLinks = 0;
|
|
|
|
foreach my $relName (sort keys %symlinks) {
|
2015-08-24 22:37:54 +00:00
|
|
|
my ($target, $priority) = @{$symlinks{$relName}};
|
2015-09-17 15:43:18 +00:00
|
|
|
my $abs = "$out" . "$extraPrefix" . "/$relName";
|
2010-07-08 12:52:17 +00:00
|
|
|
next unless isInPathsToLink $relName;
|
|
|
|
if ($target eq "") {
|
|
|
|
#print "creating directory $relName\n";
|
|
|
|
mkpath $abs or die "cannot create directory `$abs': $!";
|
|
|
|
} else {
|
|
|
|
#print "creating symlink $relName to $target\n";
|
|
|
|
symlink $target, $abs ||
|
|
|
|
die "error creating link `$abs': $!";
|
|
|
|
$nrLinks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print STDERR "created $nrLinks symlinks in user environment\n";
|
2006-11-28 16:46:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
my $manifest = $ENV{"manifest"};
|
2010-07-08 12:52:17 +00:00
|
|
|
if ($manifest) {
|
2006-11-28 16:46:12 +00:00
|
|
|
symlink($manifest, "$out/manifest") or die "cannot create manifest";
|
|
|
|
}
|