Skip to content

Instantly share code, notes, and snippets.

@tobiasBora
Created March 29, 2018 01:50
Show Gist options
  • Save tobiasBora/d1d52bcfb53dcabfa26ecbff5f51f6f2 to your computer and use it in GitHub Desktop.
Save tobiasBora/d1d52bcfb53dcabfa26ecbff5f51f6f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# Test it
# docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --privileged -it debian /bin/bash
# apt update
# apt install git wget nano
# adduser me
# su me
# cd
# echo "" > dady.sh
# chmod +x dady.sh
# nano dady.sh
# ./dady.sh install
set -eu
# This script is compiling nix from scratch
ARCH="x86_64"
# Write NIX as an asbolute path with no trailing slash at the end
NIX="${HOME}/nix"
# Write PREFIX as an asbolute path. It will contain a /lib and a /bin inside.
PREFIX="${HOME}/local"
# Arch
ARCH_ALPINE="x86_64"
ARCH_PROOT="x86_64"
# Nix Version
# NIX_VER="1.11.15"
# Alpine version
#ALPINE_VER="3.6.2"
ALPINE_VER="3.7.0"
ALPINE_SHORT_VER=$(echo "${ALPINE_VER}" | grep -o "^[0-9]*\\.[0-9]*")
# Get the script dir
SCRIPT_FILE=$(readlink -f "$0")
SCRIPT_PATH=$(dirname "$SCRIPT_FILE")
PROOT="${PREFIX}/bin/proot"
ALPINE_URL="http://dl-cdn.alpinelinux.org/alpine/v${ALPINE_SHORT_VER}/releases/${ARCH_ALPINE}/alpine-minirootfs-${ALPINE_VER}-${ARCH_ALPINE}.tar.gz"
install_proot() {
echo "Make sure proot is installed..."
# Make sure proot is installed
if [ ! -f "${PROOT}" ]
then
echo "Installing proot..."
PROOT_DIR=$(dirname "${PROOT}")
mkdir -p "${PROOT_DIR}"
wget "https://github.com/proot-me/proot-static-build/raw/master/static/proot-${ARCH_PROOT}" -O "${PROOT}"
chmod +x "${PROOT}"
echo "Proot installed."
fi
}
begin_install() {
cd /tmp
rm -rf daddy_I_want_nix/
mkdir daddy_I_want_nix/
cd daddy_I_want_nix/
# Download the alpine distribution
wget "${ALPINE_URL}"
tar xvzf alpine-minirootfs*.tar.gz || true
mkdir -p "tmp"
cp "$SCRIPT_FILE" "./tmp/daddy_I_want_nix.sh"
chmod +x "./tmp/daddy_I_want_nix.sh"
pwd=$(pwd)
echo "PWD = ${pwd}"
# "$PROOT" -0 -r . -b /proc -b /dev -b /tmp -b /etc/resolv.conf
PROOT_NO_SECCOMP=1 "$PROOT" -0 -r "${pwd}" -b /proc -b /dev -b /etc/resolv.conf /tmp/daddy_I_want_nix.sh inside_proota
echo "First part finished !"
# # I don't know why, but the tar fails in the chroot
# # https://github.com/proot-me/PRoot/issues/129
# mkdir -p /tmp/daddy_I_want_nix/tmp/
# cd /tmp/daddy_I_want_nix/tmp/
# echo "Downloading nix sources..."
# wget "https://nixos.org/releases/nix/nix-${NIX_VER}/nix-${NIX_VER}.tar.xz"
# echo "Download finished ! Tar extraction..."
# tar -xvJf "nix-${NIX_VER}.tar.xz"
# echo "Extraction finished."
PROOT_NO_SECCOMP=1 "$PROOT" -0 -r "${pwd}" -b /proc -b /dev -b /etc/resolv.conf /bin/bash -c "/tmp/daddy_I_want_nix.sh inside_prootb"
finish_install
}
inside_proota() {
echo "########## I'm inside the proot chroot ! ##########"
echo "Let's install some stuff..."
echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
PATH=/bin:/sbin:/usr/bin:/usr/sbin
apk update || true
apk upgrade || true
echo "You may have the error \"trigger: script exited with error 127\""
echo "don't pay attention."
apk add bash openssl git make gcc g++ sqlite autoconf automake pkgconfig libbz2 bzip2 gc xz xz-dev openssl-dev libxml2 libxml2-dev libxslt libxslt-dev bison flex ca-certificates bzip2-dev sqlite-libs sqlite-dev libcurl curl-dev libseccomp-dev perl perl-dev perl-dbi perl-dbd-sqlite perl-www-curl curl linux-headers boost-dev || true
rm -rf /tmp/build_nix/
mkdir /tmp/build_nix/
update-ca-certificates
cd /tmp/
git clone https://github.com/NixOS/nix
}
inside_prootb () {
PATH=/bin:/sbin:/usr/bin:/usr/sbin
cd /tmp/nix*
# Fix on NFS file system
# https://github.com/NixOS/nixpkgs/issues/29778
sed -i 's#(eaName == "security.selinux")#(eaName == "security.selinux" || eaName == "system.nfs4_acl")#g' src/libstore/local-store.cc
./bootstrap.sh
# https://github.com/NixOS/nix/issues/506
./configure --disable-doc-gen LDFLAGS="-static" --prefix="${PREFIX}" --with-store-dir="${NIX}/store" --localstatedir="${NIX}/var"
make
make install
mkdir -p "${PREFIX}/lib"
cp -Lr /lib/* "${PREFIX}/lib"
cp -Lr /usr/lib/* "${PREFIX}/lib"
echo "Congratulation, nix has been successfully compiled in the chroot."
echo "Now, we need to copy it in your user folder."
}
get_bash_into_alpine () {
cd /tmp/daddy_I_want_nix/
PATH=/bin:/sbin:/usr/bin:/usr/sbin
PROOT_NO_SECCOMP=1 "$PROOT" -0 -r "$(pwd)" -b /proc -b /dev -b /etc/resolv.conf /bin/bash
}
finish_install() {
echo "Installing in the user home..."
mkdir -p "${PREFIX}"
cp -r "/tmp/daddy_I_want_nix${PREFIX}" "${PREFIX}"
echo "Done ! Now, be sure to add to your path ${PREFIX},"
echo "by adding this line in a script that will be loaded"
echo "at startup, or in your .profile/.bashrc if you use"
echo "command line only:"
echo "export PATH=${PREFIX}/bin:\${PATH}"
echo "Now you can enjoy nix ! To install git, for example,"
echo "type this really ugly formula:"
echo "LD_LIBRARY_PATH=${PREFIX}/lib ${PREFIX}/lib/ld-musl-x86_64.so.1 nix-env"
export PATH=${PREFIX}/bin:${PATH}
}
uninstall() {
rm -rf "${NIX}"
rm -rf "${PREFIX}/nix*"
}
usage() {
echo "Usage: ./daddy_I_want_nix.sh [install | uninstall]"
exit 1
}
if [ "$#" -eq 0 ]
then
usage
fi
case "$1" in
"uninstall" )
uninstall
;;
"install" )
install_proot
begin_install
;;
"inside_proota" )
inside_proota
;;
"inside_prootb" )
inside_prootb
;;
"get_bash_alpine" )
get_bash_into_alpine
;;
* )
usage
;;
esac
# ./dady.sh get_bash_alpine
# cd /tmp
# git clone --depth=1 https://github.com/NixOS/nixpkgs.git
# export NIX_PATH=nixpkgs=/tmp/nixpkgs/
# adduser me # deal with pwd...
# su me
# X=leo NIX_REMOTE=local?root=/home/me/rootfs/ NIX_CONF_DIR=/home/$X/etc/nix NIX_LOG_DIR=/home/$X/nix/var/log/nix NIX_STORE=/home/$X/nix/store NIX_STATE_DIR=/home/$X/nix/var ./nix-build -E 'with import <nixpkgs> {}; nix.override { storeDir = "/home/'$X'/nix/store";stateDir = "/home/'$X'/nix/var"; confDir = "/home/'$X'/etc"; }'
# mkdir /home/leo/
# X=leo NIX_CONF_DIR=/home/$X/etc/nix NIX_LOG_DIR=/home/$X/nix/var/log/nix NIX_STORE=/home/$X/nix/store NIX_STATE_DIR=/home/$X/nix/var ./nix-build -E 'with import <nixpkgs> {}; nix.override { storeDir = "/home/'$X'/nix/store";stateDir = "/home/'$X'/nix/var"; confDir = "/home/'$X'/etc"; }'
# X=leo NIX_CONF_DIR=/home/$X/etc/nix NIX_LOG_DIR=/home/$X/nix/var/log/nix NIX_STORE=/home/$X/nix/store NIX_STATE_DIR=/home/$X/nix/var nix-build -E 'with import <nixpkgs> {}; nix.override { storeDir = "/home/'$X'/nix/store";stateDir = "/home/'$X'/nix/var"; confDir = "/home/'$X'/etc"; }'
# Méthode rapide avec installeur:
# proot -b /home/testproot/nix_proot/:/nix /bin/bash
# curl https://nixos.org/nix/install | sh
# . /home/testproot/.nix-profile/etc/profile.d/nix.sh
# X=testproot NIX_CONF_DIR=/home/$X/etc/nix NIX_LOG_DIR=/home/$X/nix/var/log/nix NIX_STORE=/home/$X/nix/store NIX_STATE_DIR=/home/$X/nix/var nix-build -E 'with import <nixpkgs> {}; nix.override { storeDir = "/home/'$X'/nix/store";stateDir = "/home/'$X'/nix/var"; confDir = "/home/'$X'/etc"; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment