Skip to content

Instantly share code, notes, and snippets.

@kergoth
Last active October 5, 2022 05:05
Show Gist options
  • Save kergoth/a39060037ce87e207f7c110985e1cdfa to your computer and use it in GitHub Desktop.
Save kergoth/a39060037ce87e207f7c110985e1cdfa to your computer and use it in GitHub Desktop.
#!/bin/bash
proot_url=https://proot.gitlab.io/proot/bin/proot
bindir=${XDG_DATA_HOME:-$HOME/.local/share}/../bin
scriptdir="$(cd "$(dirname "$0")" && pwd -P)"
PATH=$bindir:$PATH
set -euo pipefail
mkdir -p "$bindir"
if ! command -v proot >/dev/null 2>&1; then
wget -O "$bindir/proot" "$proot_url"
chmod +x "$bindir/proot"
fi
if ! [ -e ~/.config/nix/nix.conf ]; then
mkdir -p ~/.config/nix
cat >~/.config/nix/nix.conf <<END
experimental-features = nix-command flakes
sandbox = false
END
fi
unset NIX_PATH
mkdir -p ~/.nix
proot -b "$HOME/.nix:/nix" bash -c "curl -L https://nixos.org/nix/install | sh"
{
echo "#!/usr/bin/env bash"
echo
echo "proot -b \"\$HOME/.nix:/nix\" bash -c 'for i in ~/.nix-profile/etc/profile.d/*.sh; do if [ -e "\$i" ]; then . "\$i"; fi; done && \"\$@\"' - \"\$@\""
} >"$bindir/nixrun"
chmod +x "$bindir/nixrun"
cp -av "$scriptdir/nixwrap" "$bindir/nixwrap"
"$bindir/nixwrap"
#!/bin/bash
set -euo pipefail
scriptdir="$(cd "$(dirname "$0")" && pwd -P)"
PATH="$scriptdir:$PATH"
bins="$(mktemp -t "${0##*/}.XXXXXX")"
trap 'rm -f "$bins"' EXIT INT TERM
shims="$(mktemp -t "${0##*/}.XXXXXX")"
trap 'rm -f "$bins" "$shims"' EXIT INT TERM
if [ -L ~/.nix-profile ]; then
nixrun find ~/.nix-profile/bin -type l -printf '%f\n' 2>/dev/null | sort >>"$bins" || :
fi
if [ -e ~/.nix/shims ]; then
find ~/.nix/shims -type l -printf '%f\n' | sort >>"$shims" || :
fi
mkdir -p ~/.nix/shims
if ! [ -f ~/.nix/shims/nixrun-wrap ]; then
rm -f ~/.nix/shims/nixrun-wrap
cat >~/.nix/shims/nixrun-wrap <<END
#!/bin/sh
nixrun "\$(basename "\$0")" "\$@"
END
chmod +x ~/.nix/shims/nixrun-wrap
fi
comm -13 "$bins" "$shims" | sed -e "s#^#$HOME/.nix/shims/#" | tr '\n' '\0' | xargs -0 rm -fv
comm -23 "$bins" "$shims" | tr '\n' '\0' | xargs -0 -I "{}" ln -sfv ./nixrun-wrap ~/.nix/shims/"{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment