2023-04-12 12:48:02 +00:00
|
|
|
{ lib
|
|
|
|
, stdenv
|
|
|
|
, runCommandLocal
|
|
|
|
, buildEnv
|
|
|
|
, writeText
|
|
|
|
, writeShellScriptBin
|
|
|
|
, pkgs
|
|
|
|
, pkgsi686Linux
|
|
|
|
}:
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
{ profile ? ""
|
2023-04-12 12:48:02 +00:00
|
|
|
, targetPkgs ? pkgs: []
|
|
|
|
, multiPkgs ? pkgs: []
|
2023-07-15 17:15:38 +00:00
|
|
|
, multiArch ? false # Whether to include 32bit packages
|
2023-04-12 12:48:02 +00:00
|
|
|
, extraBuildCommands ? ""
|
|
|
|
, extraBuildCommandsMulti ? ""
|
2020-08-20 17:08:02 +00:00
|
|
|
, extraOutputsToInstall ? []
|
2024-05-15 15:35:15 +00:00
|
|
|
, ... # for name, or pname+version
|
2023-04-12 12:48:02 +00:00
|
|
|
} @ args:
|
2020-08-20 17:08:02 +00: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-21 13:32:41 +00:00
|
|
|
inherit (stdenv) is64bit;
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
name = if (args ? pname && args ? version)
|
|
|
|
then "${args.pname}-${args.version}"
|
|
|
|
else args.name;
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
# "use of glibc_multi is only supported on x86_64-linux"
|
|
|
|
isMultiBuild = multiArch && stdenv.system == "x86_64-linux";
|
2020-08-20 17:08:02 +00:00
|
|
|
isTargetBuild = !isMultiBuild;
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# list of packages (usually programs) which match the host's architecture
|
|
|
|
# (which includes stuff from multiPkgs)
|
2020-08-20 17:08:02 +00:00
|
|
|
targetPaths = targetPkgs pkgs ++ (if multiPkgs == null then [] else multiPkgs pkgs);
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
# list of packages which are for x86 (only multiPkgs, only for x86_64 hosts)
|
2020-08-20 17:08:02 +00:00
|
|
|
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-10-09 19:29:22 +00:00
|
|
|
baseTargetPaths = with pkgs; [
|
2023-07-15 17:15:38 +00: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-10-09 19:29:22 +00:00
|
|
|
baseMultiPaths = with pkgsi686Linux; [
|
2023-07-15 17:15:38 +00:00
|
|
|
(toString gcc.cc.lib)
|
|
|
|
];
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2021-02-24 18:30:23 +00:00
|
|
|
ldconfig = writeShellScriptBin "ldconfig" ''
|
2024-04-21 15:54:59 +00:00
|
|
|
# due to a glibc bug, 64-bit ldconfig complains about patchelf'd 32-bit libraries, so we use 32-bit ldconfig when we have them
|
|
|
|
exec ${if isMultiBuild then pkgsi686Linux.glibc.bin else pkgs.glibc.bin}/bin/ldconfig -f /etc/ld.so.conf -C /etc/ld.so.cache "$@"
|
2021-02-24 18:30:23 +00:00
|
|
|
'';
|
2023-07-15 17:15:38 +00:00
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
etcProfile = writeText "profile" ''
|
|
|
|
export PS1='${name}-chrootenv:\u@\h:\w\$ '
|
|
|
|
export LOCALE_ARCHIVE='/usr/lib/locale/locale-archive'
|
2023-11-16 04:20:00 +00:00
|
|
|
export LD_LIBRARY_PATH="/run/opengl-driver/lib:/run/opengl-driver-32/lib''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
|
2020-08-20 17:08:02 +00:00
|
|
|
export PATH="/run/wrappers/bin:/usr/bin:/usr/sbin:$PATH"
|
|
|
|
export TZDIR='/etc/zoneinfo'
|
|
|
|
|
2022-03-30 09:31:56 +00: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-24 13:37:59 +00: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-08-20 17:08:02 +00:00
|
|
|
# Force compilers and other tools to look in default search paths
|
|
|
|
unset NIX_ENFORCE_PURITY
|
2023-10-09 19:29:22 +00:00
|
|
|
export NIX_BINTOOLS_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}=1
|
2020-08-20 17:08:02 +00: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
|
|
|
|
|
2024-06-24 18:47:55 +00:00
|
|
|
# GStreamer searches for plugins relative to its real binary's location
|
|
|
|
# https://gitlab.freedesktop.org/gstreamer/gstreamer/-/commit/bd97973ce0f2c5495bcda5cccd4f7ef7dcb7febc
|
|
|
|
export GST_PLUGIN_SYSTEM_PATH_1_0=/usr/lib/gstreamer-1.0:/usr/lib32/gstreamer-1.0
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
${profile}
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Compose /etc for the chroot environment
|
2023-04-12 12:48:02 +00:00
|
|
|
etcPkg = runCommandLocal "${name}-chrootenv-etc" { } ''
|
|
|
|
mkdir -p $out/etc
|
2023-07-15 17:15:38 +00:00
|
|
|
pushd $out/etc
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
# environment variables
|
|
|
|
ln -s ${etcProfile} profile
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
# symlink /etc/mtab -> /proc/mounts (compat for old userspace progs)
|
|
|
|
ln -s /proc/mounts mtab
|
|
|
|
'';
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
# Composes a /usr-like directory structure
|
|
|
|
staticUsrProfileTarget = buildEnv {
|
|
|
|
name = "${name}-usr-target";
|
2021-02-24 18:30:23 +00:00
|
|
|
# ldconfig wrapper must come first so it overrides the original ldconfig
|
2023-10-09 19:29:22 +00:00
|
|
|
paths = [ etcPkg ldconfig ] ++ baseTargetPaths ++ targetPaths;
|
2020-08-20 17:08:02 +00:00
|
|
|
extraOutputsToInstall = [ "out" "lib" "bin" ] ++ extraOutputsToInstall;
|
|
|
|
ignoreCollisions = true;
|
2022-03-30 09:31:56 +00: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
|
|
|
|
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
|
|
|
|
fi
|
|
|
|
|
|
|
|
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
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p $out/share/glib-2.0/schemas
|
|
|
|
|
|
|
|
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-08-20 17:08:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
staticUsrProfileMulti = buildEnv {
|
|
|
|
name = "${name}-usr-multi";
|
2023-10-09 19:29:22 +00:00
|
|
|
paths = baseMultiPaths ++ multiPaths;
|
2020-08-20 17:08:02 +00:00
|
|
|
extraOutputsToInstall = [ "out" "lib" ] ++ extraOutputsToInstall;
|
|
|
|
ignoreCollisions = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
# setup library paths only for the targeted architecture
|
|
|
|
setupLibDirsTarget = ''
|
|
|
|
# link content of targetPaths
|
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib lib
|
2022-08-21 13:32:41 +00:00
|
|
|
ln -s lib lib${if is64bit then "64" else "32"}
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# setup /lib, /lib32 and /lib64
|
|
|
|
setupLibDirsMulti = ''
|
|
|
|
mkdir -m0755 lib32
|
|
|
|
mkdir -m0755 lib64
|
|
|
|
ln -s lib64 lib
|
|
|
|
|
|
|
|
# copy glibc stuff
|
2023-07-15 17:15:38 +00:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/32/* lib32/
|
|
|
|
chmod u+w -R lib32/
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
# copy content of multiPaths (32bit libs)
|
2023-07-15 17:15:38 +00:00
|
|
|
if [ -d ${staticUsrProfileMulti}/lib ]; then
|
|
|
|
cp -rsHf ${staticUsrProfileMulti}/lib/* lib32/
|
|
|
|
chmod u+w -R lib32/
|
|
|
|
fi
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
# copy content of targetPaths (64bit libs)
|
2023-07-15 17:15:38 +00:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/* lib64/
|
|
|
|
chmod u+w -R lib64/
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
# symlink 32-bit ld-linux.so
|
|
|
|
ln -Ls ${staticUsrProfileTarget}/lib/32/ld-linux.so.2 lib/
|
|
|
|
'';
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
setupLibDirs = if isTargetBuild
|
|
|
|
then setupLibDirsTarget
|
|
|
|
else setupLibDirsMulti;
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
# the target profile is the actual profile that will be used for the chroot
|
|
|
|
setupTargetProfile = ''
|
|
|
|
mkdir -m0755 usr
|
2023-07-15 17:15:38 +00:00
|
|
|
pushd usr
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
${setupLibDirs}
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
'' + lib.optionalString isMultiBuild ''
|
2021-02-24 18:30:23 +00:00
|
|
|
if [ -d "${staticUsrProfileMulti}/share" ]; then
|
|
|
|
cp -rLf ${staticUsrProfileMulti}/share share
|
|
|
|
fi
|
2023-07-15 17:15:38 +00:00
|
|
|
'' + ''
|
2021-02-24 18:30:23 +00:00
|
|
|
if [ -d "${staticUsrProfileTarget}/share" ]; then
|
|
|
|
if [ -d share ]; then
|
|
|
|
chmod -R 755 share
|
|
|
|
cp -rLTf ${staticUsrProfileTarget}/share share
|
|
|
|
else
|
2022-04-15 01:41:22 +00:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/share share
|
2021-02-24 18:30:23 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
for i in bin sbin include; do
|
2020-08-20 17:08:02 +00:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
cd ..
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
for i in var etc opt; do
|
2020-08-20 17:08:02 +00: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-15 17:15:38 +00:00
|
|
|
|
|
|
|
popd
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
in runCommandLocal "${name}-fhs" {
|
2023-03-30 22:05:00 +00:00
|
|
|
passthru = {
|
2024-04-21 15:54:59 +00:00
|
|
|
inherit args baseTargetPaths targetPaths baseMultiPaths ldconfig isMultiBuild;
|
2023-03-30 22:05:00 +00:00
|
|
|
};
|
2023-04-12 12:48:02 +00:00
|
|
|
} ''
|
|
|
|
mkdir -p $out
|
2023-07-15 17:15:38 +00:00
|
|
|
pushd $out
|
|
|
|
|
2023-04-12 12:48:02 +00:00
|
|
|
${setupTargetProfile}
|
|
|
|
${extraBuildCommands}
|
|
|
|
${lib.optionalString isMultiBuild extraBuildCommandsMulti}
|
|
|
|
''
|