Skip to content

Instantly share code, notes, and snippets.

@stealthybox
Last active November 23, 2024 01:39
Show Gist options
  • Save stealthybox/4511dcb77351e2636321aa860b5e3618 to your computer and use it in GitHub Desktop.
Save stealthybox/4511dcb77351e2636321aa860b5e3618 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -euxo pipefail
NIX_DIR="${HOME}/.nix"
# install nix-user-chroot binary
# how to test if feature is available:
# unshare --user --pid echo YES
mkdir -p ~/bin/
wget -O ~/bin/nix-user-chroot \
https://github.com/nix-community/nix-user-chroot/releases/download/1.2.2/nix-user-chroot-bin-1.2.2-x86_64-unknown-linux-musl
chmod +x ~/bin/nix-user-chroot
# configure nix /w modern commands, flakes, and the flox substituter cache
mkdir -p ~/.config/nix/
echo "
experimental-features = nix-command flakes
extra-trusted-substituters = https://cache.flox.dev
extra-trusted-public-keys = flox-cache-public-1:7F4OyH7ZCnFhcze3fJdfyXYLQw/aV7GEed86nQ7IsOs=
" > ~/.config/nix/nix.conf
# install single-user nix in the chroot
mkdir -m 0755 -p "${NIX_DIR}"
~/bin/nix-user-chroot "${NIX_DIR}" \
bash -c "curl -L https://nixos.org/nix/install | bash"
# check if we've already modified the bashrc
if ! grep -q nix-user-chroot ~/.bashrc; then
cp ~/.bashrc ~/.bashrc.before-nix-chroot
# configure the shell for nix and flox
echo "
# if there's no /nix, re-exec into the nix chroot
if [ ! -d /nix ]; then
exec ~/bin/nix-user-chroot \"${NIX_DIR}\" \\
\$(strings /proc/\$\$/cmdline)
else
. ~/.nix-profile/etc/profile.d/nix.sh
fi
# if flox is present, try to activate ~ and \$PWD
if command -v flox >/dev/null; then
[ -d ~/.flox ] \\
&& eval \"\$(flox activate -m run -d ~)\"
[ "\$PWD" != "\$HOME" ] \\
&& [ -d .flox ] \\
&& eval \"\$(flox activate -m dev)\"
fi
" >> ~/.bashrc
fi
# install flox in the user's default nix profile
# this should cache-hit from the substituter
~/bin/nix-user-chroot "${NIX_DIR}" \
bash -lc "
nix profile install \
--accept-flake-config \
github:flox/flox
flox init -d ~
"
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment