Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 0 additions & 159 deletions pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c

This file was deleted.

16 changes: 0 additions & 16 deletions pkgs/build-support/build-fhs-userenv/chrootenv/default.nix

This file was deleted.

5 changes: 0 additions & 5 deletions pkgs/build-support/build-fhs-userenv/chrootenv/meson.build

This file was deleted.

112 changes: 93 additions & 19 deletions pkgs/build-support/build-fhs-userenv/default.nix
Original file line number Diff line number Diff line change
@@ -1,35 +1,113 @@
{ callPackage, runCommand, writeScript, stdenv, coreutils }:
{ callPackage, runCommand, writeShellScriptBin, stdenv, coreutils, bubblewrap }:

let buildFHSEnv = callPackage ./env.nix { }; in

args@{ name, runScript ? "bash", extraInstallCommands ? "", meta ? {}, passthru ? {}, ... }:
args @ {
name,
runScript ? "bash",
extraInstallCommands ? "",
meta ? {},
passthru ? {},
...
}:

with builtins;
let
env = buildFHSEnv (removeAttrs args [ "runScript" "extraInstallCommands" "meta" "passthru" ]);
env = buildFHSEnv (removeAttrs args [
"runScript" "extraInstallCommands" "meta" "passthru"
]);

chrootenv = callPackage ./chrootenv {};

init = run: writeScript "${name}-init" ''
#! ${stdenv.shell}
for i in ${env}/* /host/*; do
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);

init = run: writeShellScriptBin "${name}-init" ''
source /etc/profile
exec ${run} "$@"
'';

bwrap_cmd = { init_args ? "" }: ''
blacklist="/nix /dev /proc /etc"
ro_mounts=""
for i in ${env}/*; do
path="/''${i##*/}"
[ -e "$path" ] || ${coreutils}/bin/ln -s "$i" "$path"
if [[ $path == '/etc' ]]; then
continue
fi
ro_mounts="$ro_mounts --ro-bind $i $path"
blacklist="$blacklist $path"
done

[ -d "$1" ] && [ -r "$1" ] && cd "$1"
shift
if [[ -d ${env}/etc ]]; then
for i in ${env}/etc/*; do
path="/''${i##*/}"
ro_mounts="$ro_mounts --ro-bind $i /etc$path"
done
fi

source /etc/profile
exec ${run} "$@"
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

exec ${bubblewrap}/bin/bwrap \
--dev-bind /dev /dev \
--proc /proc \
--chdir "$(pwd)" \
--unshare-all \
--share-net \
--die-with-parent \
--ro-bind /nix /nix \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this break single user nix?

${etcBindFlags} \
$ro_mounts \
$auto_mounts \
${init runScript}/bin/${name}-init ${init_args}
'';

bin = writeShellScriptBin name (bwrap_cmd { init_args = ''"$@"''; });

in runCommand name {
inherit meta;
passthru = passthru // {
env = runCommand "${name}-shell-env" {
shellHook = ''
exec ${chrootenv}/bin/chrootenv ${init runScript} "$(pwd)"
'';
shellHook = bwrap_cmd {};
} ''
echo >&2 ""
echo >&2 "*** User chroot 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
Expand All @@ -39,10 +117,6 @@ in runCommand name {
};
} ''
mkdir -p $out/bin
cat <<EOF >$out/bin/${name}
#! ${stdenv.shell}
exec ${chrootenv}/bin/chrootenv ${init runScript} "\$(pwd)" "\$@"
EOF
chmod +x $out/bin/${name}
ln -s ${bin}/bin/${name} $out/bin/${name}
${extraInstallCommands}
''
35 changes: 0 additions & 35 deletions pkgs/build-support/build-fhs-userenv/env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,6 @@ let
# environment variables
ln -s ${etcProfile} profile

# compatibility with NixOS
ln -s /host/etc/static static

# symlink some NSS stuff
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

# symlink sudo and su stuff
ln -s /host/etc/login.defs login.defs
ln -s /host/etc/sudoers sudoers
ln -s /host/etc/sudoers.d sudoers.d

# symlink other core stuff
ln -s /host/etc/localtime localtime
ln -s /host/etc/zoneinfo zoneinfo
ln -s /host/etc/machine-id machine-id
ln -s /host/etc/os-release os-release

# symlink PAM stuff
ln -s /host/etc/pam.d pam.d

# symlink fonts stuff
ln -s /host/etc/fonts fonts

# symlink ALSA stuff
ln -s /host/etc/asound.conf asound.conf

# symlink SSL certs
mkdir -p ssl
ln -s /host/etc/ssl/certs ssl/certs

# symlink /etc/mtab -> /proc/mounts (compat for old userspace progs)
ln -s /proc/mounts mtab
'';
Expand Down