anchor.diff:      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
     setupLibDirs_target = ''
       mkdir -m0755 lib
   
       # copy content of targetPaths
  -    cp -rsf ${staticUsrProfileTarget}/lib/* lib/
  +    cp -rsHf ${staticUsrProfileTarget}/lib/* lib/
     '';
   
     # setup /lib, /lib32 and /lib64
     setupLibDirs_multi = ''
  -    mkdir -m0755 lib
  +    mkdir -m0755 lib32
       mkdir -m0755 lib64
  -    ln -s lib lib32
  +    ln -s lib32 lib
   
       # 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)
  -    [ -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)
  -    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
  -    # 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
  -    # in the end /lib should only contain 32bit and /lib64 only 64bit libs
  -    cp -rsf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/
  +    # in the end /lib32 should only contain 32bit and /lib64 only 64bit libs
  +    cp -rsHf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/
   
  -    # copy gcc libs (and may overwrite exitsting wrongly placed libs)
  -    cp -rsf ${chosenGcc.cc}/lib/*   lib/
  -    cp -rsf ${chosenGcc.cc}/lib64/* lib64/
  +    # copy gcc libs
  +    cp -rsHf ${chosenGcc.cc}/lib/*   lib32/
  +    cp -rsHf ${chosenGcc.cc}/lib64/* lib64/
     '';
   
     setupLibDirs = if isTargetBuild then setupLibDirs_target
                                     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
     setupTargetProfile = ''
  -    ${linkProfile staticUsrProfileTarget}
  -    ${setupLibDirs}
  -
       mkdir -m0755 usr
       cd usr
  -    ${linkProfile staticUsrProfileTarget}
       ${setupLibDirs}
  -    ${setupIncludeDir}
  +    for i in bin sbin share include; do
  +      cp -r "${staticUsrProfileTarget}/$i" $i
  +    done
       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 {
  
positive.code:     mkdir -p $out
      cd $out
      ${setupTargetProfile}
      ${setupMultiProfile}
      cd $out
      ${extraBuildCommands}
      cd $out
  
negative.code:       cd $out/etc
  
        # environment variables
        cat >> profile <<EOF
        export PS1='${name}-chrootenv:\u@\h:\w\$ '
        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:/lib64
        export PATH='/usr/bin:/usr/sbin'
        ${profile}
        EOF
  
  