2016-12-12 16:34:43 +00:00
|
|
|
#! @shell@
|
|
|
|
set -e
|
|
|
|
set -o pipefail
|
|
|
|
export PATH=@path@:$PATH
|
|
|
|
|
|
|
|
|
|
|
|
showSyntax() {
|
2019-06-05 20:59:23 +00:00
|
|
|
echo "darwin-rebuild [--help] {edit | switch | activate | build | check | changelog}" >&2
|
2017-10-20 19:45:31 +00:00
|
|
|
echo " [--list-generations] [{--profile-name | -p} name] [--rollback]" >&2
|
|
|
|
echo " [{--switch-generation | -G} generation] [--verbose...] [-v...]" >&2
|
2023-07-01 15:11:16 +00:00
|
|
|
echo " [-Q] [{--max-jobs | -j} number] [--cores number] [--dry-run]" >&2
|
2024-10-22 21:10:34 +00:00
|
|
|
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
|
2016-12-15 20:52:53 +00:00
|
|
|
exit 1
|
2016-12-12 16:34:43 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:40:13 +00:00
|
|
|
sudo() {
|
2024-10-22 12:57:13 +00:00
|
|
|
# REMOVEME when support for macOS 10.13 is dropped
|
|
|
|
# macOS 10.13 does not support sudo --preserve-env so we make this conditional
|
2023-12-22 14:40:13 +00:00
|
|
|
if command sudo --help | grep -- --preserve-env= >/dev/null; then
|
2024-10-22 12:57:13 +00:00
|
|
|
# We use `env` before our command to ensure the preserved PATH gets checked
|
|
|
|
# when trying to resolve the command to execute
|
2024-10-22 11:38:17 +00:00
|
|
|
command sudo -H --preserve-env=PATH --preserve-env=SSH_CONNECTION env "$@"
|
2023-12-22 14:40:13 +00:00
|
|
|
else
|
|
|
|
command sudo -H "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-12 16:34:43 +00:00
|
|
|
# Parse the command line.
|
|
|
|
origArgs=("$@")
|
2022-10-10 11:14:05 +00:00
|
|
|
extraMetadataFlags=()
|
2016-12-19 23:05:01 +00:00
|
|
|
extraBuildFlags=()
|
2020-10-18 12:22:54 +00:00
|
|
|
extraLockFlags=()
|
2017-05-15 19:16:09 +00:00
|
|
|
extraProfileFlags=()
|
2016-12-19 23:05:01 +00:00
|
|
|
profile=@profile@
|
2016-12-12 16:34:43 +00:00
|
|
|
action=
|
2020-10-18 12:22:54 +00:00
|
|
|
flake=
|
2023-07-15 01:02:13 +00:00
|
|
|
noFlake=
|
2016-12-12 16:34:43 +00:00
|
|
|
|
2020-02-03 21:47:55 +00:00
|
|
|
while [ $# -gt 0 ]; do
|
2019-06-05 19:35:58 +00:00
|
|
|
i=$1; shift 1
|
|
|
|
case $i in
|
2016-12-15 20:52:53 +00:00
|
|
|
--help)
|
|
|
|
showSyntax
|
|
|
|
;;
|
2019-06-05 20:59:23 +00:00
|
|
|
edit|switch|activate|build|check|changelog)
|
2019-06-05 19:35:58 +00:00
|
|
|
action=$i
|
2016-12-15 20:52:53 +00:00
|
|
|
;;
|
2024-05-18 13:25:53 +00:00
|
|
|
--show-trace|--keep-going|--keep-failed|--verbose|-v|-vv|-vvv|-vvvv|-vvvvv|--fallback|--offline)
|
2022-10-10 11:14:05 +00:00
|
|
|
extraMetadataFlags+=("$i")
|
|
|
|
extraBuildFlags+=("$i")
|
|
|
|
;;
|
|
|
|
--no-build-hook|--dry-run|-k|-K|-Q)
|
2016-12-19 23:05:01 +00:00
|
|
|
extraBuildFlags+=("$i")
|
|
|
|
;;
|
2017-01-24 20:47:34 +00:00
|
|
|
-j[0-9]*)
|
|
|
|
extraBuildFlags+=("$i")
|
|
|
|
;;
|
2016-12-19 23:05:01 +00:00
|
|
|
--max-jobs|-j|--cores|-I)
|
2020-02-03 21:47:55 +00:00
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
echo "$0: '$i' requires an argument"
|
2016-12-19 23:05:01 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-06-05 19:35:58 +00:00
|
|
|
j=$1; shift 1
|
2016-12-19 23:05:01 +00:00
|
|
|
extraBuildFlags+=("$i" "$j")
|
|
|
|
;;
|
|
|
|
--arg|--argstr|--option)
|
2020-02-03 21:47:55 +00:00
|
|
|
if [ $# -lt 2 ]; then
|
|
|
|
echo "$0: '$i' requires two arguments"
|
2016-12-19 23:05:01 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-06-05 19:35:58 +00:00
|
|
|
j=$1
|
|
|
|
k=$2
|
|
|
|
shift 2
|
2022-10-10 11:14:05 +00:00
|
|
|
extraMetadataFlags+=("$i" "$j" "$k")
|
2016-12-19 23:05:01 +00:00
|
|
|
extraBuildFlags+=("$i" "$j" "$k")
|
|
|
|
;;
|
2020-10-18 12:22:54 +00:00
|
|
|
--flake)
|
|
|
|
flake=$1
|
|
|
|
shift 1
|
|
|
|
;;
|
2023-07-15 01:02:13 +00:00
|
|
|
--no-flake)
|
|
|
|
noFlake=1
|
|
|
|
;;
|
2023-06-28 16:47:31 +00:00
|
|
|
-L|-vL|--print-build-logs|--impure|--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file|--refresh)
|
2020-10-18 12:22:54 +00:00
|
|
|
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")
|
|
|
|
;;
|
2017-10-20 19:45:31 +00:00
|
|
|
--list-generations)
|
|
|
|
action="list"
|
|
|
|
extraProfileFlags=("$i")
|
|
|
|
;;
|
2017-05-15 19:16:09 +00:00
|
|
|
--rollback)
|
|
|
|
action="rollback"
|
|
|
|
extraProfileFlags=("$i")
|
|
|
|
;;
|
|
|
|
--switch-generation|-G)
|
|
|
|
action="rollback"
|
2020-02-03 21:47:55 +00:00
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
echo "$0: '$i' requires an argument"
|
2017-05-15 19:16:09 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-06-05 19:35:58 +00:00
|
|
|
j=$1; shift 1
|
2017-05-15 19:16:09 +00:00
|
|
|
extraProfileFlags=("$i" "$j")
|
|
|
|
;;
|
2016-12-19 23:05:01 +00:00
|
|
|
--profile-name|-p)
|
|
|
|
if [ -z "$1" ]; then
|
2020-02-03 21:47:55 +00:00
|
|
|
echo "$0: '$i' requires an argument"
|
2016-12-19 23:05:01 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ "$1" != system ]; then
|
|
|
|
profile="/nix/var/nix/profiles/system-profiles/$1"
|
|
|
|
mkdir -p -m 0755 "$(dirname "$profile")"
|
|
|
|
fi
|
|
|
|
shift 1
|
|
|
|
;;
|
2024-04-06 16:30:01 +00:00
|
|
|
--substituters)
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
echo "$0: '$i' requires an argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
j=$1; shift 1
|
|
|
|
extraMetadataFlags+=("$i" "$j")
|
|
|
|
extraBuildFlags+=("$i" "$j")
|
|
|
|
;;
|
2016-12-15 20:52:53 +00:00
|
|
|
*)
|
2020-02-03 21:47:55 +00:00
|
|
|
echo "$0: unknown option '$i'"
|
2016-12-15 20:52:53 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2016-12-12 16:34:43 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$action" ]; then showSyntax; fi
|
|
|
|
|
2022-02-04 08:16:08 +00:00
|
|
|
flakeFlags=(--extra-experimental-features 'nix-command flakes')
|
2020-10-18 12:22:54 +00:00
|
|
|
|
2023-07-15 01:02:13 +00:00
|
|
|
# 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.
|
2024-11-03 01:01:00 +00:00
|
|
|
if [[ -n "$flake" ]]; then
|
|
|
|
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
|
|
|
|
flake="${BASH_REMATCH[1]}"
|
|
|
|
flakeAttr="${BASH_REMATCH[2]}"
|
2020-10-18 12:22:54 +00:00
|
|
|
fi
|
2024-11-03 01:01:00 +00:00
|
|
|
if [[ -z "$flakeAttr" ]]; then
|
2023-05-29 10:10:46 +00:00
|
|
|
flakeAttr=$(scutil --get LocalHostName)
|
2020-10-18 12:22:54 +00:00
|
|
|
fi
|
2021-07-09 08:12:23 +00:00
|
|
|
flakeAttr=darwinConfigurations.${flakeAttr}
|
2020-10-18 12:22:54 +00:00
|
|
|
fi
|
|
|
|
|
2022-12-08 09:00:51 +00:00
|
|
|
if [ "$action" != build ]; then
|
2022-12-08 08:48:45 +00:00
|
|
|
if [ -n "$flake" ]; then
|
|
|
|
extraBuildFlags+=("--no-link")
|
|
|
|
else
|
|
|
|
extraBuildFlags+=("--no-out-link")
|
|
|
|
fi
|
2017-03-04 19:00:23 +00:00
|
|
|
fi
|
|
|
|
|
2018-08-03 18:14:28 +00:00
|
|
|
if [ "$action" = edit ]; then
|
2019-02-24 21:27:41 +00:00
|
|
|
darwinConfig=$(nix-instantiate --find-file darwin-config)
|
2020-10-18 12:22:54 +00:00
|
|
|
if [ -z "$flake" ]; then
|
|
|
|
exec "${EDITOR:-vi}" "$darwinConfig"
|
|
|
|
else
|
|
|
|
exec nix "${flakeFlags[@]}" edit "${extraLockFlags[@]}" -- "$flake#$flakeAttr"
|
|
|
|
fi
|
2018-08-03 18:14:28 +00:00
|
|
|
fi
|
|
|
|
|
2024-09-11 09:22:21 +00:00
|
|
|
if [ "$action" = switch ] || [ "$action" = build ] || [ "$action" = check ] || [ "$action" = changelog ]; then
|
2017-10-20 19:45:31 +00:00
|
|
|
echo "building the system configuration..." >&2
|
2020-10-18 12:22:54 +00:00
|
|
|
if [ -z "$flake" ]; then
|
|
|
|
systemConfig="$(nix-build '<darwin>' "${extraBuildFlags[@]}" -A system)"
|
|
|
|
else
|
2022-12-08 08:48:45 +00:00
|
|
|
systemConfig=$(nix "${flakeFlags[@]}" build --json \
|
|
|
|
"${extraBuildFlags[@]}" "${extraLockFlags[@]}" \
|
|
|
|
-- "$flake#$flakeAttr.system" \
|
|
|
|
| jq -r '.[0].outputs.out')
|
2020-10-18 12:22:54 +00:00
|
|
|
fi
|
2016-12-12 16:34:43 +00:00
|
|
|
fi
|
|
|
|
|
2020-10-18 12:22:54 +00:00
|
|
|
if [ "$action" = list ] || [ "$action" = rollback ]; then
|
|
|
|
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
|
2023-12-22 14:40:13 +00:00
|
|
|
sudo nix-env -p "$profile" "${extraProfileFlags[@]}"
|
2017-05-15 19:16:09 +00:00
|
|
|
else
|
2019-06-05 19:35:58 +00:00
|
|
|
nix-env -p "$profile" "${extraProfileFlags[@]}"
|
2017-05-15 19:16:09 +00:00
|
|
|
fi
|
2019-06-05 20:59:23 +00:00
|
|
|
fi
|
2017-05-15 19:16:09 +00:00
|
|
|
|
2019-07-01 20:42:44 +00:00
|
|
|
if [ "$action" = rollback ]; then
|
2017-05-15 19:16:09 +00:00
|
|
|
systemConfig="$(cat $profile/systemConfig)"
|
|
|
|
fi
|
|
|
|
|
2019-06-05 21:23:59 +00:00
|
|
|
if [ "$action" = activate ]; then
|
|
|
|
systemConfig=$(readlink -f "${0%*/sw/bin/darwin-rebuild}")
|
|
|
|
fi
|
|
|
|
|
2016-12-22 22:25:32 +00:00
|
|
|
if [ -z "$systemConfig" ]; then exit 0; fi
|
|
|
|
|
2016-12-12 16:34:43 +00:00
|
|
|
if [ "$action" = switch ]; then
|
2020-10-18 12:22:54 +00:00
|
|
|
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
|
2023-12-22 14:40:13 +00:00
|
|
|
sudo nix-env -p "$profile" --set "$systemConfig"
|
2016-12-15 20:52:53 +00:00
|
|
|
else
|
2019-06-05 19:35:58 +00:00
|
|
|
nix-env -p "$profile" --set "$systemConfig"
|
2016-12-15 20:52:53 +00:00
|
|
|
fi
|
2017-05-15 19:16:09 +00:00
|
|
|
fi
|
2016-12-14 14:45:45 +00:00
|
|
|
|
2020-10-18 12:22:54 +00:00
|
|
|
if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback ]; then
|
2019-06-05 19:35:58 +00:00
|
|
|
"$systemConfig/activate-user"
|
2017-05-11 19:36:57 +00:00
|
|
|
|
2016-12-15 20:52:53 +00:00
|
|
|
if [ "$USER" != root ]; then
|
2023-12-22 14:40:13 +00:00
|
|
|
sudo "$systemConfig/activate"
|
2016-12-15 20:52:53 +00:00
|
|
|
else
|
2019-06-05 19:35:58 +00:00
|
|
|
"$systemConfig/activate"
|
2016-12-15 20:52:53 +00:00
|
|
|
fi
|
2016-12-12 16:34:43 +00:00
|
|
|
fi
|
2017-07-23 16:02:08 +00:00
|
|
|
|
2017-07-24 21:08:26 +00:00
|
|
|
if [ "$action" = changelog ]; then
|
2024-09-11 09:22:21 +00:00
|
|
|
${PAGER:-less} -- "$systemConfig/darwin-changes"
|
2017-07-24 21:08:26 +00:00
|
|
|
fi
|
|
|
|
|
2017-07-23 16:02:08 +00:00
|
|
|
if [ "$action" = check ]; then
|
2017-07-24 21:08:26 +00:00
|
|
|
export checkActivation=1
|
2019-06-05 19:35:58 +00:00
|
|
|
"$systemConfig/activate-user"
|
2017-07-23 16:02:08 +00:00
|
|
|
fi
|