nix: update scripts to develop state

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2024-04-25 16:57:48 +02:00
parent 820aad8bb7
commit 5426463b04
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
12 changed files with 173 additions and 38 deletions

View File

@ -25,7 +25,7 @@ cleanup() {
# If you want to clean after every build set _NIX_CLEAN=true
if [[ -n "${_NIX_CLEAN}" ]]; then
trap cleanup EXIT ERR INT QUIT
trap cleanup EXIT ERR INT QUIT
fi
# build output will end up under /nix, we have to extract it
@ -44,6 +44,12 @@ if [[ -z "${TARGET}" ]]; then
exit 1
fi
# Hack fix for missing Android SDK for aarch64 on Darwin. See systemOverride in `nix/pkgs.nix`.
if [[ "${TARGET}" =~ ^(targets.status-go.mobile.android|targets.mobile.android.build)$ ]]; then
os=$(uname -s | tr '[:upper:]' '[:lower:]')
export NIXPKGS_SYSTEM_OVERRIDE="x86_64-${os}"
fi
# Some defaults flags, --pure could be optional in the future.
# NOTE: The --keep-failed flag can be used for debugging issues.
nixOpts=(

View File

@ -89,7 +89,7 @@ if [[ -n "${nixResultPath}" ]]; then
toDelete=$(findByResult "${nixResultPath}")
else
# use regular expression that should match all status-mobile build artifacts
toDelete=$(findByRegex '.*-status-(react|go)-(shell|source|build|patched-npm-gradle-modules).*')
toDelete=$(findByRegex '.*-status-(mobile|go)-(shell|source|build|patched-npm-gradle-modules).*')
fi
# remove duplicates and return

View File

@ -2,12 +2,12 @@
set -Ee
_NIX_GCROOTS="${_NIX_GCROOTS:-/nix/var/nix/gcroots/per-user/${USER}/status-mobile}"
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
source "${GIT_ROOT}/nix/scripts/source.sh"
source "${GIT_ROOT}/scripts/colors.sh"
_NIX_GCROOTS="${_NIX_GCROOTS:-${GIT_ROOT}/.nix-gcroots}"
TARGET="${1}"
shift
if [[ -z "${TARGET}" ]]; then

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -Eeuo pipefail
# script to prompt user for terminating a process
pid="${1}"
read -p "Do you want to terminate this process? (y/n): " choice
if [[ "${choice}" == "y" ]]; then
sudo kill "${pid}"
echo "Process ${pid} terminated."
else
echo "Process not terminated. Please close it manually and retry."
exit 1
fi

View File

@ -1,4 +1,6 @@
#!/usr/bin/env bash
set -eo pipefail
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
# Checking group ownership to identify installation type.
file_group() {
@ -6,7 +8,8 @@ file_group() {
if [[ "${UNAME}" == "Linux" ]]; then
stat -Lc "%G" "${1}" 2>/dev/null
elif [[ "${UNAME}" == "Darwin" ]]; then
stat -Lf "%Sg" "${1}" 2>/dev/null
# Avoid using Nix GNU stat when in Nix shell.
/usr/bin/stat -Lf "%Sg" "${1}" 2>/dev/null
fi
}
@ -24,6 +27,7 @@ nix_install_type() {
if [[ "$(os_name)" =~ NixOS ]]; then
echo "nixos"
else
USER=$(id -un) # Missing in Docker.
case "${NIX_STORE_DIR_GROUP}" in
"nixbld") echo "multi";;
"30000") echo "multi";;
@ -48,3 +52,41 @@ nix_root() {
fi
echo "${NIX_ROOT}"
}
nix_current_version() {
nix-env --version | awk '{print $3}'
}
nix_get_local_setting() {
local NIX_LOCAL_CONFIG="${GIT_ROOT}/nix/nix.conf"
local KEY="${1}"
awk -F' = ' "/^${KEY} *=/{print \$2}" nix/nix.conf
}
nix_set_global_setting() {
local NIX_GLOBAL_CONFIG="/etc/nix/nix.conf"
local KEY="${1}"
local VAL="${2}"
if grep "${KEY}" "${NIX_GLOBAL_CONFIG}" 2>/dev/null; then
sed -i "s/${KEY} = \(.*\)$/${KEY} = ${VAL}/" "${NIX_GLOBAL_CONFIG}"
else
echo "${KEY} = ${VAL}" | sudo tee -a "${NIX_GLOBAL_CONFIG}" >/dev/null
fi
}
nix_daemon_restart() {
# Restarting Nix Daemon makes sense only on a multi-user install.
[[ $(nix_install_type) != "multi" ]] && return
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "Restarting Nix daemon Launchd service..." >&2
sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
sudo launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist
elif [[ "$(uname -s)" == "Linux" ]] && [[ "$(nix_install_type)" == "multi" ]]; then
echo "Restarting Nix daemon Systemd service..." >&2
sudo systemctl daemon-reload
sudo systemctl restart nix-daemon
else
echo "Unknown platform! Unable to restart daemon!" >&2
exit 1
fi
}

View File

@ -45,6 +45,8 @@ copyNodeModules() {
# WARNING: We can't de-reference .bin symlinks
cp -Rf ${src}/node_modules/.bin/. "${tmp}/.bin/"
# Cmake called by react-native-reanimated 3.3.0 during build creates read-only temporary files.
chmod -R u+w node_modules/react-native-reanimated/android/ -f || true
rm -r "${dst}"
mv -f "${tmp}" "${dst}"
}
@ -58,9 +60,16 @@ findFilesNewerThan() {
-newer "${sentinel}" \
-not -ipath "*/*android/build/*" -prune \
-not -ipath "*/xcuserdata/*" -prune \
-not -ipath "*/unpacked_bin/clj-kondo" \
-not -ipath "*/scripts/.packager.env" \
-print
\ # generated at runtime by react-native \
\ # related code https://github.com/facebook/react-native/blob/v0.72.5/packages/react-native/ReactCommon/React-rncore.podspec#L19 \
-not -path "*/node_modules/react-native/ReactCommon/react/renderer/components/rncore/*" \
\ # generated at runtime by react-native-config \
\ # related code https://github.com/lugg/react-native-config/blob/v1.5.0/react-native-config.podspec#L52 \
-not -path "*/node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m" \
\ # generated at runtime by react-native from v0.73.x onwards \
-not -path "*/node_modules/react-native/React/Fabric/*" \
-print
}
nodeModulesUnchanged() {
@ -77,7 +86,7 @@ nodeModulesUnchanged() {
# Sentinel file holds location of the node_modules source in Nix store
currentNixSrc="$(cat "${sentinelFile}")"
if [ "${currentNixSrc}" != "${src}" ]; then
echo -e "${YLW}Yarn modules changed, copying new version over${RST}" >&2
return 1
@ -105,7 +114,7 @@ replaceNodeModules() {
if nodeModulesUnchanged "${src}" "${dst}"; then
return
fi
fi
# Replace node_modules if necessary
echo "Copying node_modules from Nix store:" >&2

View File

@ -22,12 +22,10 @@ nix_purge_linux_multi_user_users() {
}
nix_purge_darwin_multi_user_service() {
cd /Library/LaunchDaemons
NIX_SERVICES=(org.nixos.darwin-store.plist org.nixos.nix-daemon.plist)
for NIX_SERVICE in "${NIX_SERVICES[@]}"; do
sudo launchctl unload "${NIX_SERVICE}"
sudo launchctl remove "${NIX_SERVICE}"
done
sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist
sudo launchctl unload /Library/LaunchDaemons/org.nixos.darwin-store.plist
sudo rm /Library/LaunchDaemons/org.nixos.darwin-store.plist
}
nix_purge_darwin_multi_user_users() {
@ -41,7 +39,23 @@ nix_purge_darwin_multi_user_users() {
nix_purge_darwin_multi_user_volumes() {
sudo sed -i.bkp '/nix/d' /etc/synthetic.conf
sudo sed -i.bkp '/nix/d' /etc/fstab
sudo diskutil apfs deleteVolume /nix
# Attempt to delete the volume
if ! sudo diskutil apfs deleteVolume /nix; then
echo "Failed to unmount /nix because it is in use."
# Identify the process using the volume
local pid=$(lsof +D /nix | awk 'NR==2{print $2}')
if [[ -n "$pid" ]]; then
echo "The volume /nix is in use by process ID $pid."
"${GIT_ROOT}/nix/scripts/kill_proc_prompt.sh" "${pid}" || return 1
else
echo "No process found using /nix. Manual intervention required."
return 1
fi
fi
echo -e "${YLW}You will need to reboot your system!${RST}" >&2
}
@ -93,7 +107,7 @@ if (return 0 2>/dev/null); then
return
fi
# Confirm user decission, unless --force is used.
# Confirm user decision, unless --force is used.
if [[ "${1}" != "--force" ]]; then
echo -e "${YLW}Are you sure you want to purge Nix?${RST}" >&2
read -p "[y/n]: " -n 1 -r
@ -105,7 +119,7 @@ if [[ "${1}" != "--force" ]]; then
fi
NIX_INSTALL_TYPE=$(nix_install_type)
# Purging /nix on NixOS would be disasterous.
# Purging /nix on NixOS would be disastrous.
if [[ "${NIX_INSTALL_TYPE}" == "nixos" ]]; then
echo -e "${RED}You should not purge Nix files on NixOS!${RST}" >&2
exit

View File

@ -3,15 +3,11 @@
set -eo pipefail
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
source "${GIT_ROOT}/nix/scripts/lib.sh"
source "${GIT_ROOT}/scripts/colors.sh"
source "${GIT_ROOT}/nix/scripts/lib.sh"
source "${GIT_ROOT}/nix/scripts/version.sh"
NIX_VERSION="2.8.0"
NIX_INSTALL_URL="https://nixos.org/releases/nix/nix-${NIX_VERSION}/install"
NIX_INSTALL_SHA256="f43bfedfca9151479462d8bc11f238615a2d52f19b14894d8e2b59980e75ef72"
NIX_INSTALL_PATH="/tmp/nix-install-${NIX_VERSION}"
install_nix() {
nix_install() {
# Download installer and verify SHA256>
curl -sSf "${NIX_INSTALL_URL}" -o "${NIX_INSTALL_PATH}"
echo "${NIX_INSTALL_SHA256} ${NIX_INSTALL_PATH}" | sha256sum -c
@ -42,6 +38,22 @@ install_nix() {
echo "Please see: https://nixos.org/nix/manual/#chap-installation" >&2
exit 1
fi
# Additional fixes
nix_add_extra_cache
nix_daemon_restart
}
# Adding directly to global config to avoid warnings like this:
# "ignoring untrusted substituter 'https://nix-cache.status.im/', you are not a trusted user."
nix_add_extra_cache() {
# Single-user installations do not have this issue.
[[ ! -f /etc/nix/nix.conf ]] && return
echo -e 'Adding our cache to Nix daemon config...' >&2
local NIX_SETTINGS=('substituters' 'trusted-substituters' 'trusted-public-keys')
for NIX_SETTING in "${NIX_SETTINGS[@]}"; do
nix_set_global_setting "${NIX_SETTING}" "$(nix_get_local_setting "${NIX_SETTING}")"
done
}
if [[ ! -x "$(command -v sha256sum)" ]]; then
@ -72,5 +84,5 @@ fi
# If none of the checks before succeeded we need to install Nix
echo -e "${GRN}Setting up Nix package manager...${RST}"
install_nix
nix_install
echo -e "${YLW}See STARTING_GUIDE.md if you're new here.${RST}"

View File

@ -1,6 +1,5 @@
#!/usr/bin/env bash
#
# This script is used by the Makefile to have an implicit nix-shell.
# The following environment variables modify the script behavior:
# - TARGET: This attribute is passed via --attr to Nix, defining the scope.
@ -42,16 +41,6 @@ nixArgs=(
"--attr shells.${TARGET}"
)
config=''
if [[ -n "${STATUS_GO_SRC_OVERRIDE}" ]]; then
config+="status-im.status-go.src-override=\"${STATUS_GO_SRC_OVERRIDE}\";"
fi
config+="status-im.build-type=\"${BUILD_TYPE}\";"
if [[ -n "$config" ]]; then
nixArgs+=("--arg config {$config}")
fi
# This variable allows specifying which env vars to keep for Nix pure shell
# The separator is a colon
if [[ -n "${_NIX_KEEP}" ]]; then
@ -64,6 +53,12 @@ if [[ -n "${_NIX_PURE}" ]]; then
pureDesc='pure '
fi
# Hack fix for missing Android SDK for aarch64 on Darwin. See systemOverride in `nix/pkgs.nix`.
if [[ "${TARGET}" =~ ^(android-sdk|android|gradle|keytool|status-go)$ ]]; then
os=$(uname -s | tr '[:upper:]' '[:lower:]')
export NIXPKGS_SYSTEM_OVERRIDE="x86_64-${os}"
fi
echo -e "${GRN}Configuring ${pureDesc}Nix shell for target '${TARGET}'...${RST}" 1>&2
# Save derivation from being garbage collected

View File

@ -12,7 +12,7 @@ source_nix_profile() {
source "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
elif [[ "${NIX_INSTALL_TYPE}" == "single" ]]; then
source "${HOME}/.nix-profile/etc/profile.d/nix.sh"
elif [[ "${NIX_INSTALL_TYPE}" == "nixops" ]]; then
elif [[ "${NIX_INSTALL_TYPE}" == "nixos" ]]; then
echo "Sourcing profile not necessary on NixOS!" >&2
fi
}

37
nix/scripts/upgrade.sh Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
# This script upgrades Nix to specific version.
# https://nixos.org/manual/nix/stable/installation/upgrading.html
set -eo pipefail
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
source "${GIT_ROOT}/scripts/colors.sh"
source "${GIT_ROOT}/nix/scripts/lib.sh"
source "${GIT_ROOT}/nix/scripts/source.sh"
source "${GIT_ROOT}/nix/scripts/version.sh"
nix_upgrade() {
echo -e "Upgrading Nix interpreter to: ${GRN}${NIX_VERSION}${RST}" >&2
nix-channel --update
nix-env --install --attr "nixpkgs.${NIX_PACKAGE}" "nixpkgs.cacert"
nix_daemon_restart
}
# Allow for sourcing the script
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
return
fi
if [[ "$(nix_current_version)" == "${NIX_VERSION}" ]]; then
echo -e "Nix interpreter already on version: ${GRN}${NIX_VERSION}${RST}"
exit 0
fi
NIX_INSTALL_TYPE=$(nix_install_type)
if [[ "${NIX_INSTALL_TYPE}" == "nixos" ]]; then
echo -e "${YLW}WARNING:${RST} Upgrade Nix in your NixOS configuration!" >&2
exit 0
elif [[ "${NIX_INSTALL_TYPE}" == "single" ]]; then
nix_upgrade
elif [[ "${NIX_INSTALL_TYPE}" == "multi" ]]; then
sudo -i bash -c "source ${PWD}/${0}; nix_upgrade"
fi

6
nix/scripts/version.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
export NIX_VERSION="2.19.3"
export NIX_PACKAGE="nixVersions.nix_2_19"
export NIX_INSTALL_URL="https://nixos.org/releases/nix/nix-${NIX_VERSION}/install"
export NIX_INSTALL_SHA256="73d47b0ab783fddca1b2d44a03d52a74c97c28a1fc8ff5a29419079302ef9c3d"
export NIX_INSTALL_PATH="/tmp/nix-install-${NIX_VERSION}"