2015-02-05 15:12:14 +00:00
|
|
|
{ nixpkgs, nixpkgs_i686, system
|
|
|
|
} :
|
|
|
|
{ name, pkgs ? [], profile ? ""
|
|
|
|
, targetPkgs ? null, multiPkgs ? null
|
|
|
|
, extraBuildCommands ? "", extraBuildCommandsMulti ? ""
|
|
|
|
}:
|
|
|
|
|
|
|
|
assert pkgs != [] -> targetPkgs == null && multiPkgs == null;
|
|
|
|
assert targetPkgs != null -> multiPkgs != null;
|
|
|
|
assert multiPkgs != null -> targetPkgs != null;
|
|
|
|
assert targetPkgs != null -> pkgs == [];
|
|
|
|
|
|
|
|
|
|
|
|
# HOWTO:
|
|
|
|
# If pkgs is defined buildFHSEnv will run in legacy mode. This means
|
|
|
|
# it will build all pkgs contained in pkgs and basePkgs and then just merge
|
|
|
|
# all of their contents together via buildEnv.
|
|
|
|
#
|
|
|
|
# The new way is to define both targetPkgs and multiPkgs. These two are
|
|
|
|
# functions which get a pkgs environment supplied and should then return a list
|
|
|
|
# of packages based this environment.
|
|
|
|
# For example: targetPkgs = pkgs: [ pkgs.nmap ];
|
|
|
|
#
|
|
|
|
# All packages (most likely programs) placed in targetPkgs will only be
|
|
|
|
# installed once--matching the hosts architecture (64bit on x86_64 and 32bit on
|
|
|
|
# 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
|
|
|
|
# /x86 will contain a complete 32bit environment composed by multiPkgs
|
|
|
|
|
|
|
|
let
|
|
|
|
is64Bit = system == "x86_64-linux";
|
|
|
|
# enable multi builds on x86_64 hosts if pakgs_target/multi are defined
|
|
|
|
isMultiBuild = is64Bit && targetPkgs != null;
|
|
|
|
isTargetBuild = !isMultiBuild;
|
|
|
|
|
|
|
|
# list of packages (usually programs) which will only be installed for the
|
|
|
|
# hosts architecture
|
|
|
|
targetPaths = if targetPkgs == null
|
|
|
|
then pkgs
|
|
|
|
else targetPkgs nixpkgs ++ multiPkgs nixpkgs;
|
|
|
|
|
|
|
|
# list of pckages which should be build for both x86 and x86_64 on x86_64
|
|
|
|
# systems
|
|
|
|
multiPaths = if isMultiBuild
|
|
|
|
then multiPkgs nixpkgs_i686
|
|
|
|
else [];
|
|
|
|
|
|
|
|
# base packages of the chroot
|
|
|
|
# these match the hosts architecture, gcc/glibc_multi will be choosen
|
|
|
|
# on multi builds
|
|
|
|
chosenGcc = if isMultiBuild then nixpkgs.gcc_multi else nixpkgs.gcc;
|
|
|
|
basePkgs = with nixpkgs;
|
|
|
|
[ (if isMultiBuild then glibc_multi else glibc)
|
|
|
|
chosenGcc
|
|
|
|
bashInteractive coreutils less shadow su
|
|
|
|
gawk diffutils findutils gnused gnugrep
|
2015-03-09 14:14:27 +00:00
|
|
|
gnutar gzip bzip2 xz glibcLocales
|
2015-02-05 15:12:14 +00:00
|
|
|
];
|
|
|
|
|
2015-04-22 13:46:49 +01:00
|
|
|
# Compose /etc for the chroot environment
|
|
|
|
etcPkg = nixpkgs.stdenv.mkDerivation {
|
|
|
|
name = "${name}-chrootenv-etc";
|
2015-02-05 15:12:14 +00:00
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/etc
|
2015-04-22 13:46:49 +01:00
|
|
|
cd $out/etc
|
|
|
|
|
|
|
|
# environment variables
|
|
|
|
cat >> profile << "EOF"
|
2015-02-05 15:12:14 +00:00
|
|
|
export PS1='${name}-chrootenv:\u@\h:\w\$ '
|
2015-03-09 14:14:27 +00:00
|
|
|
export LOCALE_ARCHIVE='/usr/lib${if is64Bit then "64" else ""}/locale/locale-archive'
|
|
|
|
export LD_LIBRARY_PATH=/run/opengl-driver/lib:/run/opengl-driver-32/lib:/lib:/lib32:/lib64
|
|
|
|
export PATH='/bin:/sbin'
|
2015-02-05 15:12:14 +00:00
|
|
|
${profile}
|
|
|
|
EOF
|
2015-04-22 13:46:49 +01:00
|
|
|
|
|
|
|
# compatibility with NixOS
|
|
|
|
ln -s /host-etc/static static
|
|
|
|
|
|
|
|
# symlink some NSS stuff
|
|
|
|
ln -s /host-etc/passwd passwd
|
|
|
|
ln -s /host-etc/group group
|
|
|
|
ln -s /host-etc/shadow shadow
|
|
|
|
ln -s /host-etc/hosts hosts
|
|
|
|
ln -s /host-etc/resolv.conf resolv.conf
|
|
|
|
ln -s /host-etc/nsswitch.conf nsswitch.conf
|
|
|
|
|
|
|
|
# symlink other core stuff
|
|
|
|
ln -s /host-etc/localtime localtime
|
|
|
|
ln -s /host-etc/machine-id machine-id
|
2015-10-03 13:42:10 +01:00
|
|
|
ln -s /host-etc/os-release os-release
|
2015-04-22 13:46:49 +01:00
|
|
|
|
|
|
|
# symlink PAM stuff
|
|
|
|
ln -s /host-etc/pam.d pam.d
|
|
|
|
|
|
|
|
# symlink fonts stuff
|
|
|
|
ln -s /host-etc/fonts fonts
|
|
|
|
|
|
|
|
# symlink ALSA stuff
|
|
|
|
ln -s /host-etc/asound.conf asound.conf
|
|
|
|
|
|
|
|
# symlink SSL certs
|
|
|
|
mkdir -p ssl
|
|
|
|
ln -s /host-etc/ssl/certs ssl/certs
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
# Composes a /usr like directory structure
|
|
|
|
staticUsrProfileTarget = nixpkgs.buildEnv {
|
2015-04-22 13:46:49 +01:00
|
|
|
name = "${name}-usr-target";
|
|
|
|
paths = [ etcPkg ] ++ basePkgs ++ targetPaths;
|
2015-03-09 14:13:53 +00:00
|
|
|
ignoreCollisions = true;
|
2015-02-05 15:12:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
staticUsrProfileMulti = nixpkgs.buildEnv {
|
|
|
|
name = "system-profile-multi";
|
|
|
|
paths = multiPaths;
|
2015-03-09 14:13:53 +00:00
|
|
|
ignoreCollisions = true;
|
2015-02-05 15:12:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
linkProfile = profile: ''
|
2015-04-22 13:46:49 +01:00
|
|
|
for i in ${profile}/{bin,sbin,share,var,etc}; do
|
2015-02-05 15:12:14 +00:00
|
|
|
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
|
2015-04-22 13:47:20 +01:00
|
|
|
cp -rsf ${staticUsrProfileTarget}/lib/* lib/
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# setup /lib, /lib32 and /lib64
|
|
|
|
setupLibDirs_multi = ''
|
|
|
|
mkdir -m0755 lib
|
|
|
|
mkdir -m0755 lib64
|
|
|
|
ln -s lib lib32
|
|
|
|
|
|
|
|
# copy glibc stuff
|
2015-04-22 13:47:20 +01:00
|
|
|
cp -rsf ${staticUsrProfileTarget}/lib/32/* lib/ && chmod u+w -R lib/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy content of multiPaths (32bit libs)
|
2015-03-09 14:13:53 +00:00
|
|
|
[ -d ${staticUsrProfileMulti}/lib ] && cp -rsf ${staticUsrProfileMulti}/lib/* lib/ && chmod u+w -R lib/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy content of targetPaths (64bit libs)
|
2015-03-09 14:13:53 +00:00
|
|
|
cp -rsf ${staticUsrProfileTarget}/lib/* lib64/ && chmod u+w -R lib64/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# most 64bit only libs put their stuff into /lib
|
|
|
|
# some pkgs (like gcc_multi) put 32bit libs into and /lib 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
|
2015-03-09 14:13:53 +00:00
|
|
|
cp -rsf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy gcc libs (and may overwrite exitsting wrongly placed libs)
|
|
|
|
cp -rsf ${chosenGcc.cc}/lib/* lib/
|
|
|
|
cp -rsf ${chosenGcc.cc}/lib64/* lib64/
|
|
|
|
'';
|
|
|
|
|
2015-04-22 13:46:49 +01:00
|
|
|
setupLibDirs = if isTargetBuild then setupLibDirs_target
|
|
|
|
else setupLibDirs_multi;
|
2015-03-09 14:13:27 +00:00
|
|
|
|
2015-10-02 18:09:52 +01:00
|
|
|
setupIncludeDir = ''
|
|
|
|
if [ -x "${staticUsrProfileTarget}/include" ]
|
|
|
|
then
|
|
|
|
ln -s "${staticUsrProfileTarget}/include"
|
|
|
|
fi
|
|
|
|
'';
|
2015-03-09 14:13:27 +00:00
|
|
|
|
2015-04-22 13:46:49 +01:00
|
|
|
# the target profile is the actual profile that will be used for the chroot
|
|
|
|
setupTargetProfile = ''
|
|
|
|
${linkProfile staticUsrProfileTarget}
|
|
|
|
${setupLibDirs}
|
2015-03-09 14:13:27 +00:00
|
|
|
|
2015-04-22 13:46:49 +01:00
|
|
|
mkdir -m0755 usr
|
|
|
|
cd usr
|
|
|
|
${linkProfile staticUsrProfileTarget}
|
|
|
|
${setupLibDirs}
|
2015-10-02 18:09:52 +01:00
|
|
|
${setupIncludeDir}
|
2015-04-22 13:46:49 +01:00
|
|
|
cd ..
|
|
|
|
rm -rf usr/etc usr/var
|
2015-03-09 14:13:27 +00:00
|
|
|
'';
|
|
|
|
|
2015-02-05 15:12:14 +00:00
|
|
|
in nixpkgs.stdenv.mkDerivation {
|
|
|
|
name = "${name}-fhs";
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out
|
|
|
|
cd $out
|
|
|
|
${setupTargetProfile}
|
|
|
|
${setupMultiProfile}
|
|
|
|
cd $out
|
|
|
|
${extraBuildCommands}
|
|
|
|
cd $out
|
|
|
|
${if isMultiBuild then extraBuildCommandsMulti else ""}
|
|
|
|
'';
|
2015-02-05 17:39:01 +00:00
|
|
|
preferLocalBuild = true;
|
2015-02-05 15:12:14 +00:00
|
|
|
passthru = {
|
|
|
|
pname = name;
|
|
|
|
};
|
|
|
|
}
|