From a7b6cae92578f893ddc3826c8e131c9475f7cb41 Mon Sep 17 00:00:00 2001 From: James Coleman Date: Mon, 9 Dec 2024 20:23:13 -0600 Subject: [PATCH] Update scripts to better support remote installs using nixos-anywhere. --- LICENSE | 19 +++++++++++++++++++ README.md | 43 ++++++++++++++++++++++++++++++++----------- configure.sh | 28 ++++++++++++++++++++++------ install.sh | 45 +++++++++++++++++++++++++++++++++++++++------ rsync.sh | 40 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3c9e3c8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 44bb3e6..2573b18 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,34 @@ These are my configurations for nixos. You are free to use it, however it may be best for you to fork and make your own. ## Installing -In my experience, you need a larger disk size for the nix store on the installer than is created. As such, I use a swap file/drive, recommended separate drive from the one being installed to. +You can install locally, or remote using [nixos anywhere](https://github.com/nix-community/nixos-anywhere). My suggestion is to use the remote method if possible. -### Swap example. +### NixOS Anywhere +- Download this repo. +```bash +nix-shell -p git +git clone --recursive https://github.com/GRMrGecko/nixos.git +cd nixos/ +``` +- Ensure you have ssh acces with keys. +- Configure the configuration for the remote machine, entering root@IPADDR for the system you're configuring. +```bash +./configure.sh +``` +- Run the installer, entering root@IPADDR for the system you're installing on. +```bash +./install.sh +``` +- After first boot, copy over the nixos dir to make it easy to rebuild and update. +```bash +./rsync.sh --include-settings user@IPADDR +``` + +### Install on local system + +#### Swap example +On systems with a small amount of RAM, you may wish to add an USB drive and attach it as a virtual swap. +This is a small example of how to do so, you will need to update to fit your sitation. ```bash mkdir /mnt/usb @@ -16,26 +41,22 @@ swapon /mnt/usn/swap mount -o remount,size=20G,noatime /nix/.rw-store ``` -### The install process. - -After setting up the extra swap space, clone and enter the nixos repo. +#### The install process. +- clone and enter the nixos repo. ```bash nix-shell -p git git clone --recursive https://github.com/GRMrGecko/nixos.git cd nixos/ ``` - -After you get into the repo, configure the machine to your liking. +- Configure the machine to your liking. ```bash ./configure.sh ``` - -After configuring, install. You can define a tmpdir as the USB drive with `TMPDIR=/mnt/usb` if you want to reduce load on RAM. +- Install. You can define a tmpdir as the USB drive with `TMPDIR=/mnt/usb` if you want to reduce load on RAM. ```bash ./install.sh --disk main /dev/sda ``` - -After install is complete, you can then rsync the nixos dir to the user account on the install: +- After install is complete, you can then rsync the nixos dir to the user account on the install: ```bash nix-shell -p rsync mount -o compress=zstd /dev/mapper/crypted /mnt/hdd diff --git a/configure.sh b/configure.sh index 908c028..d6e12dd 100755 --- a/configure.sh +++ b/configure.sh @@ -68,9 +68,25 @@ chooseYN() { done } +remoteAddr="" +echo "If you are configuring a remote machine, ensure you have ssh access with keys." +echo -n "Configuring [local machine]: " +read -r CHOICE +if [[ -n $CHOICE ]]; then + remoteAddr="$CHOICE" +fi +sshCmd="" +if [[ -n $remoteAddr ]]; then + if ssh "$remoteAddr" /usr/bin/env true; then + sshCmd="ssh $remoteAddr" + else + echo "Unable to confirm connection to remote $remoteAddr" + fi +fi + # Determine video drivers based on PCI devices. videoDrivers="unknown" -pciRaw=$(lspci | grep -E 'VGA') +pciRaw=$($sshCmd lspci | grep -E 'VGA') if [[ "$pciRaw" =~ QXL ]]; then videoDrivers="qxl" elif [[ "$pciRaw" =~ NVIDIA ]]; then @@ -111,7 +127,7 @@ echo echo "Select a disk from the list below:" # List disks to allow a choice to be made without stopping # configuration and verifying available disks. -lsblk -o PATH,ID-LINK,SIZE -t +$sshCmd lsblk -o PATH,ID-LINK,SIZE -t echo echo -n "Choose disk (/dev/disk/by-id/{ID-LINK}) [$diskDefault]: " read -r disk @@ -143,7 +159,7 @@ if [[ "$CHOICE" == "y" ]]; then echo "Passwords do not match, try again." done # Save the password to the tmpfs for disko to pick up during partitioning. - echo "$luksPasswd" > /tmp/secret.key + echo -n "$luksPasswd" | $sshCmd dd of=/tmp/secret.key fi # Get username for the main user. @@ -171,7 +187,7 @@ while true; do done # Use mkpasswd to create a hashed password with the lastest # linux password hashing algorithm. -password=$(mkpasswd "$mainPasswd") +password=$($sshCmd mkpasswd "\"$mainPasswd\"") # Determine SSH keys to allow into the system. sshKeys=() @@ -231,7 +247,7 @@ rec { locale = "en_US.UTF-8"; packages = "${PACKAGES}"; profile = "${PROFILE}"; - hostId = (builtins.substring 0 8 (builtins.readFile "/etc/machine-id")); + hostId = "$(tr -dc a-f0-9 /dev/null || exit nixosDir=$(pwd) +remoteAddr="" +echo -n "Install on [local machine]: " +read -r CHOICE +if [[ -n $CHOICE ]]; then + remoteAddr="$CHOICE" +fi +sshCmd="" +if [[ -n $remoteAddr ]]; then + if ssh "$remoteAddr" /usr/bin/env true; then + sshCmd="ssh $remoteAddr" + else + echo "Unable to confirm connection to remote $remoteAddr" + fi +fi + # Get short hostname to work with host specific configurations. -host=$(hostname -s) -if [[ -n $nixHostOverride ]]; then - host=$nixHostOverride +host="" +if [[ "$(grep hostName settings.nix)" =~ \"(.*)\" ]]; then + host=${BASH_REMATCH[1]} +fi + +# If hostname wasn't found, try using the hostname command. +if [[ -z $host ]]; then + host=$($sshCmd hostname -s) fi # Confirm host configuration is available; If not, we should not continue. @@ -15,6 +35,19 @@ if ! grep -q "nixosConfigurations.$host " flake.nix; then host="default" fi -# Install NixOS. -# shellcheck disable=SC2068 -nix --extra-experimental-features 'nix-command flakes' run 'github:nix-community/disko#disko-install' -- --flake "path:$nixosDir/#$host" $@ +# If remote address provided, use nixos-anywhere. +if [[ -n $remoteAddr ]]; then + localArch=$(uname -m) + remoteArch=$($sshCmd uname -m) + if [[ "$localArch" != "$remoteArch" ]]; then + # shellcheck disable=SC2068 + nix --extra-experimental-features 'nix-command flakes' run 'github:nix-community/nixos-anywhere' -- --build-on-remote --flake "path:$nixosDir/#$host" "$remoteAddr" $@ + else + # shellcheck disable=SC2068 + nix --extra-experimental-features 'nix-command flakes' run 'github:nix-community/nixos-anywhere' -- --flake "path:$nixosDir/#$host" "$remoteAddr" $@ + fi +else + # Otherwise install with disko-install. + # shellcheck disable=SC2068 + nix --extra-experimental-features 'nix-command flakes' run 'github:nix-community/disko#disko-install' -- --flake "path:$nixosDir/#$host" $@ +fi diff --git a/rsync.sh b/rsync.sh index e7a13c3..405c736 100755 --- a/rsync.sh +++ b/rsync.sh @@ -4,5 +4,43 @@ cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit nixosDir=$(pwd) + +# Print the help for this command. +print_help() { + echo "NixOS Rsync" + echo + echo "Usage:" + echo "$0 [--help|--include-settings] {host}" + exit +} + +# Defaults +remoteAddr="" +excludes="--exclude settings.nix --exclude hardware-configuration.nix" + +# Parse provided arguments. +while (( $# > 0 )); do + case "$1" in + -h|h|help|--help) + print_help "$@" + ;; + -i|--include-settings) + excludes="" + shift + ;; + *) + remoteAddr="$1" + shift + ;; + esac +done + +# If no address provided, exit. +if [[ -z $remoteAddr ]]; then + echo "You must provide a host." + echo + print_help "$@" +fi + # Sync configuration via rsync. -rsync -av --delete --exclude settings.nix --exclude hardware-configuration.nix "$nixosDir/" "$1:nixos/" \ No newline at end of file +eval rsync -av --delete "$excludes" "'$nixosDir/'" "'$remoteAddr:nixos/'"