Skip to content

Commit d562bb6

Browse files
committed
Add Nix installation script
1 parent 700417f commit d562bb6

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

script/install.d/13-nix.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
# -----------------------------------------------------------------------------
4+
# Description:
5+
# Install the Nix package manager on supported platforms.
6+
#
7+
8+
ensure_prereqs() {
9+
local missing=()
10+
for dep in curl xz; do
11+
if ! command -v "$dep" >/dev/null 2>&1; then
12+
missing+=("$dep")
13+
fi
14+
done
15+
16+
if [[ ${#missing[@]} -eq 0 ]]; then
17+
return 0
18+
fi
19+
20+
if command -v brew >/dev/null 2>&1; then
21+
log_info "Installing ${missing[*]} via Homebrew"
22+
brew install "${missing[@]}" >/dev/null 2>&1 || return 1
23+
return 0
24+
fi
25+
26+
if command -v pacman >/dev/null 2>&1; then
27+
log_info "Installing ${missing[*]} via pacman"
28+
sudo pacman -S --noconfirm --needed "${missing[@]}" >/dev/null 2>&1 || return 1
29+
return 0
30+
fi
31+
32+
if command -v apt-get >/dev/null 2>&1; then
33+
log_info "Installing ${missing[*]} via apt"
34+
sudo apt-get update -qq >/dev/null 2>&1 || return 1
35+
sudo apt-get install -y curl xz-utils >/dev/null 2>&1 || return 1
36+
return 0
37+
fi
38+
39+
log_warn "Unable to install prerequisites automatically (${missing[*]} missing)"
40+
return 1
41+
}
42+
43+
run_nix_installer() {
44+
local platform="$1"
45+
local -a args=()
46+
47+
case "$platform" in
48+
linux)
49+
args+=("--daemon")
50+
;;
51+
darwin)
52+
args+=("--daemon")
53+
;;
54+
*)
55+
log_warn "Unsupported platform for Nix installer"
56+
return 1
57+
;;
58+
esac
59+
60+
if ! ensure_prereqs; then
61+
log_warn "Skipping Nix install: prerequisites not satisfied"
62+
return 1
63+
fi
64+
65+
local installer
66+
installer=$(mktemp)
67+
if ! curl -fsSL https://nixos.org/nix/install -o "$installer"; then
68+
log_error "Failed to download Nix installer"
69+
rm -f "$installer"
70+
return 1
71+
fi
72+
73+
chmod +x "$installer"
74+
if yes | sh "$installer" "${args[@]}"; then
75+
log_pass "Nix installation complete"
76+
rm -f "$installer"
77+
return 0
78+
else
79+
log_error "Nix installer exited with an error"
80+
rm -f "$installer"
81+
return 1
82+
fi
83+
}
84+
85+
main() {
86+
if command -v nix-env >/dev/null 2>&1 || command -v nix >/dev/null 2>&1; then
87+
log_info "Nix already installed; skipping"
88+
return
89+
fi
90+
91+
case "${OSTYPE:-}" in
92+
linux-gnu*)
93+
log_info "Installing Nix (Linux daemon mode)"
94+
run_nix_installer "linux"
95+
;;
96+
darwin*)
97+
log_info "Installing Nix (macOS daemon mode)"
98+
run_nix_installer "darwin"
99+
;;
100+
*)
101+
log_warn "Skipping Nix install: unsupported platform (${OSTYPE:-unknown})"
102+
;;
103+
esac
104+
}
105+
106+
main "$@"
107+
unset -f main
108+
unset -f ensure_prereqs
109+
unset -f run_nix_installer

0 commit comments

Comments
 (0)