Skip to content

Instantly share code, notes, and snippets.

@samunders-core
Last active May 31, 2019 20:58
Show Gist options
  • Save samunders-core/21faa8938ff40ba9aca7e45210c00fbc to your computer and use it in GitHub Desktop.
Save samunders-core/21faa8938ff40ba9aca7e45210c00fbc to your computer and use it in GitHub Desktop.
Gentoo-debugged nix for non-root user
#!/bin/bash -e
# inspiration: https://gitlab.com/veprbl/nix-bootstrap/blob/master/bootstrap.sh
BOOST=1.68.0 && BOOST="https://dl.bintray.com/boostorg/release/$BOOST/source/boost_${BOOST//./_}.tar.bz2"
BROTLI=1.0.7 && BROTLI="https://github.com/google/brotli/archive/v$BROTLI.tar.gz"
EDITLINE=1.16.0 && EDITLINE="https://github.com/troglobit/editline/releases/download/$EDITLINE/editline-$EDITLINE.tar.xz"
NIX=nix-2.2.2 && NIX="https://nixos.org/releases/nix/$NIX/$NIX.tar.xz"
NIXPKGS=19.03 && NIXPKGS="https://github.com/NixOS/nixpkgs/archive/$NIXPKGS.tar.gz"
SQLITE=sqlite-autoconf-3280000 && SQLITE="https://sqlite.org/2019/$SQLITE.tar.gz"
DEST="$(readlink -f $HOME)/.nix" # path to nix store must not contain symlinks
JOBS=$((1 + `grep -c 'core id' /proc/cpuinfo`))
TMPDIR="$DEST/$(basename $0)-boot-$(whoami)"
export TMPDIR
function dir_of() {
URL="$1"
FILE="$(basename $URL)" && [ -f "$FILE" ] || wget "$URL"
[ -n "$2" ] && DIR="$2" || DIR="$(basename ${URL%.*.*})"
( [ -d "$DIR" ] || tarx "$FILE" ) && pushd "$DIR"
}
function grepo() {
PATTERN="${1//\\d/[0-9]}" && shift
echo "$@" | egrep -oe "$PATTERN"
# echo "$@" | sed -re "s/^.*[^0-9]($PATTERN)[^0-9].*"'$/\1/'
}
function log() {
echo "$@" 1>&2
"$@"
}
function maki() {
log "$@" "--prefix=$TMPDIR"
log make V=1 -j $JOBS 1>&2
log make install
popd
}
function tarx() {
FILE="${1%2}"
log tar -x `man tar | grep -B 1 "Filter the archive through ${FILE##*.}" | egrep -oe '--\w+' | head -1` -f "$1"
}
(
#rm -rf "$TMPDIR"
mkdir -p "$TMPDIR" && pushd "$TMPDIR"
dir_of "$BOOST" && ./bootstrap.sh --with-libraries=context "--prefix=$TMPDIR" && ./b2 --ignore-site-config install && popd
dir_of "$BROTLI" "brotli-$(grepo '\d+.\d+.\d+' $BROTLI)" && maki ./configure-cmake
for URL in "$SQLITE" "$EDITLINE"; do
dir_of "$URL" && maki ./configure
done
dir_of "$NIX"
DEPS="-lboost_context -lbrotlicommon -lbrotlienc -lbrotlidec -lsqlite3"
sed -i -re "/nix_LDFLAGS.*${DEPS// /.*}/q" -e "s/(nix_LDFLAGS\b.*)/\1 $DEPS/" src/nix/local.mk
PKG_CONFIG_PATH="$(find $TMPDIR -mindepth 1 -maxdepth 1 -type d -name '*[0-9]*[0-9]*[0-9]*' | tr '\n' ':')$TMPDIR/lib/pkgconfig"
export PKG_CONFIG_PATH
maki ./configure --disable-seccomp-sandboxing --disable-shared "--with-store-dir=$DEST/nix/store" "--localstatedir=$DEST/nix/var"
[ -d ~/nixpkgs ] || {
which git 1> /dev/null && git clone -b "release-$(grepo '\d+.\d+' $NIXPKGS)" https://github.com/NixOS/nixpkgs.git ~/nixpkgs || ( dir_of "$NIXPKGS" && DIR="$(pwd)" && popd 1> /dev/null && mv "$DIR" ~/nixpkgs )
}
) 1> /dev/null
[ -L ~/.nix-profile ] || ln -s "$DEST/nix/var/nix/profiles/default" ~/.nix-profile
mkdir -p ~/.nixpkgs
[ -f ~/.nixpkgs/config.nix ] || cat <<EOF > ~/.nixpkgs/config.nix
pkgs:
{
packageOverrides = pkgs: {
boost = pkgs.boost.overrideAttrs (oldAttrs: {
buildPhase = builtins.replaceStrings [ "\n" ] [ " --ignore-site-config" ] "${oldAttrs.buildPhase}";
installPhase = builtins.replaceStrings [ " install\n" ] [ " --ignore-site-config install" ] "${oldAttrs.installPhase}";
});
nix = ( pkgs.nix.override ( {
storeDir = "$DEST/nix/store";
stateDir = "$DEST/nix/var";
} ) ).overrideAttrs (oldAttrs: {
doInstallCheck = false;
});
};
}
EOF
# use nix to bootstrap stdenv and install proper nix
LD_LIBRARY_PATH="$TMPDIR/lib:$LD_LIBRARY_PATH" "$TMPDIR/bin/nix-env" -j $JOBS -iA nix -f ~/nixpkgs --keep-failed
rm -rf "$TMPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment