2019-02-20 16:22:17 +01:00
|
|
|
{ callPackage, runCommandLocal, writeShellScriptBin, stdenv, coreutils, bubblewrap }:
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
let buildFHSEnv = callPackage ./env.nix { }; in
|
|
|
|
|
2019-02-20 16:22:17 +01:00
|
|
|
args @ {
|
|
|
|
name,
|
|
|
|
runScript ? "bash",
|
|
|
|
extraInstallCommands ? "",
|
|
|
|
meta ? {},
|
|
|
|
passthru ? {},
|
|
|
|
...
|
|
|
|
}:
|
2020-07-31 14:58:03 +02:00
|
|
|
|
2019-02-20 16:22:17 +01:00
|
|
|
with builtins;
|
2020-07-31 14:58:03 +02:00
|
|
|
let
|
2019-02-20 16:22:17 +01:00
|
|
|
env = buildFHSEnv (removeAttrs args [
|
|
|
|
"runScript" "extraInstallCommands" "meta" "passthru"
|
|
|
|
]);
|
2020-07-31 14:58:03 +02:00
|
|
|
|
|
|
|
chrootenv = callPackage ./chrootenv {};
|
|
|
|
|
2019-02-20 16:22:17 +01:00
|
|
|
init = run: writeShellScriptBin "${name}-init" ''
|
|
|
|
source /etc/profile
|
|
|
|
exec ${run} "$@"
|
|
|
|
'';
|
|
|
|
|
|
|
|
bwrap_cmd = { init_args ? "" }: ''
|
|
|
|
blacklist="/nix /dev /proc"
|
|
|
|
ro_mounts=""
|
|
|
|
for i in ${env}/*; do
|
2020-07-31 14:58:03 +02:00
|
|
|
path="/''${i##*/}"
|
2019-02-20 16:22:17 +01:00
|
|
|
ro_mounts="$ro_mounts --ro-bind $i $path"
|
|
|
|
blacklist="$blacklist $path"
|
2020-07-31 14:58:03 +02:00
|
|
|
done
|
|
|
|
|
2019-02-20 16:22:17 +01: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 14:58:03 +02:00
|
|
|
|
2019-02-20 16:22:17 +01:00
|
|
|
exec ${bubblewrap}/bin/bwrap \
|
|
|
|
--dev /dev \
|
|
|
|
--proc /proc \
|
|
|
|
--chdir "$(pwd)" \
|
|
|
|
--unshare-all \
|
|
|
|
--share-net \
|
|
|
|
--die-with-parent \
|
|
|
|
--ro-bind /nix /nix \
|
|
|
|
--ro-bind /etc /host-etc \
|
|
|
|
$ro_mounts \
|
|
|
|
$auto_mounts \
|
|
|
|
${init runScript}/bin/${name}-init ${init_args}
|
2020-07-31 14:58:03 +02:00
|
|
|
'';
|
|
|
|
|
2019-02-20 16:22:17 +01:00
|
|
|
bin = writeShellScriptBin name (bwrap_cmd { init_args = ''"$@"''; });
|
|
|
|
|
2020-07-31 14:58:03 +02:00
|
|
|
in runCommandLocal name {
|
|
|
|
inherit meta;
|
|
|
|
|
|
|
|
passthru = passthru // {
|
|
|
|
env = runCommandLocal "${name}-shell-env" {
|
2019-02-20 16:22:17 +01:00
|
|
|
shellHook = bwrap_cmd {};
|
2020-07-31 14:58:03 +02: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 16:22:17 +01:00
|
|
|
ln -s ${bin}/bin/${name} $out/bin/${name}
|
2020-07-31 14:58:03 +02:00
|
|
|
${extraInstallCommands}
|
|
|
|
''
|