2019-02-20 15:22:17 +00:00
|
|
|
{ callPackage, runCommandLocal, writeShellScriptBin, stdenv, coreutils, bubblewrap }:
|
2020-07-31 12:58:03 +00:00
|
|
|
|
|
|
|
let buildFHSEnv = callPackage ./env.nix { }; in
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
args @ {
|
|
|
|
name,
|
|
|
|
runScript ? "bash",
|
|
|
|
extraInstallCommands ? "",
|
|
|
|
meta ? {},
|
|
|
|
passthru ? {},
|
|
|
|
...
|
|
|
|
}:
|
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
|
2019-02-20 15:22:17 +00:00
|
|
|
env = buildFHSEnv (removeAttrs args [
|
|
|
|
"runScript" "extraInstallCommands" "meta" "passthru"
|
|
|
|
]);
|
2020-07-31 12:58:03 +00:00
|
|
|
|
|
|
|
chrootenv = callPackage ./chrootenv {};
|
|
|
|
|
2019-09-15 13:29:53 +00:00
|
|
|
etcBindFlags = let
|
|
|
|
files = [
|
|
|
|
# NixOS Compatibility
|
|
|
|
"static"
|
|
|
|
# Users, Groups, NSS
|
|
|
|
"passwd"
|
|
|
|
"group"
|
|
|
|
"shadow"
|
|
|
|
"hosts"
|
|
|
|
"resolv.conf"
|
|
|
|
"nsswitch.conf"
|
|
|
|
# Sudo & Su
|
|
|
|
"login.defs"
|
|
|
|
"sudoers"
|
|
|
|
"sudoers.d"
|
|
|
|
# Time
|
|
|
|
"localtime"
|
|
|
|
"zoneinfo"
|
|
|
|
# Other Core Stuff
|
|
|
|
"machine-id"
|
|
|
|
"os-release"
|
|
|
|
# PAM
|
|
|
|
"pam.d"
|
|
|
|
# Fonts
|
|
|
|
"fonts"
|
|
|
|
# ALSA
|
|
|
|
"asound.conf"
|
|
|
|
# SSL
|
|
|
|
"ssl/certs"
|
|
|
|
"pki"
|
|
|
|
];
|
|
|
|
in concatStringsSep " \\\n "
|
|
|
|
(map (file: "--ro-bind-try /etc/${file} /etc/${file}") files);
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
init = run: writeShellScriptBin "${name}-init" ''
|
|
|
|
source /etc/profile
|
|
|
|
exec ${run} "$@"
|
|
|
|
'';
|
|
|
|
|
2020-08-17 07:49:34 +00:00
|
|
|
bwrapCmd = { initArgs ? "" }: ''
|
2019-09-15 13:29:53 +00:00
|
|
|
blacklist="/nix /dev /proc /etc"
|
2019-02-20 15:22:17 +00:00
|
|
|
ro_mounts=""
|
|
|
|
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
|
|
|
|
continue
|
|
|
|
fi
|
2019-02-20 15:22:17 +00:00
|
|
|
ro_mounts="$ro_mounts --ro-bind $i $path"
|
|
|
|
blacklist="$blacklist $path"
|
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##*/}"
|
|
|
|
ro_mounts="$ro_mounts --ro-bind $i /etc$path"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
auto_mounts=""
|
|
|
|
# loop through all directories in the root
|
|
|
|
for dir in /*; do
|
|
|
|
# if it is a directory and it is not in the blacklist
|
|
|
|
if [[ -d "$dir" ]] && grep -v "$dir" <<< "$blacklist" >/dev/null; then
|
|
|
|
# add it to the mount list
|
|
|
|
auto_mounts="$auto_mounts --bind $dir $dir"
|
|
|
|
fi
|
|
|
|
done
|
2020-07-31 12:58:03 +00:00
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
exec ${bubblewrap}/bin/bwrap \
|
2019-09-15 19:41:48 +00:00
|
|
|
--dev-bind /dev /dev \
|
2019-02-20 15:22:17 +00:00
|
|
|
--proc /proc \
|
|
|
|
--chdir "$(pwd)" \
|
|
|
|
--unshare-all \
|
|
|
|
--share-net \
|
|
|
|
--die-with-parent \
|
|
|
|
--ro-bind /nix /nix \
|
2019-09-15 13:29:53 +00:00
|
|
|
${etcBindFlags} \
|
2019-02-20 15:22:17 +00:00
|
|
|
$ro_mounts \
|
|
|
|
$auto_mounts \
|
2020-08-17 07:49:34 +00:00
|
|
|
${init runScript}/bin/${name}-init ${initArgs}
|
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}
|
|
|
|
''
|