Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # _____ _ _
  3. # / __ \ | | |
  4. # | / \/ |__ ___ ___| | _____
  5. # | | | '_ \ / _ \/ __| |/ / __|
  6. # | \__/\ | | | __/ (__| <\__ \
  7. # \____/_| |_|\___|\___|_|\_\___/
  8.  
  9. if ! efibootmgr > /dev/null; then
  10. echo "Your machine does not have EFI on right now! I cannot proceed."
  11. echo "You may want to try turning off CSM in your BIOS"
  12. exit 1
  13. fi
  14.  
  15. if [[ $EUID -ne 0 ]]; then
  16. echo "ERROR! You must run this as root!"
  17. exit 2
  18. fi
  19.  
  20. while true; do
  21. curl -sS 1.1.1.1 > /dev/null && break
  22. read -p "Connect to internet (WiFi control is in the bottom right), then press ENTER "
  23. done
  24.  
  25. # ______ _ _ _____ _ _
  26. # | _ (_) | | | _ | | | (_)
  27. # | | | |_ ___| | __ | | | |_ _ ___ ___| |_ _ ___ _ __ ___
  28. # | | | | / __| |/ / | | | | | | |/ _ \/ __| __| |/ _ \| '_ \/ __|
  29. # | |/ /| \__ \ < \ \/' / |_| | __/\__ \ |_| | (_) | | | \__ \
  30. # |___/ |_|___/_|\_\ \_/\_\\__,_|\___||___/\__|_|\___/|_| |_|___/
  31.  
  32. while true; do
  33. if [[ "$(lsblk -e1,7 --noheadings | wc -l)" == "1" ]]; then
  34. DEVICE="$(lsblk -e1,7 --noheadings --output=NAME)"
  35. break
  36. elif [[ "$(lsblk -e1,7 --noheadings --nodeps | wc -l)" == "1" ]]; then
  37. echo "There's only 1 device to install to (/dev/$(lsblk -e1,7 --noheadings --nodeps))."
  38. echo "But that device has partitions on it (and probably some files too!)."
  39. echo "If you proceed, those files will all be gone FOREVER!"
  40. DEVICE="$(lsblk -e1,7 --noheadings --nodeps)"
  41. break
  42. else
  43. echo "Which device would you like to install to?"
  44. echo
  45. lsblk -e1,7 --nodeps --noheadings --output="NAME,SIZE" | awk '{print $1 "\t\t(" $2 ")" }'
  46. echo
  47. read -p "Type the name: " DEVICE
  48. if lsblk -e1,7 --nodeps --noheadings --output="NAME" | grep $DEVICE > /dev/null; then
  49. if [[ "$(lsblk -e1,7 /dev/$DEVICE --noheadings | wc -l)" == "1" ]]; then
  50. break
  51. else
  52. echo "That device already has partitions! There may be files on them!"
  53. echo "If you proceed, those files will all be gone FOREVER!"
  54. break
  55. fi
  56. else
  57. echo "I can't find that device. Did you mistype it?"
  58. echo "Please type it correctly, and don't include the size in parenthesis at the end"
  59. fi
  60. fi
  61. done
  62. DEVICE="/dev/$DEVICE"
  63.  
  64. while true; do
  65. echo
  66. echo "When Linux runs out of RAM, everything goes to shit."
  67. echo "Swap (using another device as extra ram) helps avoid that catastrophe."
  68. # TODO: Should I tell them the truth? Do people even know what a gibibyte is?
  69. read -p "How many gigabytes of swap do you want? (e.g. 1) " SWAP_SIZE
  70.  
  71. if [[ "$SWAP_SIZE" =~ ^[0-9]+$ ]]; then
  72. break
  73. else
  74. echo "Invalid swap size!"
  75. fi
  76. done
  77. if [[ "$SWAP_SIZE" == "0" ]]; then
  78. echo "Invalid - I'll give you 1GiB of swap"
  79. SWAP_SIZE="1GiB"
  80. else
  81. SWAP_SIZE="$SWAP_SIZE"'GiB'
  82. fi
  83.  
  84. if [[ "$DEVICE" == "/dev/nvme"* ]]; then
  85. PARTITION="$DEVICE"p
  86. else
  87. PARTITION="$DEVICE"
  88. fi
  89.  
  90. echo
  91. echo "Ok, I will install to $DEVICE with a swap size of $SWAP_SIZE"
  92. echo "WARNING: THIS WILL WIPE EVERYTHING ON $DEVICE"
  93. read -p "Are you sure about this? Press Ctrl+C to cancel or ENTER to continue "
  94.  
  95. # _____ _ ______ _ _ _ _
  96. # / __ \ | | | ___ \ | | (_) | (_)
  97. # | / \/_ __ ___ __ _| |_ ___ | |_/ /_ _ _ __| |_ _| |_ _ ___ _ __ ___
  98. # | | | '__/ _ \/ _` | __/ _ \ | __/ _` | '__| __| | __| |/ _ \| '_ \/ __|
  99. # | \__/\ | | __/ (_| | || __/ | | | (_| | | | |_| | |_| | (_) | | | \__ \
  100. # \____/_| \___|\__,_|\__\___| \_| \__,_|_| \__|_|\__|_|\___/|_| |_|___/
  101.  
  102. # If they already attempted an installation, they may have mounted some partitions - unmount them in order to be able to partition the drive!
  103. umount -q /mnt/boot 2> /dev/null
  104. umount -q /mnt 2> /dev/null
  105. # Delete any previous partitions
  106. for part in $(parted $DEVICE -- print | awk '/^ / {print $1}'); do
  107. parted $DEVICE -- rm $part
  108. done
  109. yes | parted $DEVICE -- mklabel gpt > /dev/null
  110. parted $DEVICE -- mkpart primary 512MiB -"$SWAP_SIZE" > /dev/null
  111. yes ignore | parted $DEVICE -- mkpart primary linux-swap -"$SWAP_SIZE" 100% > /dev/null
  112. set -e
  113. parted $DEVICE -- mkpart ESP fat32 1MiB 512MiB > /dev/null
  114. parted $DEVICE -- set 3 boot on > /dev/null
  115.  
  116. # _____ _ ______ _ _ _
  117. # / __ \ | | | ___(_) | | |
  118. # | / \/_ __ ___ __ _| |_ ___ | |_ _| | ___ ___ _ _ ___| |_ ___ _ __ ___ ___
  119. # | | | '__/ _ \/ _` | __/ _ \ | _| | | |/ _ \/ __| | | / __| __/ _ \ '_ ` _ \/ __|
  120. # | \__/\ | | __/ (_| | || __/ | | | | | __/\__ \ |_| \__ \ || __/ | | | | \__ \
  121. # \____/_| \___|\__,_|\__\___| \_| |_|_|\___||___/\__, |___/\__\___|_| |_| |_|___/
  122. # __/ |
  123. # |___/
  124. mkfs.ext4 -L nixos "$PARTITION"1 > /dev/null
  125. mkswap -L swap "$PARTITION"2 > /dev/null
  126. set +e
  127. mkfs.fat -F 32 -n boot "$PARTITION"3 > /dev/null
  128. set -e
  129.  
  130. # ___ ___ _
  131. # | \/ | | |
  132. # | . . | ___ _ _ _ __ | |_
  133. # | |\/| |/ _ \| | | | '_ \| __|
  134. # | | | | (_) | |_| | | | | |_
  135. # \_| |_/\___/ \__,_|_| |_|\__|
  136.  
  137. mount /dev/disk/by-label/nixos /mnt
  138. mkdir -p /mnt/boot
  139. mount /dev/disk/by-label/boot /mnt/boot
  140.  
  141. # _ _ _ _____ _____ _____ __ _
  142. # | \ | (_) | _ / ___| / __ \ / _(_)
  143. # | \| |___ _| | | \ `--. | / \/ ___ _ __ | |_ _ __ _
  144. # | . ` | \ \/ / | | |`--. \ | | / _ \| '_ \| _| |/ _` |
  145. # | |\ | |> <\ \_/ /\__/ / | \__/\ (_) | | | | | | | (_| |
  146. # \_| \_/_/_/\_\\___/\____/ \____/\___/|_| |_|_| |_|\__, |
  147. # __/ |
  148. # |___/
  149. nixos-generate-config --root /mnt
  150.  
  151. set +e
  152. echo
  153. echo
  154. echo
  155. echo "The informational and warning messages above can be ignored safely."
  156. CLONED_FROM_DOTFILES=false
  157. while true; do
  158. read -p "Would you like to clone a NixOS configuration from git? (y/n) " -n 1 EXISTING
  159. echo
  160. if [[ "$EXISTING" == "y" ]]; then
  161. echo "I need a URL to clone - everything in it will be moved to /mnt/etc/nixos"
  162. echo "and /mnt/etc/nixos will become /etc/nixos after this installation is done."
  163. echo "Please provide a URL for git-clone. There are shorthands for popular hosts:"
  164. echo " gh/user/repo -> https://github.com/user/repo.git"
  165. echo " gl/user/repo -> https://gitlab.com/user/repo.git"
  166. echo
  167. echo "You need a configuration.nix file checked into that repo for this to work."
  168. echo "Alternatively, if you have an etc/nixos folder or nixos folder in your root,"
  169. echo "then the files in that folder will be used instead"
  170. echo "If you don't have one, hit ENTER without typing anything else."
  171. echo "Otherwise, type the git URL below:"
  172. read -p "" GIT_URL
  173. if [[ "$GIT_URL" == "gh/"* ]]; then
  174. GIT_URL="$(echo $GIT_URL | sed 's~^gh~https://github.com~')"'.git'
  175. elif [[ "$GIT_URL" == "gl/"* ]]; then
  176. GIT_URL="$(echo $GIT_URL | sed 's~^gl~https://gitlab.com~')"'.git'
  177. fi
  178. cd /mnt/etc/nixos
  179. echo "Loading..."
  180. nix-env -i "$(nix-env -qa git | head -1)"
  181. git clone $GIT_URL cloned_dotfiles
  182. if [[ "$?" != "0" ]]; then
  183. echo "ERROR CLONING! Did you get the right URL?" echo "Did you have the right authentication?"
  184. continue
  185. fi
  186. mv configuration.nix default-configuration.nix
  187. cd cloned_dotfiles
  188. if [[ -d etc/nixos ]]; then
  189. cd etc/nixos
  190. elif [[ -d nixos ]]; then
  191. cd nixos
  192. fi
  193. find . -type d -exec mkdir -p /mnt/etc/nixos/{} \; # Recreate directory structure
  194. find . -type f -exec cp -i {} /mnt/etc/nixos/{} \; # Copy files from this directory to /mnt/etc/nixos
  195. rm -Rf /mnt/etc/nixos/cloned_dotfiles # Delete the originally cloned stuff; you can clone it again later on if you want
  196. CLONED_FROM_DOTFILES=true
  197. echo
  198. echo
  199. echo "Done cloning. Tip: when you finish installing and reboot, you can run a"
  200. echo "post-install script from your dotfiles! Something like: /etc/nixos/setup.sh"
  201. cd /mnt/etc/nixos
  202. break
  203. else
  204. break
  205. fi
  206. done
  207.  
  208. echo
  209. echo
  210. echo
  211. echo "Look over the configuration in /mnt/etc/nixos - in particular, you'll want to"
  212. echo "edit hardware-configuration.nix."
  213. echo "You may want to change cpuFreqGovernor to ondemand (for more CPU power)."
  214. echo
  215. if [[ "$CLONED_FROM_DOTFILES" == "true" ]]; then
  216. echo "And you should consider adding something like this to that file:"
  217. echo
  218. echo " networking.hostName = \"my-nixos-computer\";"
  219. echo " networking.useDHCP = false;"
  220. echo " networking.interfaces.eno2.useDHCP = true;"
  221. echo " networking.interfaces.wlo1.useDHCP = true;"
  222. echo
  223. fi
  224. read -p "When you are ready, press ENTER to install from the
  225. configuration"
  226.  
  227. if nixos-install; then
  228. reboot
  229. else
  230. echo
  231. echo
  232. echo "nixos-install exited with an error code. Check its output above!"
  233. echo "If you think it's safe, reboot. If not, figure it out!"
  234. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement