mirror of
https://github.com/LnL7/nix-darwin.git
synced 2025-03-30 19:54:43 +00:00
bootstrap: build user options/fetch repo with nix-prefetch-url
This commit is contained in:
parent
bfb6ae05aa
commit
7075538f7e
1 changed files with 129 additions and 28 deletions
157
bootstrap.sh
157
bootstrap.sh
|
@ -2,6 +2,38 @@
|
|||
set -e
|
||||
set -o pipefail
|
||||
|
||||
# Argument parsing
|
||||
init(){
|
||||
if [ $# -gt 0 ]; then
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
-u|--create-daemon-users)
|
||||
export CREATE_DAEMON_USERS=true
|
||||
;;
|
||||
*)
|
||||
echo -e "ERROR: Unrecognized parameter: '${1}'\nSee usage (--help) for options..." 1>&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
}
|
||||
|
||||
# Usage
|
||||
usage(){
|
||||
cat <<EOF
|
||||
Usage: bootstrap.sh [Options]
|
||||
|
||||
Options:
|
||||
|
||||
-h, --help: Show this message
|
||||
-u, --create-daemon-users: Create users & group for use with the Nix daemon
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
# Sanity checks
|
||||
sanity(){
|
||||
# Ensure the script is running on macOS
|
||||
|
@ -27,7 +59,7 @@ sanity(){
|
|||
# Check if nix-darwin is already present
|
||||
if [[ $(type darwin-rebuild 2>/dev/null) ]]; then
|
||||
echo -e "It looks like "$YELLOW"nix-darwin"$ESC" is already installed..."
|
||||
exit 1
|
||||
[ ! -z $CREATE_DAEMON_USERS ] && create_daemon_users || exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -37,9 +69,63 @@ exit_message(){
|
|||
exit 1
|
||||
}
|
||||
|
||||
# Prompt for sudo password & keep alive
|
||||
sudo_prompt(){
|
||||
echo "Please enter your password for sudo authentication"
|
||||
sudo -k
|
||||
sudo echo "sudo authenticaion successful!"
|
||||
while true ; do sudo -n true ; sleep 60 ; kill -0 "$$" || exit ; done 2>/dev/null &
|
||||
}
|
||||
|
||||
# Daemon setup
|
||||
create_daemon_users(){
|
||||
echo -e ""$BLUE"Nix daemon user/group configuration"$ESC""
|
||||
sudo_prompt
|
||||
|
||||
# If the group exists, dscl returns exit code 56.
|
||||
# Since this is not strictly an error code, standard code
|
||||
# checking doesn't work as intended.
|
||||
/usr/bin/dscl . -read /Groups/nixbld &> /dev/null
|
||||
retCode=$?
|
||||
if [[ $retCode != 0 ]]; then
|
||||
echo -e "Creating the "$YELLOW"nixbld"$ESC" group..."
|
||||
sudo /usr/sbin/dseditgroup -o create -r "Nix build group for nix-daemon" -i 30000 nixbld >&2 || \
|
||||
exit_message "Problem creating group: nixbld"
|
||||
else
|
||||
echo -e "It looks like the "$YELLOW"nixbld"$ESC" group already exists!"
|
||||
fi
|
||||
|
||||
for i in {1..10}; do
|
||||
/usr/bin/id nixbld${i} &> /dev/null
|
||||
retCode=$?
|
||||
if [[ $retCode != 0 ]]; then
|
||||
echo -e "Creating user: "$YELLOW"nixbld${i}"$ESC"..."
|
||||
sudo /usr/sbin/sysadminctl -fullName "Nix build user $i" \
|
||||
-home /var/empty \
|
||||
-shell /sbin/nologin \
|
||||
-UID $(expr 30000 + $i) \
|
||||
-addUser nixbld$i >&2 \
|
||||
|| exit_message "Problem creating user: nixbld${i}"
|
||||
|
||||
sudo dscl . -create /Users/nixbld$i IsHidden 1 || \
|
||||
exit_message "Problem setting 'IsHidden' for user: nixbld${i}"
|
||||
sudo dscl . -create /Users/nixbld$i UserShell /sbin/nologin || \
|
||||
exit_message "Problem setting shell for user: nixbld${i}"
|
||||
sudo /usr/sbin/dseditgroup -o edit -t user -a nixbld$i nixbld || \
|
||||
exit_message "Problem setting primary group for user: nixbld${i}"
|
||||
sudo /usr/bin/dscl . -create /Users/nixbld$i PrimaryGroupID 30000 >&2 || \
|
||||
exit_message "Problem setting PrimaryGroupID for user: nixbld${i}"
|
||||
else
|
||||
echo -e "It looks like the "$YELLOW"nixbld${i}"$ESC" user already exists!"
|
||||
fi
|
||||
done
|
||||
|
||||
[ ! -z $CREATE_DAEMON_USERS ] && exit 0
|
||||
}
|
||||
|
||||
# Installer
|
||||
install(){
|
||||
echo -e ""$BLUE"Welcome to the nix-darwin installer!"$ESC""
|
||||
echo -e ""$BLUE_UL"Welcome to the nix-darwin installer!"$ESC""
|
||||
|
||||
# Prompt for nix package upgrade
|
||||
echo -e "Ensuring "$YELLOW"nixpkgs"$ESC" version meets requirements..."
|
||||
|
@ -63,37 +149,26 @@ install(){
|
|||
esac
|
||||
done
|
||||
|
||||
# Prompt for initial sudo password & keep alive
|
||||
echo "Please enter your password for sudo authentication"
|
||||
sudo -k
|
||||
sudo echo "sudo authenticaion successful!"
|
||||
while true ; do sudo -n true ; sleep 60 ; kill -0 "$$" || exit ; done 2>/dev/null &
|
||||
|
||||
sudo_prompt
|
||||
|
||||
# Link run directory
|
||||
echo "Setting up /run..."
|
||||
test -L /run || sudo ln -s /private/var/run /run
|
||||
test -L /run || sudo ln -snf private/var/run /run
|
||||
|
||||
# Fetch the nix-darwin repo as a zip (shouldn't assume presence of git)
|
||||
REPO_DOWNLOAD=/tmp/nix-darwin-$(date +%Y%m%d).zip
|
||||
if [ ! -f $REPO_DOWNLOAD ]; then
|
||||
echo -e ""$YELLOW"Fetching nix-darwin repo..."$ESC""
|
||||
curl -Lo $REPO_DOWNLOAD https://github.com/LnL7/nix-darwin/archive/master.zip || \
|
||||
exit_message "Problem downloading nix-darwin repo"
|
||||
fi
|
||||
|
||||
# Extract the repository
|
||||
echo -e "Extracting repo to "$YELLOW"~/.nix-defexpr/darwin"$ESC"..."
|
||||
mkdir -p ~/.nix-defexpr
|
||||
cd ~/.nix-defexpr
|
||||
unzip -q $REPO_DOWNLOAD && echo -e ""$GREEN"Success!"$ESC""
|
||||
mv nix-darwin-master darwin
|
||||
cd - &> /dev/null
|
||||
# Fetch the nix-darwin repo
|
||||
echo -e ""$YELLOW"Fetching nix-darwin repo..."$ESC""
|
||||
nix-env -p "/nix/var/nix/profiles/per-user/$USER/darwin" \
|
||||
--set $(nix-prefetch-url --unpack --print-path \
|
||||
--name nix-darwin-17.03 \
|
||||
https://github.com/LnL7/nix-darwin/archive/master.tar.gz \
|
||||
| grep nix-darwin)
|
||||
ln -sfn "/nix/var/nix/profiles/per-user/$USER/darwin" "$HOME/.nix-defexpr/darwin"
|
||||
|
||||
# Copy the example configuration
|
||||
echo -e "Copying example configuration to "$YELLOW"~/.nixpkgs/darwin-configuration.nix"$ESC"..."
|
||||
mkdir -p ~/.nixpkgs
|
||||
cp ~/.nix-defexpr/darwin/modules/examples/simple.nix ~/.nixpkgs/darwin-configuration.nix
|
||||
|
||||
mkdir -p "$HOME/.nixpkgs"
|
||||
cp "$HOME/.nix-defexpr/darwin/modules/examples/simple.nix" "$HOME/.nixpkgs/darwin-configuration.nix"
|
||||
|
||||
# Bootstrap build using default nix.nixPath
|
||||
echo "Bootstrapping..."
|
||||
|
@ -108,7 +183,32 @@ install(){
|
|||
echo -e "Running first "$YELLOW"darwin-rebuild switch"$ESC"..."
|
||||
darwin-rebuild switch && echo -e ""$GREEN"Success!"$ESC"" || exit_message "Problem running darwin-rebuild switch"
|
||||
|
||||
echo -e ""$BLUE"You're all done!"$ESC""
|
||||
echo -e ""$BLUE_UL"Nix daemon"$ESC""
|
||||
echo "Optionally, this script can also create the group and users"
|
||||
echo -e "needed for running the Nix "$YELLOW"multi-user support daemon"$ESC"."
|
||||
echo "If you're unfamiliar with the Nix daemon, see:"
|
||||
echo -e ""$BLUE_UL"http://nixos.org/nix/manual/#sec-nix-daemon\n"$ESC""
|
||||
echo "If you decide not to, but later change your mind, you can always re-run"
|
||||
echo -e "this script with "$YELLOW"-u"$ESC" or "$YELLOW"--create-daemon-users"$ESC""
|
||||
|
||||
while true ; do
|
||||
read -p "Would you like to create the Nix daemon group/users? [y/n] " ANSWER
|
||||
case $ANSWER in
|
||||
y|Y)
|
||||
create_daemon_users
|
||||
break
|
||||
;;
|
||||
n|N)
|
||||
echo "Not creating Nix daemon group/users"
|
||||
break
|
||||
;;
|
||||
*)
|
||||
echo "Please answer 'y' or 'n'..."
|
||||
esac
|
||||
done
|
||||
|
||||
# Finish
|
||||
echo -e ""$GREEN"You're all done!"$ESC""
|
||||
echo -e "Take a look at "$YELLOW"~/.nixpkgs/darwin-configuration.nix"$ESC" to get started."
|
||||
echo -e "See the README for more information: "$BLUE_UL"https://github.com/LnL7/nix-darwin/blob/master/README.md"$ESC""
|
||||
}
|
||||
|
@ -125,9 +225,10 @@ main(){
|
|||
local YELLOW='\033[38;33m'
|
||||
local YELLOW_UL='\033[38;4;33m'
|
||||
|
||||
init $@
|
||||
sanity
|
||||
install
|
||||
}
|
||||
|
||||
# Actual run
|
||||
main
|
||||
main $@
|
||||
|
|
Loading…
Add table
Reference in a new issue