#!/usr/bin/env bash head /etc/os-release --lines 1 | grep 'NixOS$' &>/dev/null; test $? -eq 1; is_nixos=$? head /etc/os-release --lines 1 | grep 'Arch Linux' &>/dev/null; test $? -eq 1; is_arch_linux=$? set -e # NOTE: run this from inside a Linux installation, not from the live USB/CD # TODO: detect OS and perform OS-specific setup root_home="/root" daniel_home="/home/daniel/.home" nice_home="/home/daniel" dotfiles="/.config/lytedev-dotfiles" add_unstable_channel() { nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable nix-channel --update } clone_dotfiles() { echo "Setting up dotfiles for $USER..." mkdir --parents "$1" rm --recursive --force "$1" git clone "https://git.lyte.dev/lytedev/dotfiles" "$1" } symlink_nixos() { rm --force "/etc/nixos/lytedev" ln --symbolic "$1" "/etc/nixos/lytedev" } setup_wallpaper() { mkdir --parents "$nice_home/img/walls" curl --silent --output "$nice_home/img/walls/clouds_by_souredapply.png" \ "https://art.ngfiles.com/images/530000/530895_souredapple_clouds.png" rm --recursive --force "$daniel_home/.wallpaper" ln --symbolic "$nice_home/img/walls/clouds_by_souredapply.png" "$daniel_home/.wallpaper" } generate_ssh_key() { mkdir --mode 600 --parents "$daniel_home/.ssh" keyfile="$daniel_home/.ssh/$(hostname --short)" if ! [ -f "$keyfile" ]; then ssh-keygen -N '' -t ed25519 -f "$keyfile" mkdir --mode 640 --parents "$nice_home/public" cp "$keyfile.pub" "$nice_home/public" fi } fix_dotfiles_origin() { pushd "$daniel_home$dotfiles" git remote set-url origin "ssh://git@git.lyte.dev:2222/lytedev/dotfiles.git" popd } setup_home_manager() { nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager nix-channel --update nix-shell '' -A install } setup_dotfiles() { pushd "$daniel_home$dotfiles" ./bin/setup-dotfiles popd } distro_specific_root_setup() { if test $is_nixos -eq 1; then symlink_nixos "$root_home$dotfiles/env/nix/" add_unstable_channel nixos-rebuild switch # this should create the `daniel` user elif test $is_arch_linux -eq 1; then "$root_home$dotfiles/env/arch-linux/provision.d/00-add-user.bash" fi } distro_specific_user_setup() { if test $is_nixos -eq 1; then FUNC=$(declare -f symlink_nixos) sudo sh -c "$FUNC; symlink_nixos \"$daniel_home$dotfiles/env/nix/\"" elif test $is_arch_linux -eq 1; then "$root_home$dotfiles/env/arch-linux/provision.sh" fi } init_for_root() { clone_dotfiles "$root_home$dotfiles" distro_specific_root_setup clone_dotfiles "$daniel_home$dotfiles" chown daniel:users -R "$daniel_home" echo "Re-running as user daniel..." sudo --user daniel "$daniel_home$dotfiles/bin/init-dotfiles" } init_for_daniel() { set +x clone_dotfiles "$daniel_home$dotfiles" generate_ssh_key distro_specific_user_setup setup_wallpaper # TODO: setup ssh/gpg keys # TODO: setup password store fix_dotfiles_origin setup_dotfiles # TODO: fetch password store # TODO: fetch notes database } if [ "$EUID" -eq 0 ]; then if test $is_arch_linux -eq 1; then pacman -S --needed git fi init_for_root else init_for_daniel fi echo "Here is this machine's public SSH key:" echo " $(cat "$daniel_home/.ssh/$(hostname --short).pub")" echo "It needs to be added to existing cloud-based git accounts" echo "and other machines before proceeding." echo echo "Don't forget to setup GPG keys by importing from an existing machine" echo "or adding new child keys!" if test $is_arch_linux -eq 1; then echo echo 'You will need to set a password for your new user.' fi echo echo "The simplest method for doing this is to run the following:" echo ' scp -r "$TARGET_MACHINE:~/.gnupg" "$HOME/.gnupg'