Skip to content

Instantly share code, notes, and snippets.

@lpenz
Last active March 26, 2017 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpenz/81e388590adfb20fcb26affae03c96b2 to your computer and use it in GitHub Desktop.
Save lpenz/81e388590adfb20fcb26affae03c96b2 to your computer and use it in GitHub Desktop.
Enter a shell with ~/nix privately mounted in /nix, using mount namespaces. sudo required
#!/bin/bash
set -e -x
TMP=$(mktemp)
trap 'rm -f $TMP' EXIT
chmod u+x "$TMP"
cat > "$TMP" <<END
sudo mkdir /nix
sudo mount -n -o bind $HOME/nix /nix
if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
fi
END
if [ "$#" = 0 ]; then
echo "$@" >> "$TMP"
else
echo "$SHELL -i" >> "$TMP"
fi
sudo unshare -m -- sudo -u "$USER" "$TMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment