2023-02-06 20:49:02 +00:00
|
|
|
{ stdenv, lib, buildEnv, writeText, pkgs, pkgsi686Linux }:
|
2016-06-05 01:44:06 +00:00
|
|
|
|
2021-07-30 09:17:19 +00:00
|
|
|
{ name
|
|
|
|
, profile ? ""
|
|
|
|
, targetPkgs ? pkgs: []
|
|
|
|
, multiPkgs ? pkgs: []
|
|
|
|
, extraBuildCommands ? ""
|
|
|
|
, extraBuildCommandsMulti ? ""
|
2016-04-13 11:28:33 +00:00
|
|
|
, extraOutputsToInstall ? []
|
2015-02-05 15:12:14 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# HOWTO:
|
2015-07-28 11:06:06 +00: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 15:12:14 +00:00
|
|
|
#
|
2015-07-28 11:06:06 +00: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 15:12:14 +00:00
|
|
|
# /lib64 will include 64bit libraries from multiPkgs and targetPkgs
|
2015-07-28 11:06:06 +00:00
|
|
|
# /lib will link to /lib32
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
let
|
2018-08-20 19:11:29 +00:00
|
|
|
is64Bit = stdenv.hostPlatform.parsed.cpu.bits == 64;
|
2021-07-30 09:17:19 +00:00
|
|
|
# multi-lib glibc is only supported on x86_64
|
|
|
|
isMultiBuild = multiPkgs != null && stdenv.hostPlatform.system == "x86_64-linux";
|
2015-02-05 15:12:14 +00:00
|
|
|
isTargetBuild = !isMultiBuild;
|
|
|
|
|
2015-07-28 11:06:06 +00:00
|
|
|
# list of packages (usually programs) which are only be installed for the
|
|
|
|
# host's architecture
|
2016-06-05 01:44:06 +00:00
|
|
|
targetPaths = targetPkgs pkgs ++ (if multiPkgs == null then [] else multiPkgs pkgs);
|
2015-02-05 15:12:14 +00:00
|
|
|
|
2015-07-28 11:06:06 +00:00
|
|
|
# list of packages which are installed for both x86 and x86_64 on x86_64
|
2015-02-05 15:12:14 +00:00
|
|
|
# systems
|
2016-06-05 01:44:06 +00:00
|
|
|
multiPaths = multiPkgs pkgsi686Linux;
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# base packages of the chroot
|
2016-05-29 18:40:53 +00:00
|
|
|
# these match the host's architecture, glibc_multi is used for multilib
|
2019-04-02 20:42:59 +00:00
|
|
|
# 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.
|
2016-06-05 01:44:06 +00:00
|
|
|
basePkgs = with pkgs;
|
2019-04-02 20:42:59 +00:00
|
|
|
[ glibcLocales
|
|
|
|
(if isMultiBuild then glibc_multi else glibc)
|
2022-02-19 23:34:55 +00:00
|
|
|
(toString gcc.cc.lib) bashInteractiveFHS coreutils less shadow su
|
2015-02-05 15:12:14 +00:00
|
|
|
gawk diffutils findutils gnused gnugrep
|
2019-04-02 20:42:59 +00:00
|
|
|
gnutar gzip bzip2 xz
|
2015-02-05 15:12:14 +00:00
|
|
|
];
|
2016-06-05 01:44:06 +00:00
|
|
|
baseMultiPkgs = with pkgsi686Linux;
|
2016-06-05 13:12:25 +00:00
|
|
|
[ (toString gcc.cc.lib)
|
2016-05-29 18:40:53 +00:00
|
|
|
];
|
2015-02-05 15:12:14 +00:00
|
|
|
|
2016-06-05 01:44:06 +00:00
|
|
|
etcProfile = writeText "profile" ''
|
2015-10-21 12:57:58 +00:00
|
|
|
export PS1='${name}-chrootenv:\u@\h:\w\$ '
|
2015-11-10 20:59:57 +00:00
|
|
|
export LOCALE_ARCHIVE='/usr/lib/locale/locale-archive'
|
2020-01-02 00:29:34 +00:00
|
|
|
export LD_LIBRARY_PATH="/run/opengl-driver/lib:/run/opengl-driver-32/lib:/usr/lib:/usr/lib32''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
|
2018-10-14 23:37:28 +00:00
|
|
|
export PATH="/run/wrappers/bin:/usr/bin:/usr/sbin:$PATH"
|
2018-02-14 12:03:59 +00:00
|
|
|
export TZDIR='/etc/zoneinfo'
|
2016-03-14 23:35:00 +00:00
|
|
|
|
2022-03-15 05:39:36 +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-07 01:03:42 +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
|
|
|
|
|
2017-04-12 11:41:43 +00:00
|
|
|
# Force compilers and other tools to look in default search paths
|
2018-03-10 20:53:53 +00:00
|
|
|
unset NIX_ENFORCE_PURITY
|
2020-04-28 04:08:48 +00:00
|
|
|
export NIX_CC_WRAPPER_TARGET_HOST_${stdenv.cc.suffixSalt}=1
|
2016-03-14 23:35:00 +00:00
|
|
|
export NIX_CFLAGS_COMPILE='-idirafter /usr/include'
|
2018-03-10 20:53:53 +00:00
|
|
|
export NIX_CFLAGS_LINK='-L/usr/lib -L/usr/lib32'
|
|
|
|
export NIX_LDFLAGS='-L/usr/lib -L/usr/lib32'
|
2017-04-12 11:41:43 +00:00
|
|
|
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
|
|
|
|
export ACLOCAL_PATH=/usr/share/aclocal
|
2016-03-14 23:35:00 +00:00
|
|
|
|
2015-10-21 12:57:58 +00:00
|
|
|
${profile}
|
|
|
|
'';
|
|
|
|
|
2015-04-22 12:46:49 +00:00
|
|
|
# Compose /etc for the chroot environment
|
2016-06-05 01:44:06 +00:00
|
|
|
etcPkg = stdenv.mkDerivation {
|
2015-04-22 12:46:49 +00:00
|
|
|
name = "${name}-chrootenv-etc";
|
2015-02-05 15:12:14 +00:00
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/etc
|
2015-04-22 12:46:49 +00:00
|
|
|
cd $out/etc
|
|
|
|
|
|
|
|
# environment variables
|
2015-10-21 12:57:58 +00:00
|
|
|
ln -s ${etcProfile} profile
|
2015-04-22 12:46:49 +00:00
|
|
|
|
|
|
|
# compatibility with NixOS
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/static static
|
2015-04-22 12:46:49 +00:00
|
|
|
|
2021-02-25 19:34:17 +00:00
|
|
|
# symlink nix config
|
|
|
|
ln -s /host/etc/nix nix
|
|
|
|
|
2015-04-22 12:46:49 +00:00
|
|
|
# symlink some NSS stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
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-04-22 12:46:49 +00:00
|
|
|
|
2021-01-26 00:41:50 +00:00
|
|
|
# symlink user profiles
|
|
|
|
ln -s /host/etc/profiles profiles
|
|
|
|
|
2015-12-15 18:31:12 +00:00
|
|
|
# symlink sudo and su stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/login.defs login.defs
|
|
|
|
ln -s /host/etc/sudoers sudoers
|
|
|
|
ln -s /host/etc/sudoers.d sudoers.d
|
2015-12-15 18:31:12 +00:00
|
|
|
|
2015-04-22 12:46:49 +00:00
|
|
|
# symlink other core stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/localtime localtime
|
2016-10-11 11:51:15 +00:00
|
|
|
ln -s /host/etc/zoneinfo zoneinfo
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/machine-id machine-id
|
|
|
|
ln -s /host/etc/os-release os-release
|
2015-04-22 12:46:49 +00:00
|
|
|
|
|
|
|
# symlink PAM stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/pam.d pam.d
|
2015-04-22 12:46:49 +00:00
|
|
|
|
|
|
|
# symlink fonts stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/fonts fonts
|
2015-04-22 12:46:49 +00:00
|
|
|
|
|
|
|
# symlink ALSA stuff
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/asound.conf asound.conf
|
2015-04-22 12:46:49 +00:00
|
|
|
|
|
|
|
# symlink SSL certs
|
|
|
|
mkdir -p ssl
|
2016-06-05 01:44:06 +00:00
|
|
|
ln -s /host/etc/ssl/certs ssl/certs
|
2015-10-21 19:34:12 +00:00
|
|
|
|
|
|
|
# symlink /etc/mtab -> /proc/mounts (compat for old userspace progs)
|
|
|
|
ln -s /proc/mounts mtab
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-07-28 11:06:06 +00:00
|
|
|
# Composes a /usr-like directory structure
|
2016-06-05 01:44:06 +00:00
|
|
|
staticUsrProfileTarget = buildEnv {
|
2015-04-22 12:46:49 +00:00
|
|
|
name = "${name}-usr-target";
|
|
|
|
paths = [ etcPkg ] ++ basePkgs ++ targetPaths;
|
2016-06-07 00:50:16 +00:00
|
|
|
extraOutputsToInstall = [ "out" "lib" "bin" ] ++ extraOutputsToInstall;
|
2015-03-09 14:13:53 +00:00
|
|
|
ignoreCollisions = true;
|
2022-02-24 23:08:47 +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
|
2022-03-16 12:47:27 +00: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-24 23:08:47 +00:00
|
|
|
fi
|
|
|
|
|
2022-03-16 12:47:27 +00: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-24 23:08:47 +00:00
|
|
|
fi
|
|
|
|
|
2022-03-16 12:47:27 +00:00
|
|
|
mkdir -p $out/share/glib-2.0/schemas
|
|
|
|
|
2022-02-24 23:08:47 +00: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
|
|
|
|
'';
|
2015-02-05 15:12:14 +00:00
|
|
|
};
|
|
|
|
|
2016-06-05 01:44:06 +00:00
|
|
|
staticUsrProfileMulti = buildEnv {
|
2016-05-29 18:40:53 +00:00
|
|
|
name = "${name}-usr-multi";
|
|
|
|
paths = baseMultiPkgs ++ multiPaths;
|
2016-06-07 00:50:16 +00:00
|
|
|
extraOutputsToInstall = [ "out" "lib" ] ++ extraOutputsToInstall;
|
2015-03-09 14:13:53 +00:00
|
|
|
ignoreCollisions = true;
|
2015-02-05 15:12:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# setup library paths only for the targeted architecture
|
|
|
|
setupLibDirs_target = ''
|
2016-06-05 01:44:06 +00:00
|
|
|
# link content of targetPaths
|
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib lib
|
|
|
|
ln -s lib lib${if is64Bit then "64" else "32"}
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# setup /lib, /lib32 and /lib64
|
|
|
|
setupLibDirs_multi = ''
|
2015-07-28 11:06:06 +00:00
|
|
|
mkdir -m0755 lib32
|
2015-02-05 15:12:14 +00:00
|
|
|
mkdir -m0755 lib64
|
2015-11-10 20:59:57 +00:00
|
|
|
ln -s lib64 lib
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy glibc stuff
|
2015-07-28 11:06:06 +00:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/32/* lib32/ && chmod u+w -R lib32/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy content of multiPaths (32bit libs)
|
2015-07-28 11:06:06 +00:00
|
|
|
[ -d ${staticUsrProfileMulti}/lib ] && cp -rsHf ${staticUsrProfileMulti}/lib/* lib32/ && chmod u+w -R lib32/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
|
|
|
# copy content of targetPaths (64bit libs)
|
2015-07-28 11:06:06 +00:00
|
|
|
cp -rsHf ${staticUsrProfileTarget}/lib/* lib64/ && chmod u+w -R lib64/
|
2015-02-05 15:12:14 +00:00
|
|
|
|
2015-11-10 20:59:57 +00:00
|
|
|
# symlink 32-bit ld-linux.so
|
2016-05-29 18:40:53 +00:00
|
|
|
ln -Ls ${staticUsrProfileTarget}/lib/32/ld-linux.so.2 lib/
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
|
|
|
|
2015-04-22 12:46:49 +00:00
|
|
|
setupLibDirs = if isTargetBuild then setupLibDirs_target
|
|
|
|
else setupLibDirs_multi;
|
2015-03-09 14:13:27 +00:00
|
|
|
|
2015-04-22 12:46:49 +00: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 11:06:06 +00:00
|
|
|
for i in bin sbin share include; do
|
2015-11-23 16:49:20 +00:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
2015-11-23 18:39:49 +00:00
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
2015-11-23 16:49:20 +00:00
|
|
|
fi
|
2015-07-28 11:06:06 +00:00
|
|
|
done
|
2015-04-22 12:46:49 +00:00
|
|
|
cd ..
|
2015-11-23 18:39:49 +00:00
|
|
|
|
2021-06-25 16:08:56 +00:00
|
|
|
for i in var etc opt; do
|
2015-11-23 16:49:20 +00:00
|
|
|
if [ -d "${staticUsrProfileTarget}/$i" ]; then
|
2015-11-23 18:39:49 +00:00
|
|
|
cp -rsHf "${staticUsrProfileTarget}/$i" "$i"
|
2015-11-23 16:49:20 +00:00
|
|
|
fi
|
2015-07-28 11:06:06 +00:00
|
|
|
done
|
|
|
|
for i in usr/{bin,sbin,lib,lib32,lib64}; do
|
2015-11-23 16:49:20 +00:00
|
|
|
if [ -d "$i" ]; then
|
2015-07-28 11:06:06 +00:00
|
|
|
ln -s "$i"
|
|
|
|
fi
|
|
|
|
done
|
2015-03-09 14:13:27 +00:00
|
|
|
'';
|
|
|
|
|
2016-06-05 01:44:06 +00:00
|
|
|
in stdenv.mkDerivation {
|
2015-02-05 15:12:14 +00:00
|
|
|
name = "${name}-fhs";
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out
|
|
|
|
cd $out
|
|
|
|
${setupTargetProfile}
|
|
|
|
cd $out
|
|
|
|
${extraBuildCommands}
|
|
|
|
cd $out
|
2023-02-06 20:49:02 +00:00
|
|
|
${lib.optionalString isMultiBuild extraBuildCommandsMulti}
|
2015-02-05 15:12:14 +00:00
|
|
|
'';
|
2015-02-05 17:39:01 +00:00
|
|
|
preferLocalBuild = true;
|
2019-02-08 00:27:40 +00:00
|
|
|
allowSubstitutes = false;
|
2015-02-05 15:12:14 +00:00
|
|
|
}
|