Merge pull request #10320 from abbradar/fhs-simplify

chroot-env: simplify, clean directories structure
This commit is contained in:
Nikolay Amiantov 2015-10-19 11:18:49 +03:00
commit a995837606

View File

@ -1,61 +1,48 @@
{ nixpkgs, nixpkgs_i686, system { nixpkgs, nixpkgs_i686, system
} : } :
{ name, pkgs ? [], profile ? "" { name, profile ? ""
, targetPkgs ? null, multiPkgs ? null , pkgs ? null, targetPkgs ? pkgs: [], multiPkgs ? pkgs: []
, extraBuildCommands ? "", extraBuildCommandsMulti ? "" , extraBuildCommands ? "", extraBuildCommandsMulti ? ""
}: }:
assert pkgs != [] -> targetPkgs == null && multiPkgs == null;
assert targetPkgs != null -> multiPkgs != null;
assert multiPkgs != null -> targetPkgs != null;
assert targetPkgs != null -> pkgs == [];
# HOWTO: # HOWTO:
# If pkgs is defined buildFHSEnv will run in legacy mode. This means # All packages (most likely programs) returned from targetPkgs will only be
# it will build all pkgs contained in pkgs and basePkgs and then just merge # installed once--matching the host's architecture (64bit on x86_64 and 32bit on
# all of their contents together via buildEnv. # x86).
# #
# The new way is to define both targetPkgs and multiPkgs. These two are # Packages (most likely libraries) returned from multiPkgs are installed
# functions which get a pkgs environment supplied and should then return a list # once on x86 systems and twice on x86_64 systems.
# of packages based this environment. # On x86 they are merged with packages from targetPkgs.
# For example: targetPkgs = pkgs: [ pkgs.nmap ]; # On x86_64 they are added to targetPkgs and in addition their 32bit
# # versions are also installed. The final directory structure looks as
# All packages (most likely programs) placed in targetPkgs will only be # follows:
# installed once--matching the hosts architecture (64bit on x86_64 and 32bit on # /lib32 will include 32bit libraries from multiPkgs
# x86). These packages will populate the chroot directory tree.
#
# Packages (most likeley libraries) defined in multiPkgs will be installed once
# on x86 systems and twice on x86_64 systems.
# On x86 they will just be merge with the packages defined in targetPkgs.
# On x86_64 they will be added to targetPkgs and in addition their 32bit
# versions will also be installed. The final directory should look as follows:
# /lib will include 32bit libraries from multiPkgs
# /lib32 will link to /lib
# /lib64 will include 64bit libraries from multiPkgs and targetPkgs # /lib64 will include 64bit libraries from multiPkgs and targetPkgs
# /x86 will contain a complete 32bit environment composed by multiPkgs # /lib will link to /lib32
let let
is64Bit = system == "x86_64-linux"; isMultiBuild = pkgs == null && multiPkgs != null && system == "x86_64-linux";
# enable multi builds on x86_64 hosts if pakgs_target/multi are defined
isMultiBuild = is64Bit && targetPkgs != null;
isTargetBuild = !isMultiBuild; isTargetBuild = !isMultiBuild;
# list of packages (usually programs) which will only be installed for the # support deprecated "pkgs" option.
# hosts architecture targetPkgs' =
targetPaths = if targetPkgs == null if pkgs != null
then pkgs then builtins.trace "buildFHSEnv: 'pkgs' option is deprecated, use 'targetPkgs'" (pkgs': pkgs)
else targetPkgs nixpkgs ++ multiPkgs nixpkgs; else targetPkgs;
# list of pckages which should be build for both x86 and x86_64 on x86_64 # list of packages (usually programs) which are only be installed for the
# host's architecture
targetPaths = targetPkgs' nixpkgs ++ (if multiPkgs == null then [] else multiPkgs nixpkgs);
# list of packages which are installed for both x86 and x86_64 on x86_64
# systems # systems
multiPaths = if isMultiBuild multiPaths = if isMultiBuild
then multiPkgs nixpkgs_i686 then multiPkgs nixpkgs_i686
else []; else [];
# base packages of the chroot # base packages of the chroot
# these match the hosts architecture, gcc/glibc_multi will be choosen # these match the host's architecture, gcc/glibc_multi are used for multilib
# on multi builds # builds.
chosenGcc = if isMultiBuild then nixpkgs.gcc_multi else nixpkgs.gcc; chosenGcc = if isMultiBuild then nixpkgs.gcc_multi else nixpkgs.gcc;
basePkgs = with nixpkgs; basePkgs = with nixpkgs;
[ (if isMultiBuild then glibc_multi else glibc) [ (if isMultiBuild then glibc_multi else glibc)
@ -73,11 +60,11 @@ let
cd $out/etc cd $out/etc
# environment variables # environment variables
cat >> profile << "EOF" cat >> profile <<EOF
export PS1='${name}-chrootenv:\u@\h:\w\$ ' export PS1='${name}-chrootenv:\u@\h:\w\$ '
export LOCALE_ARCHIVE='/usr/lib${if is64Bit then "64" else ""}/locale/locale-archive' export LOCALE_ARCHIVE='/usr/lib${if isMultiBuild then "64" else ""}/locale/locale-archive'
export LD_LIBRARY_PATH=/run/opengl-driver/lib:/run/opengl-driver-32/lib:/lib:/lib32:/lib64 export LD_LIBRARY_PATH=/run/opengl-driver/lib:/run/opengl-driver-32/lib:/lib:/lib64
export PATH='/bin:/sbin' export PATH='/usr/bin:/usr/sbin'
${profile} ${profile}
EOF EOF
@ -112,7 +99,7 @@ let
''; '';
}; };
# Composes a /usr like directory structure # Composes a /usr-like directory structure
staticUsrProfileTarget = nixpkgs.buildEnv { staticUsrProfileTarget = nixpkgs.buildEnv {
name = "${name}-usr-target"; name = "${name}-usr-target";
paths = [ etcPkg ] ++ basePkgs ++ targetPaths; paths = [ etcPkg ] ++ basePkgs ++ targetPaths;
@ -125,83 +112,61 @@ let
ignoreCollisions = true; ignoreCollisions = true;
}; };
linkProfile = profile: ''
for i in ${profile}/{bin,sbin,share,var,etc}; do
if [ -x "$i" ]
then
ln -s "$i"
fi
done
'';
# this will happen on x86_64 host:
# /x86 -> links to the whole profile defined by multiPaths
# /lib, /lib32 -> links to 32bit binaries
# /lib64 -> links to 64bit binaries
# /usr/lib* -> same as above
setupMultiProfile = if isTargetBuild then "" else ''
mkdir -m0755 x86
cd x86
${linkProfile staticUsrProfileMulti}
cd ..
'';
# setup library paths only for the targeted architecture # setup library paths only for the targeted architecture
setupLibDirs_target = '' setupLibDirs_target = ''
mkdir -m0755 lib mkdir -m0755 lib
# copy content of targetPaths # copy content of targetPaths
cp -rsf ${staticUsrProfileTarget}/lib/* lib/ cp -rsHf ${staticUsrProfileTarget}/lib/* lib/
''; '';
# setup /lib, /lib32 and /lib64 # setup /lib, /lib32 and /lib64
setupLibDirs_multi = '' setupLibDirs_multi = ''
mkdir -m0755 lib mkdir -m0755 lib32
mkdir -m0755 lib64 mkdir -m0755 lib64
ln -s lib lib32 ln -s lib32 lib
# copy glibc stuff # copy glibc stuff
cp -rsf ${staticUsrProfileTarget}/lib/32/* lib/ && chmod u+w -R lib/ cp -rsHf ${staticUsrProfileTarget}/lib/32/* lib32/ && chmod u+w -R lib32/
# copy content of multiPaths (32bit libs) # copy content of multiPaths (32bit libs)
[ -d ${staticUsrProfileMulti}/lib ] && cp -rsf ${staticUsrProfileMulti}/lib/* lib/ && chmod u+w -R lib/ [ -d ${staticUsrProfileMulti}/lib ] && cp -rsHf ${staticUsrProfileMulti}/lib/* lib32/ && chmod u+w -R lib32/
# copy content of targetPaths (64bit libs) # copy content of targetPaths (64bit libs)
cp -rsf ${staticUsrProfileTarget}/lib/* lib64/ && chmod u+w -R lib64/ cp -rsHf ${staticUsrProfileTarget}/lib/* lib64/ && chmod u+w -R lib64/
# most 64bit only libs put their stuff into /lib # most 64bit only libs put their stuff into /lib
# some pkgs (like gcc_multi) put 32bit libs into and /lib 64bit libs into /lib64 # some pkgs (like gcc_multi) put 32bit libs into /lib and 64bit libs into /lib64
# by overwriting these we will hopefully catch all these cases # by overwriting these we will hopefully catch all these cases
# in the end /lib should only contain 32bit and /lib64 only 64bit libs # in the end /lib32 should only contain 32bit and /lib64 only 64bit libs
cp -rsf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/ cp -rsHf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/
# copy gcc libs (and may overwrite exitsting wrongly placed libs) # copy gcc libs
cp -rsf ${chosenGcc.cc}/lib/* lib/ cp -rsHf ${chosenGcc.cc}/lib/* lib32/
cp -rsf ${chosenGcc.cc}/lib64/* lib64/ cp -rsHf ${chosenGcc.cc}/lib64/* lib64/
''; '';
setupLibDirs = if isTargetBuild then setupLibDirs_target setupLibDirs = if isTargetBuild then setupLibDirs_target
else setupLibDirs_multi; else setupLibDirs_multi;
setupIncludeDir = ''
if [ -x "${staticUsrProfileTarget}/include" ]
then
ln -s "${staticUsrProfileTarget}/include"
fi
'';
# the target profile is the actual profile that will be used for the chroot # the target profile is the actual profile that will be used for the chroot
setupTargetProfile = '' setupTargetProfile = ''
${linkProfile staticUsrProfileTarget}
${setupLibDirs}
mkdir -m0755 usr mkdir -m0755 usr
cd usr cd usr
${linkProfile staticUsrProfileTarget}
${setupLibDirs} ${setupLibDirs}
${setupIncludeDir} for i in bin sbin share include; do
cp -r "${staticUsrProfileTarget}/$i" $i
done
cd .. cd ..
rm -rf usr/etc usr/var
for i in var etc; do
cp -r "${staticUsrProfileTarget}/$i" "$i"
done
for i in usr/{bin,sbin,lib,lib32,lib64}; do
if [ -x "$i" ]; then
ln -s "$i"
fi
done
''; '';
in nixpkgs.stdenv.mkDerivation { in nixpkgs.stdenv.mkDerivation {
@ -210,7 +175,6 @@ in nixpkgs.stdenv.mkDerivation {
mkdir -p $out mkdir -p $out
cd $out cd $out
${setupTargetProfile} ${setupTargetProfile}
${setupMultiProfile}
cd $out cd $out
${extraBuildCommands} ${extraBuildCommands}
cd $out cd $out