diff options
| author | Mike Vink <59492084+ivi-vink@users.noreply.github.com> | 2025-01-16 22:22:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-16 22:22:34 +0100 |
| commit | 8e7bd91f353caacc0bc4105f573eb3e17f09e03a (patch) | |
| tree | c5059edcbebd9644290cad7c653c49a36d593021 /pkgs/nix-tools | |
| parent | 6bd39d420578aacf7c0bab7de3e7027b952115ae (diff) | |
| parent | bd921223ba7cdac346477d7ea5204d6f4736fcc6 (diff) | |
Diffstat (limited to 'pkgs/nix-tools')
| -rw-r--r-- | pkgs/nix-tools/darwin-rebuild.sh | 86 | ||||
| -rw-r--r-- | pkgs/nix-tools/default.nix | 1 |
2 files changed, 44 insertions, 43 deletions
diff --git a/pkgs/nix-tools/darwin-rebuild.sh b/pkgs/nix-tools/darwin-rebuild.sh index 6422262..7824913 100644 --- a/pkgs/nix-tools/darwin-rebuild.sh +++ b/pkgs/nix-tools/darwin-rebuild.sh @@ -9,16 +9,25 @@ showSyntax() { 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 " [-I path] [--option name value] [--arg name value] [--argstr name value]" >&2 - echo " [--flake flake] [--update-input input flake] [--impure] [--recreate-lock-file]" >&2 - echo " [--no-update-lock-file] [--refresh] ..." >&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 - command sudo -H --preserve-env=PATH env "$@" + # 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 @@ -33,6 +42,7 @@ extraProfileFlags=() profile=@profile@ action= flake= +noFlake= while [ $# -gt 0 ]; do i=$1; shift 1 @@ -43,7 +53,7 @@ while [ $# -gt 0 ]; do edit|switch|activate|build|check|changelog) action=$i ;; - --show-trace|--keep-going|--keep-failed|--verbose|-v|-vv|-vvv|-vvvv|-vvvvv|--fallback) + --show-trace|--keep-going|--keep-failed|--verbose|-v|-vv|-vvv|-vvvv|-vvvvv|--fallback|--offline) extraMetadataFlags+=("$i") extraBuildFlags+=("$i") ;; @@ -76,6 +86,9 @@ while [ $# -gt 0 ]; do 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") ;; @@ -116,6 +129,15 @@ while [ $# -gt 0 ]; do 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 @@ -127,41 +149,23 @@ if [ -z "$action" ]; then showSyntax; fi flakeFlags=(--extra-experimental-features 'nix-command flakes') -if [ -n "$flake" ]; then - # Offical regex from https://www.rfc-editor.org/rfc/rfc3986#appendix-B - if [[ "${flake}" =~ ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? ]]; then - scheme=${BASH_REMATCH[1]} # eg. http: - authority=${BASH_REMATCH[3]} # eg. //www.ics.uci.edu - path=${BASH_REMATCH[5]} # eg. /pub/ietf/uri/ - queryWithQuestion=${BASH_REMATCH[6]} - fragment=${BASH_REMATCH[9]} - - flake=${scheme}${authority}${path}${queryWithQuestion} - flakeAttr=${fragment} - fi - if [ -z "$flakeAttr" ]; then - flakeAttr=$(scutil --get LocalHostName) - fi - flakeAttr=darwinConfigurations.${flakeAttr} +# 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 -if [ -n "$flake" ]; then - if nix "${flakeFlags[@]}" flake metadata --version &>/dev/null; then - cmd=metadata - else - cmd=info +# 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 - - metadata=$(nix "${flakeFlags[@]}" flake "$cmd" --json "${extraMetadataFlags[@]}" "${extraLockFlags[@]}" -- "$flake") - flake=$(jq -r .url <<<"${metadata}") - - if [ "$(jq -r .resolved.submodules <<<"${metadata}")" = "true" ]; then - if [[ "$flake" == *'?'* ]]; then - flake="${flake}&submodules=1" - else - flake="${flake}?submodules=1" - fi + if [[ -z "$flakeAttr" ]]; then + flakeAttr=$(scutil --get LocalHostName) fi + flakeAttr=darwinConfigurations.${flakeAttr} fi if [ "$action" != build ]; then @@ -181,7 +185,7 @@ if [ "$action" = edit ]; then fi fi -if [ "$action" = switch ] || [ "$action" = build ] || [ "$action" = check ]; then +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)" @@ -230,11 +234,7 @@ if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback fi if [ "$action" = changelog ]; then - echo >&2 - echo "[1;1mCHANGELOG[0m" >&2 - echo >&2 - head -n 32 "$systemConfig/darwin-changes" - echo >&2 + ${PAGER:-less} -- "$systemConfig/darwin-changes" fi if [ "$action" = check ]; then diff --git a/pkgs/nix-tools/default.nix b/pkgs/nix-tools/default.nix index 4e4336a..5fdc382 100644 --- a/pkgs/nix-tools/default.nix +++ b/pkgs/nix-tools/default.nix @@ -17,6 +17,7 @@ let inherit name src; dir = "bin"; isExecutable = true; + meta.mainProgram = name; } // env); path = "${extraPath}:${systemPath}"; |
