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.

262 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
  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 -n "Confirm your password for main user: "
  147. read -r -s confirmMainPasswd
  148. if [[ "$mainPasswd" == "$confirmMainPasswd" ]]; then
  149. break
  150. fi
  151. echo "Passwords do not match, try again."
  152. done
  153. # Use mkpasswd to create a hashed password with the lastest
  154. # linux password hashing algorithm.
  155. password=$(mkpasswd "$mainPasswd")
  156. # Determine SSH keys to allow into the system.
  157. sshKeys=()
  158. while true; do
  159. echo "To exit loop, press enter."
  160. echo -n "Add ssh key (Github Username or ssh key): "
  161. read -r keyToAdd
  162. # If empty, exit loop as all keys were selected.
  163. [[ -z $keyToAdd ]] && break
  164. # If matches an ssh public key, add to list.
  165. if [[ "$keyToAdd" =~ ^ssh-.* ]]; then
  166. echo "Added key: $keyToAdd"
  167. sshKeys+=("$keyToAdd")
  168. continue
  169. fi
  170. # If is an username, check github for all keys and add them.
  171. if [[ "$keyToAdd" =~ ([a-zA-Z0-9]+) ]]; then
  172. githubUsername=${BASH_REMATCH[1]}
  173. while read -r key; do
  174. if [[ $key == "Not Found" ]]; then
  175. echo "Github user provided not found"
  176. continue
  177. fi
  178. echo "Adding key: $key"
  179. sshKeys+=("$key")
  180. done < <(curl -s -q "https://github.com/$githubUsername.keys")
  181. fi
  182. done
  183. # Determine if we want to autologin to the main user,
  184. # this may be desirable on full disk encrypted machines.
  185. autoLogin="false"
  186. chooseYN "Autologin to main user? [N/y]"
  187. if [[ "$CHOICE" == "y" ]]; then
  188. autoLogin="true"
  189. fi
  190. # Get git name.
  191. echo -n "Git name [$defaultGitName]: "
  192. read -r gitName
  193. [[ -z $gitName ]] && gitName=$defaultGitName me
  194. # Get git email.
  195. echo -n "Git email [$defaultGitEmail]: "
  196. read -r gitEmail
  197. [[ -z $gitEmail ]] && gitEmail=$defaultGitEmail
  198. # Generate settings.nix file with above choosen options.
  199. echo "Generating settings.nix:"
  200. cat <<EOF | tee "$nixosDir/settings.nix"
  201. rec {
  202. system = "x86_64-linux";
  203. timezone = "America/Chicago";
  204. locale = "en_US.UTF-8";
  205. packages = "${PACKAGES}";
  206. profile = "${PROFILE}";
  207. hostId = (builtins.substring 0 8 (builtins.readFile "/etc/machine-id"));
  208. hostName = "${hostName}";
  209. videoDrivers = "${videoDrivers}";
  210. disk = {
  211. device = "${disk}";
  212. swapSize = "${swapSize}";
  213. luks = ${luks};
  214. };
  215. user = {
  216. name = "${name}";
  217. description = "${description}";
  218. hashedPassword = "${password}";
  219. openssh.authorizedKeys.keys = [$(printf ' "%s"' "${sshKeys[@]}") ];
  220. autoLogin = ${autoLogin};
  221. };
  222. root = {
  223. hashedPassword = user.hashedPassword;
  224. openssh.authorizedKeys.keys = user.openssh.authorizedKeys.keys;
  225. };
  226. git = {
  227. name = "${gitName}";
  228. email = "${gitEmail}";
  229. };
  230. networkmanager.profiles = {};
  231. }
  232. EOF
  233. # Generate hardware-configuration.nix without filesystems as we use the disko partitoning flake.
  234. echo
  235. echo "Generating hardware-configuration.nix"
  236. nixos-generate-config --no-filesystems --show-hardware-config | tee "$nixosDir/hardware-configuration.nix"