-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·189 lines (155 loc) · 4.64 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·189 lines (155 loc) · 4.64 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env bash
set -Ceuo pipefail
readonly CONFIG_NAME="workstation"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly MACHINE_LOCAL_DIR="$SCRIPT_DIR/machine-local"
readonly MACHINE_IDENTITIES_FILE="$MACHINE_LOCAL_DIR/identities.nix"
readonly NIX_FEATURES="nix-command flakes"
readonly NIX_DARWIN="github:nix-darwin/nix-darwin/nix-darwin-26.05"
require_1password_app() {
if [[ -d /Applications/1Password.app ]]; then
return
fi
cat >&2 <<'EOF'
1Password for Mac is required before running this setup.
Install the app and enable Settings > Developer > Integrate with 1Password CLI.
The `op` executable itself is managed by Nix. Then run this script again.
https://1password.com/downloads/mac/
EOF
exit 1
}
verify_1password_cli() {
local op_bin="/etc/profiles/per-user/$1/bin/op"
if [[ ! -x "$op_bin" ]]; then
echo "Nix activation completed, but the 1Password CLI was not installed at $op_bin." >&2
exit 1
fi
"$op_bin" --version >/dev/null
}
write_machine_identity() {
local target_user="$1"
local target_home
if ! /usr/bin/id "$target_user" >/dev/null 2>&1; then
echo "macOS user '$target_user' does not exist." >&2
exit 1
fi
target_home="$(resolve_home_directory "$target_user")"
if [[ -z "$target_home" || "$target_home" != /* ]]; then
echo "Could not resolve an absolute home directory for '$target_user'." >&2
exit 1
fi
/bin/mkdir -p "$MACHINE_LOCAL_DIR"
/bin/chmod 700 "$MACHINE_LOCAL_DIR"
printf '%s\n' "$target_user" >| "$MACHINE_LOCAL_DIR/username"
printf '%s\n' "$target_home" >| "$MACHINE_LOCAL_DIR/home-directory"
if [[ ! -e "$MACHINE_IDENTITIES_FILE" ]]; then
cat > "$MACHINE_IDENTITIES_FILE" <<'EOF'
{
corrupt952 = { };
labee = { };
# example = {
# directory = "example";
# git = {
# name = "Example User";
# email = "user@example.com";
# signingKey = null;
# };
# sallyport = {
# expand = false;
# env = { };
# };
# };
}
EOF
fi
/bin/chmod 600 \
"$MACHINE_LOCAL_DIR/username" \
"$MACHINE_LOCAL_DIR/home-directory" \
"$MACHINE_IDENTITIES_FILE"
}
resolve_home_directory() {
local account_record
local home_directory
local passwd_record
if account_record="$(
/usr/bin/dscl . -read "/Users/$1" NFSHomeDirectory 2>/dev/null
)"; then
home_directory="${account_record#NFSHomeDirectory:}"
home_directory="${home_directory#"${home_directory%%[![:space:]]*}"}"
printf '%s\n' "$home_directory"
return
fi
# Directory Services can transiently reject dscl in restricted environments.
if account_record="$(/usr/bin/dscacheutil -q user -a name "$1" 2>/dev/null)"; then
home_directory="$(
printf '%s\n' "$account_record" \
| /usr/bin/sed -n -E 's/^dir:[[:space:]]*//p'
)"
if [[ -n "$home_directory" ]]; then
printf '%s\n' "$home_directory"
return
fi
fi
# `id -P` reads the same account database in passwd(5) format on macOS.
if passwd_record="$(/usr/bin/id -P "$1" 2>/dev/null)"; then
printf '%s\n' "$passwd_record" | /usr/bin/awk -F: '{ print $(NF - 1) }'
fi
}
load_nix_environment() {
if command -v nix >/dev/null 2>&1; then
return
fi
local profile
for profile in \
/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh \
"$HOME/.nix-profile/etc/profile.d/nix.sh"; do
if [[ -r "$profile" ]]; then
# Lix's profile script currently reads ZSH_VERSION without a default.
# Do not leak this script's nounset option into external shell code.
set +u
# shellcheck source=/dev/null
source "$profile"
set -u
break
fi
done
}
install_nix() {
load_nix_environment
if command -v nix >/dev/null 2>&1; then
return
fi
curl --proto '=https' --tlsv1.2 --fail --silent --show-error --location \
https://install.lix.systems/lix \
| sh -s -- install
load_nix_environment
}
main() {
local nix_bin
local target_user
if [[ "$(uname -s)" != "Darwin" ]]; then
echo "This setup currently supports macOS only." >&2
exit 1
fi
if (( $# > 1 )); then
echo "Usage: $0 [username]" >&2
exit 1
fi
target_user="${1:-$(id -un)}"
require_1password_app
write_machine_identity "$target_user"
install_nix
if ! command -v nix >/dev/null 2>&1; then
echo "Nix was installed but is not available in this shell." >&2
echo "Open a new shell and run this script again." >&2
exit 1
fi
nix_bin="$(command -v nix)"
sudo -H "$nix_bin" \
--extra-experimental-features "$NIX_FEATURES" \
run "$NIX_DARWIN#darwin-rebuild" -- \
switch --flake "path:$SCRIPT_DIR#$CONFIG_NAME"
verify_1password_cli "$target_user"
}
main "$@"