-
-
Save LnL7/76362609a50de2401c5165678d800ce6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
root_disks() { | |
diskutil list -plist / | |
} | |
apfs_volumes_for() { | |
disk=$1 | |
diskutil apfs list -plist "$disk" | |
} | |
disk_identifier() { | |
xpath "/plist/dict/key[text()='WholeDisks']/following-sibling::array[1]/string/text()" 2>/dev/null | |
} | |
volume_get() { | |
key=$1 i=$2 | |
xpath "/plist/dict/array/dict/key[text()='Volumes']/following-sibling::array/dict[$i]/key[text()='$key']/following-sibling::string[1]/text()" 2> /dev/null | |
} | |
find_nix_volume() { | |
disk=$1 | |
i=1 | |
volumes=$(apfs_volumes_for "$disk") | |
while true; do | |
name=$(echo "$volumes" | volume_get "Name" "$i") | |
if [ -z "$name" ]; then | |
break | |
fi | |
case "$name" in | |
[Nn]ix*) | |
echo "$name" | |
break | |
;; | |
esac | |
i=$((i+1)) | |
done | |
} | |
test_fstab() { | |
grep -q "/nix" /etc/fstab 2>/dev/null | |
} | |
test_synthetic_conf() { | |
grep -q "^nix" /etc/synthetic.conf 2>/dev/null | |
} | |
test_nix() { | |
test -d "/nix" | |
} | |
main() { | |
( | |
echo "" | |
echo "NOTICE:" | |
echo "" | |
echo " ------------------------------------------------------------------ " | |
echo " | This installer will create a volume for the nix store and |" | |
echo " | configure it to mount at /nix. Follow these steps to uninstall. |" | |
echo " ------------------------------------------------------------------ " | |
echo "" | |
echo " 1. Remove the entry from fstab using 'sudo vifs'" | |
echo " 2. Destroy the data volume using 'diskutil apfs deleteVolume'" | |
echo " 3. Delete /etc/synthetic.conf" | |
echo "" | |
) >&2 | |
if [ -L "/nix" ]; then | |
echo "error: /nix is a symlink, please remove it or edit synthetic.conf (requires reboot)" >&2 | |
echo " /nix -> $(readlink "/nix")" >&2 | |
exit 2 | |
fi | |
if ! test_synthetic_conf; then | |
echo "Configuring /etc/synthetic.conf..." >&2 | |
printf "$ " >&2; echo "echo nix | sudo tee /etc/synthetic.conf" | |
printf "$ " >&2; echo "/System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util -B" | |
fi | |
if ! test_nix; then | |
echo "Creating mountpoint for /nix..." >&2 | |
printf "$ " >&2; echo "sudo mkdir /nix" | |
fi | |
disk=$(root_disks | disk_identifier) | |
volume=$(find_nix_volume "$disk") | |
if [ -z "$volume" ]; then | |
echo "Creating a Nix Store volume..." >&2 | |
printf "$ " >&2; echo "diskutil apfs addVolume $disk APFS 'Nix Store' -mountpoint /nix" | |
volume="Nix Store" | |
fi | |
if ! test_fstab; then | |
echo "Configuring /etc/fstab..." >&2 | |
label=$(echo "$volume" | sed 's/ /\\040/g') | |
printf "$ " >&2; echo "printf '\$a\nLABEL=$label /nix apfs rw\n.\nwq\n' | EDITOR=vi sudo vifs" | |
printf "$ " >&2; echo "sudo defaults write /Library/Preferences/SystemConfiguration/autodiskmount AutomountDisksWithoutUserLogin -bool true" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment