nixos configurations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.2 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. #!/usr/bin/env bash
  2. # Change into script dir.
  3. cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit
  4. nixosDir=$(pwd)
  5. # Defaults
  6. defaultHostname="nixos"
  7. defaultSwap="8G"
  8. defaultName="grmrgecko"
  9. defaultDescription="James Coleman"
  10. defaultGitName="GRMrGecko"
  11. defaultGitEmail="grmrgecko@gmail.com"
  12. # A simple function to print an array.
  13. CHOICE=0
  14. chooseOpts() {
  15. local opts i
  16. CHOICE=-1
  17. opts=("$@")
  18. # Keep an index to properly index the options.
  19. i=0
  20. echo
  21. # For each option, print it and increment the index.
  22. for opt in "${opts[@]}"; do
  23. echo "$i) $opt"
  24. i=$((i+1))
  25. done
  26. # Ask for their choice.
  27. echo
  28. echo -n "Enter choice: "
  29. read -r CHOICE
  30. # Check inputted index range.
  31. if ((CHOICE >= ${#opts[@]} || CHOICE < 0)); then
  32. echo "Invalid range"
  33. chooseOpts "$@"
  34. fi
  35. }
  36. # A looping function to choose Y or N.
  37. chooseYN() {
  38. # Determine the default based on upper case Y or N in prompt.
  39. local default=""
  40. if [[ "$1" =~ \[.*([YN]).*\] ]]; then
  41. default=${BASH_REMATCH[1]}
  42. fi
  43. # Loop for the choice.
  44. while true; do
  45. # Prompt for choice.
  46. echo -n "$1: "
  47. read -r CHOICE
  48. # If choice is empty, set choice to the default.
  49. [[ -z $CHOICE ]] && CHOICE=$default
  50. # If choice does not equal Y or N, continue.
  51. # Otherwise set the global CHOICE variable to lowercase y or n.
  52. # Lowercase allows for easy logic in code that calls this function.
  53. if [[ "$CHOICE" =~ ^[yY]$ ]]; then
  54. CHOICE="y"
  55. elif [[ "$CHOICE" =~ ^[nN]$ ]]; then
  56. CHOICE="n"
  57. else
  58. continue
  59. fi
  60. break
  61. done
  62. }
  63. # Determine video drivers based on PCI devices.
  64. videoDrivers="unknown"
  65. pciRaw=$(lspci | grep -E 'VGA')
  66. if [[ "$pciRaw" =~ QXL ]]; then
  67. videoDrivers="qxl"
  68. elif [[ "$pciRaw" =~ NVIDIA ]]; then
  69. videoDrivers="nvidia"
  70. elif [[ "$pciRaw" =~ AMD ]]; then
  71. videoDrivers="amdgpu"
  72. fi
  73. # Get the packages souce, rather its unstable or stable.
  74. PACKAGESOPTS=(
  75. "stable"
  76. "unstable"
  77. )
  78. echo "Packages source"
  79. chooseOpts "${PACKAGESOPTS[@]}"
  80. PACKAGES=${PACKAGESOPTS[$CHOICE]}
  81. # Get the profile for this system.
  82. PROFILEOPTS=()
  83. # Build profile list from profiles directory.
  84. for profile in ./profiles/*.nix; do
  85. PROFILEOPTS+=("$(basename "${profile%.*}")")
  86. done
  87. echo "Choose your profile"
  88. chooseOpts "${PROFILEOPTS[@]}"
  89. PROFILE=${PROFILEOPTS[$CHOICE]}
  90. # Get the hostname.
  91. echo -n "Choose hostname [$defaultHostname]: "
  92. read -r hostName
  93. [[ -z $hostName ]] && hostName=$defaultHostname
  94. # Determine default disk.
  95. diskDefault=""
  96. [[ -e /dev/sda ]] && diskDefault="/dev/sda"
  97. [[ -e /dev/vda ]] && diskDefault="/dev/vda"
  98. echo
  99. echo "Select a disk from the list below:"
  100. # List disks to allow a choice to be made without stopping
  101. # configuration and verifying available disks.
  102. lsblk -o PATH,ID-LINK,SIZE -t
  103. echo
  104. echo -n "Choose disk (/dev/disk/by-id/{ID-LINK}) [$diskDefault]: "
  105. read -r disk
  106. # If selected disk is none, use the default disk determined above.
  107. [[ -z $disk ]] && disk=$diskDefault
  108. # Get the swap size.
  109. echo -n "Swap size [$defaultSwap]: "
  110. read -r swapSize
  111. [[ -z $swapSize ]] && swapSize=$defaultSwap
  112. # Determine if we should LUKS encrypt the disk.
  113. luks="false"
  114. chooseYN "Use LUKS Encryption? [N/y]"
  115. if [[ "$CHOICE" == "y" ]]; then
  116. luks="true"
  117. # Get a password from the user, with confirmation to ensure
  118. # we are not setting a typo.
  119. while true; do
  120. echo -n "Enter your luks encryption passphrase: "
  121. read -r -s luksPasswd
  122. echo
  123. echo -n "Confirm your luks encryption passphrase: "
  124. read -r -s confirmLuksPasswd
  125. echo
  126. if [[ "$luksPasswd" == "$confirmLuksPasswd" ]]; then
  127. break
  128. fi
  129. echo "Passwords do not match, try again."
  130. done
  131. # Save the password to the tmpfs for disko to pick up during partitioning.
  132. echo "$luksPasswd" > /tmp/secret.key
  133. fi
  134. # Get username for the main user.
  135. echo -n "Main user name [$defaultName]: "
  136. read -r name
  137. [[ -z $name ]] && name=$defaultName
  138. # Get description for the main user.
  139. echo -n "Main user description [$defaultDescription]: "
  140. read -r description
  141. [[ -z $description ]] && description=$defaultDescription
  142. # Determine password for main user, verifying no typos.
  143. while true; do
  144. echo -n "Enter password for main user: "
  145. read -r -s mainPasswd
  146. echo
  147. echo -n "Confirm your password for main user: "
  148. read -r -s confirmMainPasswd
  149. echo
  150. if [[ "$mainPasswd" == "$confirmMainPasswd" ]]; then
  151. break
  152. fi
  153. echo "Passwords do not match, try again."
  154. done
  155. # Use mkpasswd to create a hashed password with the lastest
  156. # linux password hashing algorithm.
  157. password=$(mkpasswd "$mainPasswd")
  158. # Determine SSH keys to allow into the system.
  159. sshKeys=()
  160. while true; do
  161. echo "To exit loop, press enter."
  162. echo -n "Add ssh key (Github Username or ssh key): "
  163. read -r keyToAdd
  164. # If empty, exit loop as all keys were selected.
  165. [[ -z $keyToAdd ]] && break
  166. # If matches an ssh public key, add to list.
  167. if [[ "$keyToAdd" =~ ^ssh-.* ]]; then
  168. echo "Added key: $keyToAdd"
  169. sshKeys+=("$keyToAdd")
  170. continue
  171. fi
  172. # If is an username, check github for all keys and add them.
  173. if [[ "$keyToAdd" =~ ([a-zA-Z0-9]+) ]]; then
  174. githubUsername=${BASH_REMATCH[1]}
  175. while read -r key; do
  176. if [[ $key == "Not Found" ]]; then
  177. echo "Github user provided not found"
  178. continue
  179. fi
  180. echo "Adding key: $key"
  181. sshKeys+=("$key")
  182. done < <(curl -s -q "https://github.com/$githubUsername.keys")
  183. fi
  184. done
  185. # Determine if we want to autologin to the main user,
  186. # this may be desirable on full disk encrypted machines.
  187. autoLogin="false"
  188. chooseYN "Autologin to main user? [N/y]"
  189. if [[ "$CHOICE" == "y" ]]; then
  190. autoLogin="true"
  191. fi
  192. # Get git name.
  193. echo -n "Git name [$defaultGitName]: "
  194. read -r gitName
  195. [[ -z $gitName ]] && gitName=$defaultGitName
  196. # Get git email.
  197. echo -n "Git email [$defaultGitEmail]: "
  198. read -r gitEmail
  199. [[ -z $gitEmail ]] && gitEmail=$defaultGitEmail
  200. # Generate settings.nix file with above choosen options.
  201. echo "Generating settings.nix:"
  202. cat <<EOF | tee "$nixosDir/settings.nix"
  203. rec {
  204. system = "x86_64-linux";
  205. timezone = "America/Chicago";
  206. locale = "en_US.UTF-8";
  207. packages = "${PACKAGES}";
  208. profile = "${PROFILE}";
  209. hostId = (builtins.substring 0 8 (builtins.readFile "/etc/machine-id"));
  210. hostName = "${hostName}";
  211. videoDrivers = "${videoDrivers}";
  212. disk = {
  213. device = "${disk}";
  214. swapSize = "${swapSize}";
  215. luks = ${luks};
  216. };
  217. user = {
  218. name = "${name}";
  219. description = "${description}";
  220. hashedPassword = "${password}";
  221. openssh.authorizedKeys.keys = [$(printf ' "%s"' "${sshKeys[@]}") ];
  222. autoLogin = ${autoLogin};
  223. };
  224. root = {
  225. hashedPassword = user.hashedPassword;
  226. openssh.authorizedKeys.keys = user.openssh.authorizedKeys.keys;
  227. };
  228. git = {
  229. name = "${gitName}";
  230. email = "${gitEmail}";
  231. };
  232. networkmanager.profiles = {};
  233. }
  234. EOF
  235. # Generate hardware-configuration.nix without filesystems as we use the disko partitoning flake.
  236. echo
  237. echo "Generating hardware-configuration.nix"
  238. nixos-generate-config --no-filesystems --show-hardware-config | tee "$nixosDir/hardware-configuration.nix"