2021-02-04 01:16:20 +00:00
|
|
|
{ lib, callPackage, runCommandLocal, writeShellScriptBin, glibc, pkgsi686Linux, coreutils, bubblewrap }:
|
2020-10-30 18:22:04 +00:00
|
|
|
|
|
|
|
let buildFHSEnv = callPackage ./env.nix { }; in
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
args @ {
|
2020-12-09 02:20:44 +00:00
|
|
|
name
|
|
|
|
, runScript ? "bash"
|
|
|
|
, extraInstallCommands ? ""
|
|
|
|
, meta ? {}
|
|
|
|
, passthru ? {}
|
|
|
|
, unshareUser ? true
|
|
|
|
, unshareIpc ? true
|
|
|
|
, unsharePid ? true
|
|
|
|
, unshareNet ? false
|
|
|
|
, unshareUts ? true
|
|
|
|
, unshareCgroup ? true
|
2021-04-08 05:16:30 +00:00
|
|
|
, dieWithParent ? true
|
2020-12-09 02:20:44 +00:00
|
|
|
, ...
|
2019-02-20 15:22:17 +00:00
|
|
|
}:
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
with builtins;
|
2020-07-31 12:58:03 +00:00
|
|
|
let
|
2020-12-09 02:20:44 +00:00
|
|
|
buildFHSEnv = callPackage ./env.nix { };
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
env = buildFHSEnv (removeAttrs args [
|
2021-04-08 05:16:30 +00:00
|
|
|
"runScript" "extraInstallCommands" "meta" "passthru" "dieWithParent"
|
2020-12-09 02:20:44 +00:00
|
|
|
"unshareUser" "unshareCgroup" "unshareUts" "unshareNet" "unsharePid" "unshareIpc"
|
2019-02-20 15:22:17 +00:00
|
|
|
]);
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2019-09-15 13:29:53 +00:00
|
|
|
etcBindFlags = let
|
|
|
|
files = [
|
|
|
|
# NixOS Compatibility
|
|
|
|
"static"
|
2021-04-08 05:16:30 +00:00
|
|
|
"nix" # mainly for nixUnstable users, but also for access to nix/netrc
|
|
|
|
# Shells
|
|
|
|
"bashrc"
|
|
|
|
"zshenv"
|
|
|
|
"zshrc"
|
|
|
|
"zinputrc"
|
|
|
|
"zprofile"
|
2019-09-15 13:29:53 +00:00
|
|
|
# Users, Groups, NSS
|
|
|
|
"passwd"
|
|
|
|
"group"
|
|
|
|
"shadow"
|
|
|
|
"hosts"
|
|
|
|
"resolv.conf"
|
|
|
|
"nsswitch.conf"
|
2021-01-26 00:41:50 +00:00
|
|
|
# User profiles
|
|
|
|
"profiles"
|
2019-09-15 13:29:53 +00:00
|
|
|
# Sudo & Su
|
|
|
|
"login.defs"
|
|
|
|
"sudoers"
|
|
|
|
"sudoers.d"
|
|
|
|
# Time
|
|
|
|
"localtime"
|
|
|
|
"zoneinfo"
|
|
|
|
# Other Core Stuff
|
|
|
|
"machine-id"
|
|
|
|
"os-release"
|
|
|
|
# PAM
|
|
|
|
"pam.d"
|
|
|
|
# Fonts
|
|
|
|
"fonts"
|
|
|
|
# ALSA
|
2021-07-10 10:50:52 +00:00
|
|
|
"alsa"
|
2019-09-15 13:29:53 +00:00
|
|
|
"asound.conf"
|
|
|
|
# SSL
|
|
|
|
"ssl/certs"
|
|
|
|
"pki"
|
|
|
|
];
|
2020-09-20 15:02:08 +00:00
|
|
|
in concatStringsSep "\n "
|
2019-09-15 13:29:53 +00:00
|
|
|
(map (file: "--ro-bind-try /etc/${file} /etc/${file}") files);
|
|
|
|
|
2020-10-30 18:22:04 +00:00
|
|
|
# Create this on the fly instead of linking from /nix
|
|
|
|
# The container might have to modify it and re-run ldconfig if there are
|
|
|
|
# issues running some binary with LD_LIBRARY_PATH
|
|
|
|
createLdConfCache = ''
|
|
|
|
cat > /etc/ld.so.conf <<EOF
|
|
|
|
/lib
|
|
|
|
/lib/x86_64-linux-gnu
|
|
|
|
/lib64
|
|
|
|
/usr/lib
|
|
|
|
/usr/lib/x86_64-linux-gnu
|
|
|
|
/usr/lib64
|
|
|
|
/lib/i386-linux-gnu
|
|
|
|
/lib32
|
|
|
|
/usr/lib/i386-linux-gnu
|
|
|
|
/usr/lib32
|
|
|
|
EOF
|
|
|
|
ldconfig &> /dev/null
|
|
|
|
'';
|
2019-02-20 15:22:17 +00:00
|
|
|
init = run: writeShellScriptBin "${name}-init" ''
|
|
|
|
source /etc/profile
|
2020-10-30 18:22:04 +00:00
|
|
|
${createLdConfCache}
|
2019-02-20 15:22:17 +00:00
|
|
|
exec ${run} "$@"
|
|
|
|
'';
|
|
|
|
|
2020-08-17 07:49:34 +00:00
|
|
|
bwrapCmd = { initArgs ? "" }: ''
|
2020-09-20 15:02:08 +00:00
|
|
|
blacklist=(/nix /dev /proc /etc)
|
|
|
|
ro_mounts=()
|
2020-10-24 15:23:37 +00:00
|
|
|
symlinks=()
|
2019-02-20 15:22:17 +00:00
|
|
|
for i in ${env}/*; do
|
2020-07-31 12:58:03 +00:00
|
|
|
path="/''${i##*/}"
|
2019-09-15 13:29:53 +00:00
|
|
|
if [[ $path == '/etc' ]]; then
|
2020-10-24 15:23:37 +00:00
|
|
|
:
|
|
|
|
elif [[ -L $i ]]; then
|
2021-03-11 13:27:14 +00:00
|
|
|
symlinks+=(--symlink "$(${coreutils}/bin/readlink "$i")" "$path")
|
2020-10-24 15:23:37 +00:00
|
|
|
blacklist+=("$path")
|
|
|
|
else
|
|
|
|
ro_mounts+=(--ro-bind "$i" "$path")
|
|
|
|
blacklist+=("$path")
|
2019-09-15 13:29:53 +00:00
|
|
|
fi
|
2020-07-31 12:58:03 +00:00
|
|
|
done
|
|
|
|
|
2019-09-15 13:29:53 +00:00
|
|
|
if [[ -d ${env}/etc ]]; then
|
|
|
|
for i in ${env}/etc/*; do
|
|
|
|
path="/''${i##*/}"
|
2021-02-22 19:54:04 +00:00
|
|
|
# NOTE: we're binding /etc/fonts and /etc/ssl/certs from the host so we
|
|
|
|
# don't want to override it with a path from the FHS environment.
|
|
|
|
if [[ $path == '/fonts' || $path == '/ssl' ]]; then
|
2021-01-26 00:42:32 +00:00
|
|
|
continue
|
|
|
|
fi
|
2020-09-20 15:02:08 +00:00
|
|
|
ro_mounts+=(--ro-bind "$i" "/etc$path")
|
2019-09-15 13:29:53 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-10-28 20:00:17 +00:00
|
|
|
declare -a auto_mounts
|
2019-02-20 15:22:17 +00:00
|
|
|
# loop through all directories in the root
|
|
|
|
for dir in /*; do
|
|
|
|
# if it is a directory and it is not in the blacklist
|
2020-09-20 15:02:08 +00:00
|
|
|
if [[ -d "$dir" ]] && [[ ! "''${blacklist[@]}" =~ "$dir" ]]; then
|
2019-02-20 15:22:17 +00:00
|
|
|
# add it to the mount list
|
2020-10-28 20:00:17 +00:00
|
|
|
auto_mounts+=(--bind "$dir" "$dir")
|
2019-02-20 15:22:17 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2020-09-20 15:02:08 +00:00
|
|
|
cmd=(
|
|
|
|
${bubblewrap}/bin/bwrap
|
|
|
|
--dev-bind /dev /dev
|
|
|
|
--proc /proc
|
|
|
|
--chdir "$(pwd)"
|
2020-12-09 02:20:44 +00:00
|
|
|
${lib.optionalString unshareUser "--unshare-user"}
|
|
|
|
${lib.optionalString unshareIpc "--unshare-ipc"}
|
|
|
|
${lib.optionalString unsharePid "--unshare-pid"}
|
|
|
|
${lib.optionalString unshareNet "--unshare-net"}
|
|
|
|
${lib.optionalString unshareUts "--unshare-uts"}
|
|
|
|
${lib.optionalString unshareCgroup "--unshare-cgroup"}
|
2021-04-08 05:16:30 +00:00
|
|
|
${lib.optionalString dieWithParent "--die-with-parent"}
|
2020-09-20 15:02:08 +00:00
|
|
|
--ro-bind /nix /nix
|
2021-02-23 14:44:16 +00:00
|
|
|
# Our glibc will look for the cache in its own path in `/nix/store`.
|
|
|
|
# As such, we need a cache to exist there, because pressure-vessel
|
|
|
|
# depends on the existence of an ld cache. However, adding one
|
|
|
|
# globally proved to be a bad idea (see #100655), the solution we
|
|
|
|
# settled on being mounting one via bwrap.
|
|
|
|
# Also, the cache needs to go to both 32 and 64 bit glibcs, for games
|
|
|
|
# of both architectures to work.
|
2020-10-30 18:22:04 +00:00
|
|
|
--tmpfs ${glibc}/etc \
|
|
|
|
--symlink /etc/ld.so.conf ${glibc}/etc/ld.so.conf \
|
|
|
|
--symlink /etc/ld.so.cache ${glibc}/etc/ld.so.cache \
|
|
|
|
--ro-bind ${glibc}/etc/rpc ${glibc}/etc/rpc \
|
|
|
|
--remount-ro ${glibc}/etc \
|
2021-02-04 01:16:20 +00:00
|
|
|
--tmpfs ${pkgsi686Linux.glibc}/etc \
|
|
|
|
--symlink /etc/ld.so.conf ${pkgsi686Linux.glibc}/etc/ld.so.conf \
|
|
|
|
--symlink /etc/ld.so.cache ${pkgsi686Linux.glibc}/etc/ld.so.cache \
|
|
|
|
--ro-bind ${pkgsi686Linux.glibc}/etc/rpc ${pkgsi686Linux.glibc}/etc/rpc \
|
|
|
|
--remount-ro ${pkgsi686Linux.glibc}/etc \
|
2020-09-20 15:02:08 +00:00
|
|
|
${etcBindFlags}
|
|
|
|
"''${ro_mounts[@]}"
|
2020-10-24 15:23:37 +00:00
|
|
|
"''${symlinks[@]}"
|
2020-09-20 15:02:08 +00:00
|
|
|
"''${auto_mounts[@]}"
|
2020-08-17 07:49:34 +00:00
|
|
|
${init runScript}/bin/${name}-init ${initArgs}
|
2020-09-20 15:02:08 +00:00
|
|
|
)
|
|
|
|
exec "''${cmd[@]}"
|
2020-07-31 12:58:03 +00:00
|
|
|
'';
|
|
|
|
|
2020-08-17 07:49:34 +00:00
|
|
|
bin = writeShellScriptBin name (bwrapCmd { initArgs = ''"$@"''; });
|
2019-02-20 15:22:17 +00:00
|
|
|
|
2020-07-31 12:58:03 +00:00
|
|
|
in runCommandLocal name {
|
|
|
|
inherit meta;
|
|
|
|
|
|
|
|
passthru = passthru // {
|
|
|
|
env = runCommandLocal "${name}-shell-env" {
|
2020-08-17 07:49:34 +00:00
|
|
|
shellHook = bwrapCmd {};
|
2020-07-31 12:58:03 +00:00
|
|
|
} ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** User chroot 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
2019-02-20 15:22:17 +00:00
|
|
|
ln -s ${bin}/bin/${name} $out/bin/${name}
|
2020-07-31 12:58:03 +00:00
|
|
|
${extraInstallCommands}
|
|
|
|
''
|