2015-02-05 16:12:14 +01:00
|
|
|
{ nixpkgs, nixpkgs_i686, system
|
|
|
|
} :
|
2015-07-28 13:06:06 +02:00
|
|
|
{ name, profile ? ""
|
|
|
|
, pkgs ? null, targetPkgs ? pkgs: [], multiPkgs ? pkgs: []
|
2015-02-05 16:12:14 +01:00
|
|
|
, extraBuildCommands ? "", extraBuildCommandsMulti ? ""
|
|
|
|
}:
|
|
|
|
|
|
|
|
# HOWTO:
|
2015-07-28 13:06:06 +02:00
|
|
|
# All packages (most likely programs) returned from targetPkgs will only be
|
|
|
|
# installed once--matching the host's architecture (64bit on x86_64 and 32bit on
|
|
|
|
# x86).
|
2015-02-05 16:12:14 +01:00
|
|
|
#
|
2015-07-28 13:06:06 +02:00
|
|
|
# Packages (most likely libraries) returned from multiPkgs are installed
|
|
|
|
# once on x86 systems and twice on x86_64 systems.
|
|
|
|
# On x86 they are merged with packages from targetPkgs.
|
|
|
|
# On x86_64 they are added to targetPkgs and in addition their 32bit
|
|
|
|
# versions are also installed. The final directory structure looks as
|
|
|
|
# follows:
|
|
|
|
# /lib32 will include 32bit libraries from multiPkgs
|
2015-02-05 16:12:14 +01:00
|
|
|
# /lib64 will include 64bit libraries from multiPkgs and targetPkgs
|
2015-07-28 13:06:06 +02:00
|
|
|
# /lib will link to /lib32
|
2015-02-05 16:12:14 +01:00
|
|
|
|
|
|
|
let
|
2015-07-28 13:06:06 +02:00
|
|
|
isMultiBuild = pkgs == null && multiPkgs != null && system == "x86_64-linux";
|
2015-02-05 16:12:14 +01:00
|
|
|
isTargetBuild = !isMultiBuild;
|
|
|
|
|
2015-07-28 13:06:06 +02:00
|
|
|
# support deprecated "pkgs" option.
|
|
|
|
targetPkgs' =
|
|
|
|
if pkgs != null
|
|
|
|
then builtins.trace "buildFHSEnv: 'pkgs' option is deprecated, use 'targetPkgs'" (pkgs': pkgs)
|
|
|
|
else targetPkgs;
|
|
|
|
|
|
|
|
# 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);
|
2015-02-05 16:12:14 +01:00
|
|
|
|
2015-07-28 13:06:06 +02:00
|
|
|
# list of packages which are installed for both x86 and x86_64 on x86_64
|
2015-02-05 16:12:14 +01:00
|
|
|
# systems
|
|
|
|
multiPaths = if isMultiBuild
|
|
|
|
then multiPkgs nixpkgs_i686
|
|
|
|
else [];
|
|
|
|
|
|
|
|
# base packages of the chroot
|
2015-07-28 13:06:06 +02:00
|
|
|
# these match the host's architecture, gcc/glibc_multi are used for multilib
|
|
|
|
# builds.
|
2015-02-05 16:12:14 +01:00
|
|
|
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 15:14:27 +01:00
|
|
|
gnutar gzip bzip2 xz glibcLocales
|
2015-02-05 16:12:14 +01:00
|
|
|
];
|
|
|
|
|
2015-10-21 14:57:58 +02:00
|
|
|
etcProfile = nixpkgs.writeText "profile" ''
|
|
|
|
export PS1='${name}-chrootenv:\u@\h:\w\$ '
|
2015-11-10 21:59:57 +01:00
|
|
|
export LOCALE_ARCHIVE='/usr/lib/locale/locale-archive'
|
|
|
|
export LD_LIBRARY_PATH='/run/opengl-driver/lib:/run/opengl-driver-32/lib:/usr/lib:/usr/lib32'
|
2015-12-15 19:32:32 +01:00
|
|
|
export PATH='/var/setuid-wrappers:/usr/bin:/usr/sbin'
|
2015-10-21 14:57:58 +02:00
|
|
|
${profile}
|
|
|
|
'';
|
|
|
|
|
2015-04-22 14:46:49 +02:00
|
|
|
# Compose /etc for the chroot environment
|
|
|
|
etcPkg = nixpkgs.stdenv.mkDerivation {
|
|
|
|
name = "${name}-chrootenv-etc";
|
2015-02-05 16:12:14 +01:00
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/etc
|
2015-04-22 14:46:49 +02:00
|
|
|
cd $out/etc
|
|
|
|
|
|
|
|
# environment variables
|
2015-10-21 14:57:58 +02:00
|
|
|
ln -s ${etcProfile} profile
|
2015-04-22 14:46:49 +02: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
|
|
|
|
|
2015-12-15 19:31:12 +01:00
|
|
|
# symlink sudo and su stuff
|
|
|
|
ln -s /host-etc/login.defs login.defs
|
|
|
|
ln -s /host-etc/sudoers sudoers
|
|
|
|
ln -s /host-etc/sudoers.d sudoers.d
|
|
|
|
|
2015-04-22 14:46:49 +02:00
|
|
|
# symlink other core stuff
|
|
|
|
ln -s /host-etc/localtime localtime
|
|
|
|
ln -s /host-etc/machine-id machine-id
|
2015-10-03 14:42:10 +02:00
|
|
|
ln -s /host-etc/os-release os-release
|
2015-04-22 14:46:49 +02: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-10-21 21:34:12 +02:00
|
|
|
|
|
|
|
# symlink /etc/mtab -> /proc/mounts (compat for old userspace progs)
|
|
|
|
ln -s /proc/mounts mtab
|
2015-02-05 16:12:14 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-07-28 13:06:06 +02:00
|
|
|
# Composes a /usr-like directory structure
|
2015-02-05 16:12:14 +01:00
|
|
|
staticUsrProfileTarget = nixpkgs.buildEnv {
|
2015-04-22 14:46:49 +02:00
|
|
|
name = "${name}-usr-target";
|
|
|
|
paths = [ etcPkg ] ++ basePkgs ++ targetPaths;
|
2015-03-09 15:13:53 +01:00
|
|
|
ignoreCollisions = true;
|
2015-02-05 16:12:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
staticUsrProfileMulti = nixpkgs.buildEnv {
|
|
|
|
name = "system-profile-multi";
|
|
|
|
paths = multiPaths;
|
2015-03-09 15:13:53 +01:00
|
|
|
ignoreCollisions = true;
|
2015-02-05 16:12:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# setup library paths only for the targeted architecture
|
|
|
|
setupLibDirs_target = ''
|
|
|
|
mkdir -m0755 lib
|
|
|
|
|
|
|
|
# copy content of targetPaths
|
2015-07-28 13:06:06 +02:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/* lib/
|
2015-02-05 16:12:14 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
# setup /lib, /lib32 and /lib64
|
|
|
|
setupLibDirs_multi = ''
|
2015-07-28 13:06:06 +02:00
|
|
|
mkdir -m0755 lib32
|
2015-02-05 16:12:14 +01:00
|
|
|
mkdir -m0755 lib64
|
2015-11-10 21:59:57 +01:00
|
|
|
ln -s lib64 lib
|
2015-02-05 16:12:14 +01:00
|
|
|
|
|
|
|
# copy glibc stuff
|
2015-07-28 13:06:06 +02:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/32/* lib32/ && chmod u+w -R lib32/
|
2015-02-05 16:12:14 +01:00
|
|
|
|
|
|
|
# copy content of multiPaths (32bit libs)
|
2015-07-28 13:06:06 +02:00
|
|
|
[ -d ${staticUsrProfileMulti}/lib ] && cp -rsHf ${staticUsrProfileMulti}/lib/* lib32/ && chmod u+w -R lib32/
|
2015-02-05 16:12:14 +01:00
|
|
|
|
|
|
|
# copy content of targetPaths (64bit libs)
|
2015-07-28 13:06:06 +02:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/* lib64/ && chmod u+w -R lib64/
|
2015-02-05 16:12:14 +01:00
|
|
|
|
|
|
|
# most 64bit only libs put their stuff into /lib
|
2015-07-28 13:06:06 +02:00
|
|
|
# some pkgs (like gcc_multi) put 32bit libs into /lib and 64bit libs into /lib64
|
2015-02-05 16:12:14 +01:00
|
|
|
# by overwriting these we will hopefully catch all these cases
|
2015-07-28 13:06:06 +02:00
|
|
|
# in the end /lib32 should only contain 32bit and /lib64 only 64bit libs
|
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib64/* lib64/ && chmod u+w -R lib64/
|
2015-02-05 16:12:14 +01:00
|
|
|
|
2015-07-28 13:06:06 +02:00
|
|
|
# copy gcc libs
|
|
|
|
cp -rsHf ${chosenGcc.cc}/lib/* lib32/
|
|
|
|
cp -rsHf ${chosenGcc.cc}/lib64/* lib64/
|
2015-11-10 21:59:57 +01:00
|
|
|
|
|
|
|
# symlink 32-bit ld-linux.so
|
|
|
|
ln -s ${staticUsrProfileTarget}/lib/32/ld-linux.so.2 lib/
|
2015-02-05 16:12:14 +01:00
|
|
|
'';
|
|
|
|
|
2015-04-22 14:46:49 +02:00
|
|
|
setupLibDirs = if isTargetBuild then setupLibDirs_target
|
|
|
|
else setupLibDirs_multi;
|
2015-03-09 15:13:27 +01:00
|
|
|
|
2015-04-22 14:46:49 +02:00
|
|
|
# the target profile is the actual profile that will be used for the chroot
|
|
|
|
setupTargetProfile = ''
|
|
|
|
mkdir -m0755 usr
|
|
|
|
cd usr
|
|
|
|
${setupLibDirs}
|
2015-07-28 13:06:06 +02:00
|
|
|
for i in bin sbin share include; do
|
2015-11-23 17:49:20 +01:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
2015-11-23 19:39:49 +01:00
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
2015-11-23 17:49:20 +01:00
|
|
|
fi
|
2015-07-28 13:06:06 +02:00
|
|
|
done
|
2015-04-22 14:46:49 +02:00
|
|
|
cd ..
|
2015-11-23 19:39:49 +01:00
|
|
|
|
2015-07-28 13:06:06 +02:00
|
|
|
for i in var etc; do
|
2015-11-23 17:49:20 +01:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
2015-11-23 19:39:49 +01:00
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
2015-11-23 17:49:20 +01:00
|
|
|
fi
|
2015-07-28 13:06:06 +02:00
|
|
|
done
|
|
|
|
for i in usr/{bin,sbin,lib,lib32,lib64}; do
|
2015-11-23 17:49:20 +01:00
|
|
|
if [ -d "$i" ]; then
|
2015-07-28 13:06:06 +02:00
|
|
|
ln -s "$i"
|
|
|
|
fi
|
|
|
|
done
|
2015-03-09 15:13:27 +01:00
|
|
|
'';
|
|
|
|
|
2015-02-05 16:12:14 +01:00
|
|
|
in nixpkgs.stdenv.mkDerivation {
|
|
|
|
name = "${name}-fhs";
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out
|
|
|
|
cd $out
|
|
|
|
${setupTargetProfile}
|
|
|
|
cd $out
|
|
|
|
${extraBuildCommands}
|
|
|
|
cd $out
|
|
|
|
${if isMultiBuild then extraBuildCommandsMulti else ""}
|
|
|
|
'';
|
2015-02-05 18:39:01 +01:00
|
|
|
preferLocalBuild = true;
|
2015-02-05 16:12:14 +01:00
|
|
|
passthru = {
|
|
|
|
pname = name;
|
|
|
|
};
|
|
|
|
}
|