aboutsummaryrefslogtreecommitdiff
path: root/public/nixos/setup.sh
blob: 39b006c3a6365e9b2fe921c22666885e1d306395 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash
# shellcheck shell=bash
set -Eeuo pipefail

end="\033[0m"

green() {
  green_color="\033[0;32m"
  echo -e "${green_color}${1}${end}"
}

yellow() {
  yellow_color="\033[0;33m"
  echo -e "${yellow_color}${1}${end}"
}

blue() {
  blue_color="\033[0;34m"
  echo -e "${blue_color}${1}${end}"
}

red() {
  red_color="\033[0;31m"
  echo -e "${red_color}${1}${end}"
}

usage() {
  red "Missing input argument $1.\n"
  cat <<EOF
Usage:
    setup.sh <TAR_PATH>

      Arguments
        TAR_PATH   Path to the encrypted tar file with key pair and ownertrust information.

Examples:
    Download setup.sh and run it local './EuAndreh.tar.gpg' key pair:
        curl https://euandre.org/dotfiles/nixos/setup.sh | bash -s ./EuAndreh.tar.gpg
EOF
}

TAR_PATH="${1:-}"

[[ -z "${TAR_PATH}" ]] && {
  usage 'TAR_PATH'
  exit 2
}

import_gpg() {
  yellow "Import GPG data..."
  gpg -o EuAndreh.tar -d "${TAR_PATH}"
  tar -xvf EuAndreh.tar
  gpg --import keys.gpg
  gpg --import-ownertrust trust.txt

  rm "${TAR_PATH}"
  shred trust.txt
  rm trust.txt
  shred keys.gpg
  rm keys.gpg
  shred EuAndreh.tar
  rm EuAndreh.tar
  green "Done."
}

start_nextcloud() {
  yellow "Starting Nextcloud process and leaving it on the background..."
  nextcloud &> /dev/null &
  disown
  green "Done."

  yellow "Waiting for Nextcloud Git Annex repo to become available..."
  while true; do
    read -r -p "Has Nextcloud finished syncing? (Type uppercase sync): " sync
    [ "$sync" = 'SYNC' ] && break
    red "Please try again."
  done
  green "Done."
}

configure_git_annex() {
  yellow "Setting up Git Annex repository..."
  git clone "gcrypt::$HOME/Nextcloud/annex.git" "$HOME/annex/"
  pushd ~/annex/
  read -r -p "What's the description of the local Git Annex repository? " DESCRIPTION
  git config --global user.email "eu@euandre.org"
  git config --global user.name "EuAndreh"
  git annex describe here "${DESCRIPTION}"

  git annex enableremote nextcloud
  git annex enableremote s3
  # git annex enableremote hd
  # gitlab
  # rsyncnet

  git annex upgrade
  git annex sync
  popd
  green "Done."
}

configure_dotfiles() {
  yellow "Setting up temporary SSH configuration and dotfiles repository..."
  mkdir -p ~/.ssh
  cp ~/annex/secret/SSH/id_rsa.pub.txt ~/.ssh/id_rsa.pub
  cp ~/annex/secret/SSH/id_rsa.txt ~/.ssh/id_rsa
  chmod 600 ~/.ssh/id_rsa
  blue "Open up the password manager to decrypt the SSH private key"
  read -p "A prompt for the SSH password will appear! (Press any key to continue)" -n 1 -r

  export SSH_ENV="$HOME/.ssh/environment"
  echo "Initialising new SSH agent..."
  ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  echo succeeded
  chmod 600 "${SSH_ENV}"
  . "${SSH_ENV}" > /dev/null
  ssh-add

  mkdir -p ~/dev/libre/
  git clone git@git.sr.ht:~euandreh/dotfiles ~/dev/libre/dotfiles/
  pushd ~/dev/libre/dotfiles/
  git crypt unlock
  popd
  pushd ~/
  source ~/dev/libre/dotfiles/bash/bashrc.sh
  popd

  read -p "A prompt for the sudo password password will appear for the NixOS symlinks! (Press any key to continue)" -n 1 -r
  sudo ln -fs ~/dev/libre/dotfiles/nixos/configuration.nix /etc/nixos/configuration.nix
  # sudo create symlink for /etc/nixos/configuration.nix and /etc/nixos/local-configuration.nix
  green "Done."
}

restore_backups() {
  # rsync
  # borg serve

  # restore mbsync from backup
  # restore notmuch from backup
  # restore newsboat from backup
  # restore archive from backup
}

import_gpg
start_nextcloud
setup_git_annex
setup_dotfiles
restore_backups

green "All done!"