daml/dev-env/lib/dade-dump-profile
Digital Asset GmbH 05e691f558 open-sourcing daml
2019-04-04 09:33:38 +01:00

131 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Prints out commands to set up profile for both bash and zsh.
#
# Requires:
# - DADE_DEVENV_DIR correctly set, DADE_BASE is accepted for compatibility
# Optional:
# - DADE_VAR_DIR to use separate directory for mutable persisted state.
# Reset locale to ensure no warnings from the nix-build calls below.
# The nix-build Perl script will use a Nix glibc, which comes without
# any locales. We can therefore not make any assumptions about the
# available locales at this point (DEL-2851).
unset LC_ALL
unset LANG
unset NIX_CONF_DIR
unset NIX_PATH
set -Eeuo pipefail
if [[ -n "${DADE_DEBUG+x}" ]]; then
set -x
fi
errcho() {
>&2 echo "[dev-env] $*"
}
check_nix_version() {
# Keep in sync with dade-nix-install.
NIX_MAJOR="$1"
NIX_MINOR="$2"
NIX_PATCH="$3"
local current
current=$(nix-env --version)
if [[ $current =~ ([0-9])[.]([0-9]+)([.]([0-9]+))? ]]; then
local current_major="${BASH_REMATCH[1]}"
local current_minor="${BASH_REMATCH[2]}"
local current_patch="${BASH_REMATCH[4]:-0}"
if [[ $current_major -lt $NIX_MAJOR ]] ||
[[ $current_major -eq $NIX_MAJOR && $current_minor -lt $NIX_MINOR ]] ||
[[ $current_major -eq $NIX_MAJOR && $current_minor -eq $NIX_MINOR && $current_patch -lt $NIX_PATCH ]];
then
errcho "WARNING: Version ${current_major}.${current_minor}.${current_patch} detected, but ${NIX_MAJOR}.${NIX_MINOR}.${NIX_PATCH} or higher required."
errcho "Please run 'nix upgrade-nix' or 'curl https://nixos.org/nix/install | sh' to update Nix."
fi
else
errcho "WARNING: Unexpected output of 'nix-env --version' ($current), dev-env might not work properly. Contact #disc-enterprise-pipe immediately!"
fi
}
# TODO(gleber): Compatibility mode until JBH is ugraded.
DADE_DEVENV_DIR="${DADE_DEVENV_DIR:-${DADE_BASE:-}}"
if [ -z "$DADE_DEVENV_DIR" ]; then
DADE_DEVENV_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
fi
export DADE_DEVENV_DIR="${DADE_DEVENV_DIR}"
export DADE_REPO_ROOT="${DADE_REPO_ROOT:-${DADE_DEVENV_DIR}/..}"
# If used from another repository, DADE_VAR_DIR should already point to a
# mutable `var/` directory to store gc-roots.
DADE_VAR_DIR="${DADE_VAR_DIR:-$DADE_DEVENV_DIR/var}"
DADE_NIXPKGS="${DADE_VAR_DIR}/gc-roots/nixpkgs-snapshot"
# shellcheck source=./ensure-nix
source "$(dirname "${BASH_SOURCE[0]}")/ensure-nix"
# In a sub-shell, source in the nix-profile and preload nixpkgs-snapshot.
(
# shellcheck source=./ensure-nix
source "$(dirname "${BASH_SOURCE[0]}")/ensure-nix"
: "${DADE_DESIRED_NIX_VERSION:=2 1 3}"
check_nix_version $DADE_DESIRED_NIX_VERSION
# If the user has no channels configured, then NIX_PATH might point
# to non-existing location. Add a backup snapshot to use as fallback
# for the bootstrapping.
export NIX_PATH=${NIX_PATH:+$NIX_PATH:}nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-18.03.tar.gz
# Load nixpkgs-snapshot.
hashfile="${DADE_NIXPKGS}.hash"
currentHash="$(nix-hash "$DADE_DEVENV_DIR/../nix/nixpkgs.nix")"
test -e "$hashfile" && hash="$(cat "$hashfile")"
if [[ ! -e "$DADE_NIXPKGS" || "$hash" != "$currentHash" ]]; then
errcho "Loading outdated or missing nixpkgs snapshot..."
outpath="$(
nix-store -Q --realise --indirect --add-root "$DADE_NIXPKGS" \
"$(nix-instantiate -Q --eval "${DADE_DEVENV_DIR}/../nix/nixpkgs.nix" -A path \
| sed 's/^\"//;s/\"$//')"
)"
printf "$currentHash" >| "$hashfile"
errcho "Done loading the nixpkgs snapshot to $outpath"
fi
)
# Make sure nix gets ensured in main shell
echo source "$(dirname "${BASH_SOURCE[0]}")/ensure-nix"
# Expose our tools in /dev-env/bin and our version of nixpkgs
echo "export NIX_CONF_DIR=\"${DADE_DEVENV_DIR}/etc\""
echo "export NIX_PATH=nixpkgs=\"${DADE_NIXPKGS}\""
echo "export PATH=\"${DADE_DEVENV_DIR}/bin:\$PATH\""
echo "export PYTHONPATH=\".\${PYTHONPATH:+:\$PYTHONPATH}\""
echo "export DADE_REPO_ROOT=${DADE_REPO_ROOT}"
echo "export DADE_VAR_DIR=${DADE_VAR_DIR}"
echo "export DADE_DEVENV_DIR=${DADE_DEVENV_DIR}"
# Make sure the locale is UTF-8 capable. Setting LOCALE_ARCHIVE is
# necessary on Ubuntu (but not on CentOS) to make the Nix glibc find
# the en_US.UTF-8 locale (DEL-2851).
if [[ $(uname -v) =~ 'Ubuntu' ]]; then
# If the user has no channels configured, then NIX_PATH might point
# to non-existing location. Add a backup snapshot to use as fallback
# for the bootstrapping.
TMP_NIX_PATH=nixpkgs=${DADE_NIXPKGS}
# since we need to set LC_ALL=C to prevent any messages about missing locale
# until we fetch the locale, we start in a separate shell
(
export LC_ALL=C
unset NIX_PATH
out=$(nix-build --no-out-link -Q "${DADE_DEVENV_DIR}/../nix" -I "${TMP_NIX_PATH}" -A pkgs.glibcLocales)
echo "export LOCALE_ARCHIVE=\"${out}/lib/locale/locale-archive\""
)
fi
echo "export LC_ALL=en_US.UTF-8"
echo "export LANG=en_US.UTF-8"