Skip to content

Instantly share code, notes, and snippets.

@alexhrescale
Last active November 20, 2017 05:57
Show Gist options
  • Save alexhrescale/cf3560a6a3434a4b5a0da784c9b6e7a6 to your computer and use it in GitHub Desktop.
Save alexhrescale/cf3560a6a3434a4b5a0da784c9b6e7a6 to your computer and use it in GitHub Desktop.
Bootstrap nix-env for a non-root user; based on https://github.com/pjotrp/nix-no-root
#! /bin/bash
#
# This script creates a working nix in $HOME
# ref https://github.com/pjotrp/nix-no-root
if [ $# -ne 1 ]; then
echo NEED NIX ROOT
exit 1
fi
set -e
NIX_BASE=$1
mkdir -p $NIX_BASE
export nix=$NIX_BASE
export PATH=$NIX_BASE/bin:$PATH
export PKG_CONFIG_PATH=$NIX_BASE/lib/pkgconfig:$PKG_CONFIG_PATH
export LDFLAGS="-L$NIX_BASE/lib $LDFLAGS"
export CPPFLAGS="-I$NIX_BASE/include $CPPFLAGS"
export PERL5OPT="-I$NIX_BASE/lib/perl -I$NIX_BASE/lib64/perl5 -I$NIX_BASE/lib/perl5 -I$NIX_BASE/lib/perl5/site_perl"
cd $NIX_BASE
apt-get update -y
apt-get install -y sudo git vim tmux htop wget curl xz-utils libbz2-dev
apt-get install -y make gcc g++ pkg-config
apt-get install -y patch liblzma-dev libseccomp-dev
apt-get install -y libssl-dev libcurl4-openssl-dev libsqlite3-dev
apt-get install -y cpanminus
### DBI
cpanm install DBI
cpanm install DBD::SQLite
# you need WWW-Curl-4.17 from CPAN
cpanm install WWW::Curl
# but it may not install easily
# apt-get install -y libwww-curl-perl
if [ ! -e $nix/nix-1.11.15.tar.xz ] ; then
pushd $nix
wget http://nixos.org/releases/nix/nix-1.11.15/nix-1.11.15.tar.xz
popd
fi
if [ ! -e $nix/bin/nix-env ] ; then
cd $nix
tar xf nix-*xz
cd nix-1.11.15
./configure --prefix=$NIX_BASE --with-store-dir=$NIX_BASE/store --localstatedir=$NIX_BASE/var
make
make install
fi
$nix/bin/nix-env --version
[ $? -ne 0 ] && exit 1
echo "Success. To proceed you may want to set"
echo 'export NIX_BASE'=$NIX_BASE
echo 'export PATH=$NIX_BASE/bin:$PATH'
echo 'export PKG_CONFIG_PATH=$NIX_BASE/lib/pkgconfig:$PKG_CONFIG_PATH'
echo 'export LDFLAGS="-L$NIX_BASE/lib $LDFLAGS"'
echo 'export CPPFLAGS="-I$NIX_BASE/include $CPPFLAGS"'
echo 'export PERL5OPT="-I$NIX_BASE/lib/perl -I$NIX_BASE/lib64/perl5 -I$NIX_BASE/lib/perl5 -I$NIX_BASE/lib/perl5/site_perl"'
# may need to run
# SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt nix-channel --update
# to update channel after activate
# to fix nixbld warning
useradd nixbuilder
groupadd nixbld
usermod -a nixbuilder -G nixbld
chown -R nixbuilder:nixbld $NIX_BASE
# su nixbuilder
# hackfix
# during compilation you will probably get an error
# error: opening lock file ‘/nix/var/nix/profiles/default.lock’: No such file or directory
mkdir -p /nix/var/nix/profiles # workaround
touch /nix/var/nix/profiles/default.lock # workaround
# errors if .nixpkgs is not present:
# error: file ‘nixpkgs’ was not found in the Nix search path (add it using $NIX_PATH or -I), at (string):1:13
# error: opening file ‘/root/.nixpkgs/default.nix’: No such file or directory
export NIX_PATH=$HOME/.nixpkgs:nixpkgs=$HOME/.nixpkgs
git clone https://github.com/nixos/nixpkgs.git $HOME/.nixpkgs
# then you need a config.nix
# then edit $HOME/.nixpkgs/config.nix
# pkgs:
# {
# packageOverrides = self: {
# nix = self.nix.override {
# storeDir = "/$NIX_BASE/store";
# stateDir = "/$NIX_BASE/var";
# };
# };
# }
# where $NIX_BASE is the one used in this script
# test
nix-env -i hello
# nix-shell -p hello
# also see follow https://github.com/deepfire/nixos-wiki/blob/master/How%20to%20install%20nix%20in%20home%20(on%20another%20distribution).page
# nix-channel --add http://nixos.org/channels/nixpkgs-unstable
# possibly want something like this for containers
# cp -R ~/.nix-profile $NIX_BASE/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment