blob: 7824913d2e2a71991bb9b9b2b90afc62a30abda8 (
plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
#! @shell@
set -e
set -o pipefail
export PATH=@path@:$PATH
showSyntax() {
echo "darwin-rebuild [--help] {edit | switch | activate | build | check | changelog}" >&2
echo " [--list-generations] [{--profile-name | -p} name] [--rollback]" >&2
echo " [{--switch-generation | -G} generation] [--verbose...] [-v...]" >&2
echo " [-Q] [{--max-jobs | -j} number] [--cores number] [--dry-run]" >&2
echo " [--keep-going | -k] [--keep-failed | -K] [--fallback] [--show-trace]" >&2
echo " [--print-build-logs | -L] [--impure] [-I path]" >&2
echo " [--option name value] [--arg name value] [--argstr name value]" >&2
echo " [--no-flake | [--flake flake]" >&2
echo " [--commit-lock-file] [--recreate-lock-file]" >&2
echo " [--no-update-lock-file] [--no-write-lock-file]" >&2
echo " [--override-input input flake] [--update-input input]" >&2
echo " [--no-registries] [--offline] [--refresh]]" >&2
echo " [--substituters substituters-list] ..." >&2
exit 1
}
sudo() {
# REMOVEME when support for macOS 10.13 is dropped
# macOS 10.13 does not support sudo --preserve-env so we make this conditional
if command sudo --help | grep -- --preserve-env= >/dev/null; then
# We use `env` before our command to ensure the preserved PATH gets checked
# when trying to resolve the command to execute
command sudo -H --preserve-env=PATH --preserve-env=SSH_CONNECTION env "$@"
else
command sudo -H "$@"
fi
}
# Parse the command line.
origArgs=("$@")
extraMetadataFlags=()
extraBuildFlags=()
extraLockFlags=()
extraProfileFlags=()
profile=@profile@
action=
flake=
noFlake=
while [ $# -gt 0 ]; do
i=$1; shift 1
case $i in
--help)
showSyntax
;;
edit|switch|activate|build|check|changelog)
action=$i
;;
--show-trace|--keep-going|--keep-failed|--verbose|-v|-vv|-vvv|-vvvv|-vvvvv|--fallback|--offline)
extraMetadataFlags+=("$i")
extraBuildFlags+=("$i")
;;
--no-build-hook|--dry-run|-k|-K|-Q)
extraBuildFlags+=("$i")
;;
-j[0-9]*)
extraBuildFlags+=("$i")
;;
--max-jobs|-j|--cores|-I)
if [ $# -lt 1 ]; then
echo "$0: '$i' requires an argument"
exit 1
fi
j=$1; shift 1
extraBuildFlags+=("$i" "$j")
;;
--arg|--argstr|--option)
if [ $# -lt 2 ]; then
echo "$0: '$i' requires two arguments"
exit 1
fi
j=$1
k=$2
shift 2
extraMetadataFlags+=("$i" "$j" "$k")
extraBuildFlags+=("$i" "$j" "$k")
;;
--flake)
flake=$1
shift 1
;;
--no-flake)
noFlake=1
;;
-L|-vL|--print-build-logs|--impure|--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file|--refresh)
extraLockFlags+=("$i")
;;
--update-input)
j="$1"; shift 1
extraLockFlags+=("$i" "$j")
;;
--override-input)
j="$1"; shift 1
k="$1"; shift 1
extraLockFlags+=("$i" "$j" "$k")
;;
--list-generations)
action="list"
extraProfileFlags=("$i")
;;
--rollback)
action="rollback"
extraProfileFlags=("$i")
;;
--switch-generation|-G)
action="rollback"
if [ $# -lt 1 ]; then
echo "$0: '$i' requires an argument"
exit 1
fi
j=$1; shift 1
extraProfileFlags=("$i" "$j")
;;
--profile-name|-p)
if [ -z "$1" ]; then
echo "$0: '$i' requires an argument"
exit 1
fi
if [ "$1" != system ]; then
profile="/nix/var/nix/profiles/system-profiles/$1"
mkdir -p -m 0755 "$(dirname "$profile")"
fi
shift 1
;;
--substituters)
if [ -z "$1" ]; then
echo "$0: '$i' requires an argument"
exit 1
fi
j=$1; shift 1
extraMetadataFlags+=("$i" "$j")
extraBuildFlags+=("$i" "$j")
;;
*)
echo "$0: unknown option '$i'"
exit 1
;;
esac
done
if [ -z "$action" ]; then showSyntax; fi
flakeFlags=(--extra-experimental-features 'nix-command flakes')
# Use /etc/nix-darwin/flake.nix if it exists. It can be a symlink to the
# actual flake.
if [[ -z $flake && -e /etc/nix-darwin/flake.nix && -z $noFlake ]]; then
flake="$(dirname "$(readlink -f /etc/nix-darwin/flake.nix)")"
fi
# For convenience, use the hostname as the default configuration to
# build from the flake.
if [[ -n "$flake" ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [[ -z "$flakeAttr" ]]; then
flakeAttr=$(scutil --get LocalHostName)
fi
flakeAttr=darwinConfigurations.${flakeAttr}
fi
if [ "$action" != build ]; then
if [ -n "$flake" ]; then
extraBuildFlags+=("--no-link")
else
extraBuildFlags+=("--no-out-link")
fi
fi
if [ "$action" = edit ]; then
darwinConfig=$(nix-instantiate --find-file darwin-config)
if [ -z "$flake" ]; then
exec "${EDITOR:-vi}" "$darwinConfig"
else
exec nix "${flakeFlags[@]}" edit "${extraLockFlags[@]}" -- "$flake#$flakeAttr"
fi
fi
if [ "$action" = switch ] || [ "$action" = build ] || [ "$action" = check ] || [ "$action" = changelog ]; then
echo "building the system configuration..." >&2
if [ -z "$flake" ]; then
systemConfig="$(nix-build '<darwin>' "${extraBuildFlags[@]}" -A system)"
else
systemConfig=$(nix "${flakeFlags[@]}" build --json \
"${extraBuildFlags[@]}" "${extraLockFlags[@]}" \
-- "$flake#$flakeAttr.system" \
| jq -r '.[0].outputs.out')
fi
fi
if [ "$action" = list ] || [ "$action" = rollback ]; then
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
sudo nix-env -p "$profile" "${extraProfileFlags[@]}"
else
nix-env -p "$profile" "${extraProfileFlags[@]}"
fi
fi
if [ "$action" = rollback ]; then
systemConfig="$(cat $profile/systemConfig)"
fi
if [ "$action" = activate ]; then
systemConfig=$(readlink -f "${0%*/sw/bin/darwin-rebuild}")
fi
if [ -z "$systemConfig" ]; then exit 0; fi
if [ "$action" = switch ]; then
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
sudo nix-env -p "$profile" --set "$systemConfig"
else
nix-env -p "$profile" --set "$systemConfig"
fi
fi
if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback ]; then
"$systemConfig/activate-user"
if [ "$USER" != root ]; then
sudo "$systemConfig/activate"
else
"$systemConfig/activate"
fi
fi
if [ "$action" = changelog ]; then
${PAGER:-less} -- "$systemConfig/darwin-changes"
fi
if [ "$action" = check ]; then
export checkActivation=1
"$systemConfig/activate-user"
fi
|