2023-04-09 19:50:02 +02:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
|
|
|
, runCommandLocal
|
|
|
|
, buildEnv
|
|
|
|
, writeText
|
|
|
|
, writeShellScriptBin
|
|
|
|
, pkgs
|
|
|
|
, pkgsi686Linux
|
|
|
|
}:
|
2020-07-31 14:58:03 +02:00
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
{ name ? null
|
|
|
|
, profile ? ""
|
|
|
|
, targetPkgs ? pkgs: []
|
|
|
|
, multiPkgs ? pkgs: []
|
2023-07-01 06:42:31 +02:00
|
|
|
, multiArch ? false # Whether to include 32bit packages
|
2023-04-09 19:50:02 +02:00
|
|
|
, extraBuildCommands ? ""
|
|
|
|
, extraBuildCommandsMulti ? ""
|
2020-07-31 14:58:03 +02:00
|
|
|
, extraOutputsToInstall ? []
|
2023-04-09 19:50:02 +02:00
|
|
|
} @ args:
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# HOWTO:
|
|
|
|
# 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).
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# /lib64 will include 64bit libraries from multiPkgs and targetPkgs
|
|
|
|
# /lib will link to /lib32
|
|
|
|
|
|
|
|
let
|
2022-08-17 21:36:45 +02:00
|
|
|
inherit (stdenv) is64bit;
|
2021-08-07 05:20:36 +02:00
|
|
|
|
2023-07-01 06:36:05 +02:00
|
|
|
# "use of glibc_multi is only supported on x86_64-linux"
|
|
|
|
isMultiBuild = multiArch && stdenv.system == "x86_64-linux";
|
2020-07-31 14:58:03 +02:00
|
|
|
isTargetBuild = !isMultiBuild;
|
|
|
|
|
|
|
|
# list of packages (usually programs) which are only be installed for the
|
|
|
|
# host's architecture
|
|
|
|
targetPaths = targetPkgs pkgs ++ (if multiPkgs == null then [] else multiPkgs pkgs);
|
|
|
|
|
|
|
|
# list of packages which are installed for both x86 and x86_64 on x86_64
|
|
|
|
# systems
|
|
|
|
multiPaths = multiPkgs pkgsi686Linux;
|
|
|
|
|
|
|
|
# base packages of the chroot
|
|
|
|
# these match the host's architecture, glibc_multi is used for multilib
|
|
|
|
# builds. glibcLocales must be before glibc or glibc_multi as otherwiese
|
|
|
|
# the wrong LOCALE_ARCHIVE will be used where only C.UTF-8 is available.
|
2023-09-24 10:41:26 +02:00
|
|
|
baseTargetPaths = with pkgs; [
|
2023-07-01 06:23:45 +02:00
|
|
|
glibcLocales
|
|
|
|
(if isMultiBuild then glibc_multi else glibc)
|
|
|
|
(toString gcc.cc.lib)
|
|
|
|
bashInteractiveFHS
|
|
|
|
coreutils
|
|
|
|
less
|
|
|
|
shadow
|
|
|
|
su
|
|
|
|
gawk
|
|
|
|
diffutils
|
|
|
|
findutils
|
|
|
|
gnused
|
|
|
|
gnugrep
|
|
|
|
gnutar
|
|
|
|
gzip
|
|
|
|
bzip2
|
|
|
|
xz
|
|
|
|
];
|
2023-09-24 10:41:26 +02:00
|
|
|
baseMultiPaths = with pkgsi686Linux; [
|
2023-07-01 06:23:45 +02:00
|
|
|
(toString gcc.cc.lib)
|
|
|
|
];
|
2020-07-31 14:58:03 +02:00
|
|
|
|
2021-02-23 15:47:40 +01:00
|
|
|
ldconfig = writeShellScriptBin "ldconfig" ''
|
2022-08-02 17:05:33 +02:00
|
|
|
# due to a glibc bug, 64-bit ldconfig complains about patchelf'd 32-bit libraries, so we're using 32-bit ldconfig
|
2023-07-01 06:23:45 +02:00
|
|
|
exec ${if stdenv.system == "x86_64-linux" then pkgsi686Linux.glibc.bin else pkgs.glibc.bin}/bin/ldconfig -f /etc/ld.so.conf -C /etc/ld.so.cache "$@"
|
2020-10-30 19:22:04 +01:00
|
|
|
'';
|
2023-07-01 06:23:45 +02:00
|
|
|
|
2020-07-31 14:58:03 +02:00
|
|
|
etcProfile = writeText "profile" ''
|
|
|
|
export PS1='${name}-chrootenv:\u@\h:\w\$ '
|
|
|
|
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''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
|
|
|
|
export PATH="/run/wrappers/bin:/usr/bin:/usr/sbin:$PATH"
|
|
|
|
export TZDIR='/etc/zoneinfo'
|
|
|
|
|
2022-03-21 19:45:13 +01:00
|
|
|
# XDG_DATA_DIRS is used by pressure-vessel (steam proton) and vulkan loaders to find the corresponding icd
|
|
|
|
export XDG_DATA_DIRS=$XDG_DATA_DIRS''${XDG_DATA_DIRS:+:}/run/opengl-driver/share:/run/opengl-driver-32/share
|
|
|
|
|
2023-05-07 03:03:42 +02:00
|
|
|
# Following XDG spec [1], XDG_DATA_DIRS should default to "/usr/local/share:/usr/share".
|
|
|
|
# In nix, it is commonly set without containing these values, so we add them as fallback.
|
|
|
|
#
|
|
|
|
# [1] <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
|
|
|
|
case ":$XDG_DATA_DIRS:" in
|
|
|
|
*:/usr/local/share:*) ;;
|
|
|
|
*) export XDG_DATA_DIRS="$XDG_DATA_DIRS''${XDG_DATA_DIRS:+:}/usr/local/share" ;;
|
|
|
|
esac
|
|
|
|
case ":$XDG_DATA_DIRS:" in
|
|
|
|
*:/usr/share:*) ;;
|
|
|
|
*) export XDG_DATA_DIRS="$XDG_DATA_DIRS''${XDG_DATA_DIRS:+:}/usr/share" ;;
|
|
|
|
esac
|
|
|
|
|
2020-07-31 14:58:03 +02:00
|
|
|
# Force compilers and other tools to look in default search paths
|
|
|
|
unset NIX_ENFORCE_PURITY
|
2023-08-21 08:37:01 +02:00
|
|
|
export NIX_BINTOOLS_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}=1
|
2020-07-31 14:58:03 +02:00
|
|
|
export NIX_CC_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}=1
|
|
|
|
export NIX_CFLAGS_COMPILE='-idirafter /usr/include'
|
|
|
|
export NIX_CFLAGS_LINK='-L/usr/lib -L/usr/lib32'
|
|
|
|
export NIX_LDFLAGS='-L/usr/lib -L/usr/lib32'
|
|
|
|
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
|
|
|
|
export ACLOCAL_PATH=/usr/share/aclocal
|
|
|
|
|
|
|
|
${profile}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Compose /etc for the chroot environment
|
2023-04-09 19:50:23 +02:00
|
|
|
etcPkg = runCommandLocal "${name}-chrootenv-etc" { } ''
|
2023-04-09 19:50:02 +02:00
|
|
|
mkdir -p $out/etc
|
2023-07-01 06:23:45 +02:00
|
|
|
pushd $out/etc
|
2020-07-31 14:58:03 +02:00
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
# environment variables
|
|
|
|
ln -s ${etcProfile} profile
|
2020-07-31 14:58:03 +02:00
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
# symlink /etc/mtab -> /proc/mounts (compat for old userspace progs)
|
|
|
|
ln -s /proc/mounts mtab
|
|
|
|
'';
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# Composes a /usr-like directory structure
|
|
|
|
staticUsrProfileTarget = buildEnv {
|
|
|
|
name = "${name}-usr-target";
|
2020-10-30 19:22:04 +01:00
|
|
|
# ldconfig wrapper must come first so it overrides the original ldconfig
|
2023-09-24 10:41:26 +02:00
|
|
|
paths = [ etcPkg ldconfig ] ++ baseTargetPaths ++ targetPaths;
|
2020-07-31 14:58:03 +02:00
|
|
|
extraOutputsToInstall = [ "out" "lib" "bin" ] ++ extraOutputsToInstall;
|
|
|
|
ignoreCollisions = true;
|
2022-02-25 00:08:47 +01:00
|
|
|
postBuild = ''
|
|
|
|
if [[ -d $out/share/gsettings-schemas/ ]]; then
|
|
|
|
# Recreate the standard schemas directory if its a symlink to make it writable
|
|
|
|
if [[ -L $out/share/glib-2.0 ]]; then
|
2022-03-16 13:47:27 +01:00
|
|
|
target=$(readlink $out/share/glib-2.0)
|
|
|
|
rm $out/share/glib-2.0
|
|
|
|
mkdir $out/share/glib-2.0
|
|
|
|
ln -fs $target/* $out/share/glib-2.0
|
2022-02-25 00:08:47 +01:00
|
|
|
fi
|
|
|
|
|
2022-03-16 13:47:27 +01:00
|
|
|
if [[ -L $out/share/glib-2.0/schemas ]]; then
|
|
|
|
target=$(readlink $out/share/glib-2.0/schemas)
|
|
|
|
rm $out/share/glib-2.0/schemas
|
|
|
|
mkdir $out/share/glib-2.0/schemas
|
|
|
|
ln -fs $target/* $out/share/glib-2.0/schemas
|
2022-02-25 00:08:47 +01:00
|
|
|
fi
|
|
|
|
|
2022-03-16 13:47:27 +01:00
|
|
|
mkdir -p $out/share/glib-2.0/schemas
|
|
|
|
|
2022-02-25 00:08:47 +01:00
|
|
|
for d in $out/share/gsettings-schemas/*; do
|
|
|
|
# Force symlink, in case there are duplicates
|
|
|
|
ln -fs $d/glib-2.0/schemas/*.xml $out/share/glib-2.0/schemas
|
|
|
|
ln -fs $d/glib-2.0/schemas/*.gschema.override $out/share/glib-2.0/schemas
|
|
|
|
done
|
|
|
|
|
|
|
|
# and compile them
|
|
|
|
${pkgs.glib.dev}/bin/glib-compile-schemas $out/share/glib-2.0/schemas
|
|
|
|
fi
|
|
|
|
'';
|
2020-07-31 14:58:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
staticUsrProfileMulti = buildEnv {
|
|
|
|
name = "${name}-usr-multi";
|
2023-09-24 10:41:26 +02:00
|
|
|
paths = baseMultiPaths ++ multiPaths;
|
2020-07-31 14:58:03 +02:00
|
|
|
extraOutputsToInstall = [ "out" "lib" ] ++ extraOutputsToInstall;
|
|
|
|
ignoreCollisions = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
# setup library paths only for the targeted architecture
|
2020-08-17 09:49:34 +02:00
|
|
|
setupLibDirsTarget = ''
|
2020-07-31 14:58:03 +02:00
|
|
|
# link content of targetPaths
|
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib lib
|
2022-08-17 21:36:45 +02:00
|
|
|
ln -s lib lib${if is64bit then "64" else "32"}
|
2020-07-31 14:58:03 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
# setup /lib, /lib32 and /lib64
|
2020-08-17 09:49:34 +02:00
|
|
|
setupLibDirsMulti = ''
|
2020-07-31 14:58:03 +02:00
|
|
|
mkdir -m0755 lib32
|
|
|
|
mkdir -m0755 lib64
|
|
|
|
ln -s lib64 lib
|
|
|
|
|
|
|
|
# copy glibc stuff
|
2023-07-01 06:23:45 +02:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/32/* lib32/
|
|
|
|
chmod u+w -R lib32/
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# copy content of multiPaths (32bit libs)
|
2023-07-01 06:23:45 +02:00
|
|
|
if [ -d ${staticUsrProfileMulti}/lib ]; then
|
|
|
|
cp -rsHf ${staticUsrProfileMulti}/lib/* lib32/
|
|
|
|
chmod u+w -R lib32/
|
|
|
|
fi
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# copy content of targetPaths (64bit libs)
|
2023-07-01 06:23:45 +02:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/* lib64/
|
|
|
|
chmod u+w -R lib64/
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# symlink 32-bit ld-linux.so
|
|
|
|
ln -Ls ${staticUsrProfileTarget}/lib/32/ld-linux.so.2 lib/
|
|
|
|
'';
|
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
setupLibDirs = if isTargetBuild
|
|
|
|
then setupLibDirsTarget
|
|
|
|
else setupLibDirsMulti;
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
# the target profile is the actual profile that will be used for the chroot
|
|
|
|
setupTargetProfile = ''
|
|
|
|
mkdir -m0755 usr
|
2023-07-01 06:23:45 +02:00
|
|
|
pushd usr
|
|
|
|
|
2020-07-31 14:58:03 +02:00
|
|
|
${setupLibDirs}
|
2023-07-01 06:23:45 +02:00
|
|
|
|
|
|
|
'' + lib.optionalString isMultiBuild ''
|
2021-02-04 01:25:55 +01:00
|
|
|
if [ -d "${staticUsrProfileMulti}/share" ]; then
|
|
|
|
cp -rLf ${staticUsrProfileMulti}/share share
|
|
|
|
fi
|
2023-07-01 06:23:45 +02:00
|
|
|
'' + ''
|
2021-02-04 01:25:55 +01:00
|
|
|
if [ -d "${staticUsrProfileTarget}/share" ]; then
|
|
|
|
if [ -d share ]; then
|
|
|
|
chmod -R 755 share
|
|
|
|
cp -rLTf ${staticUsrProfileTarget}/share share
|
|
|
|
else
|
2022-03-19 06:44:25 +01:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/share share
|
2021-02-04 01:25:55 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
for i in bin sbin include; do
|
2020-07-31 14:58:03 +02:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
cd ..
|
|
|
|
|
2021-06-25 18:08:56 +02:00
|
|
|
for i in var etc opt; do
|
2020-07-31 14:58:03 +02:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
for i in usr/{bin,sbin,lib,lib32,lib64}; do
|
|
|
|
if [ -d "$i" ]; then
|
|
|
|
ln -s "$i"
|
|
|
|
fi
|
|
|
|
done
|
2023-07-01 06:23:45 +02:00
|
|
|
|
|
|
|
popd
|
2020-07-31 14:58:03 +02:00
|
|
|
'';
|
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
in runCommandLocal "${name}-fhs" {
|
2023-03-07 20:35:30 +01:00
|
|
|
passthru = {
|
2023-09-24 10:41:26 +02:00
|
|
|
inherit args baseTargetPaths targetPaths baseMultiPaths multiPaths ldconfig;
|
2023-03-07 20:35:30 +01:00
|
|
|
};
|
2023-04-09 19:50:02 +02:00
|
|
|
} ''
|
|
|
|
mkdir -p $out
|
2023-07-01 06:23:45 +02:00
|
|
|
pushd $out
|
|
|
|
|
2023-04-09 19:50:02 +02:00
|
|
|
${setupTargetProfile}
|
|
|
|
${extraBuildCommands}
|
|
|
|
${lib.optionalString isMultiBuild extraBuildCommandsMulti}
|
|
|
|
''
|