-
Notifications
You must be signed in to change notification settings - Fork 11
/
nix-quick-install.sh
executable file
·116 lines (101 loc) · 3.22 KB
/
nix-quick-install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
set -eu
set -o pipefail
source "${BASH_SOURCE[0]%/*}/vercomp.sh"
# Figure out system type (TODO don't assume x86_64)
case "$OSTYPE" in
darwin*)
sys="x86_64-darwin"
;;
linux*)
sys="x86_64-linux"
;;
*)
echo >& "unsupported OS type: $OSTYPE"
exit 1
esac
# Make sure /nix exists and is writeable
if [ -a /nix ]; then
if ! [ -w /nix ]; then
echo >&2 "/nix exists but is not writeable, can't set up nix-quick-install-action"
exit 1
else
rm -rf /nix/var/nix-quick-install-action
fi
elif [[ "$sys" =~ .*-darwin ]]; then
sudo $SHELL -euo pipefail << EOF
echo nix >> /etc/synthetic.conf
echo -e "run\\tprivate/var/run" >> /etc/synthetic.conf
/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B &>/dev/null \
|| /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -t &>/dev/null \
|| echo "warning: failed to execute apfs.util"
diskutil apfs addVolume disk1 APFS nix -mountpoint /nix
mdutil -i off /nix
chown $USER /nix
EOF
else
sudo install -d -o "$USER" /nix
if [[ "$NIX_ON_TMPFS" == "true" || "$NIX_ON_TMPFS" == "True" || "$NIX_ON_TMPFS" == "TRUE" ]]; then
sudo mount -t tmpfs -o size=90%,mode=0755,gid="$(id -g)",uid="$(id -u)" tmpfs /nix
fi
fi
# Fetch and unpack nix archive
if [[ "$sys" =~ .*-darwin ]]; then
# MacOS tar doesn't have the --skip-old-files, so we use gtar
tar=gtar
else
tar=tar
fi
rel="$(head -n1 "$RELEASE_FILE")"
url="${NIX_ARCHIVES_URL:-https://github.com/nixbuild/nix-quick-install-action/releases/download/$rel}/nix-$NIX_VERSION-$sys.tar.zstd"
echo >&2 "Fetching nix archives from $url"
case "$url" in
file://)
"$tar" --skip-old-files --strip-components 1 -x -I unzstd -C /nix "${url#file://}"
;;
*)
curl -sL --retry 3 --retry-connrefused "$url" \
| "$tar" --skip-old-files --strip-components 1 -x -I unzstd -C /nix
;;
esac
# Setup nix.conf
NIX_CONF_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/nix/nix.conf"
mkdir -p "$(dirname "$NIX_CONF_FILE")"
touch "$NIX_CONF_FILE"
if [ -n "${NIX_CONF:-}" ]; then
printenv NIX_CONF > "$NIX_CONF_FILE"
fi
# Setup GitHub access token
if [[ -n "${GITHUB_ACCESS_TOKEN:-}" ]]; then
echo >>"$NIX_CONF_FILE" \
"access-tokens = github.com=$GITHUB_ACCESS_TOKEN"
fi
# Setup Flakes
if vergt "$NIX_VERSION" "2.13"; then
echo >>"$NIX_CONF_FILE" \
"experimental-features = nix-command flakes"
echo >>"$NIX_CONF_FILE" \
"accept-flake-config = true"
fi
# Populate the nix db
nix="$(readlink /nix/var/nix-quick-install-action/nix)"
retries=2
while true; do
"$nix/bin/nix-store" \
--load-db < /nix/var/nix-quick-install-action/registration && break || true
((retries--))
echo >&2 "Retrying Nix DB registration"
sleep 2
done
# Install nix in profile
MANPATH= . "$nix/etc/profile.d/nix.sh"
"$nix/bin/nix-env" -i "$nix"
# Certificate bundle is not detected by nix.sh on macOS.
if [ -z "${NIX_SSL_CERT_FILE:-}" -a -e "/etc/ssl/cert.pem" ]; then
NIX_SSL_CERT_FILE="/etc/ssl/cert.pem"
fi
# Set env
echo "$HOME/.nix-profile/bin" >> $GITHUB_PATH
echo "NIX_PROFILES=/nix/var/nix/profiles/default $HOME/.nix-profile" >> $GITHUB_ENV
echo "NIX_USER_PROFILE_DIR=/nix/var/nix/profiles/per-user/$USER" >> $GITHUB_ENV
echo "NIX_SSL_CERT_FILE=$NIX_SSL_CERT_FILE" >> $GITHUB_ENV