Update scripts to better support remote installs using nixos-anywhere.

This commit is contained in:
James Coleman 2024-12-09 20:23:13 -06:00
parent 05035f82bd
commit a7b6cae925
5 changed files with 151 additions and 24 deletions

19
LICENSE Normal file
View File

@ -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.

View File

@ -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. 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 ## 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 ```bash
mkdir /mnt/usb mkdir /mnt/usb
@ -16,26 +41,22 @@ swapon /mnt/usn/swap
mount -o remount,size=20G,noatime /nix/.rw-store mount -o remount,size=20G,noatime /nix/.rw-store
``` ```
### The install process. #### The install process.
- clone and enter the nixos repo.
After setting up the extra swap space, clone and enter the nixos repo.
```bash ```bash
nix-shell -p git nix-shell -p git
git clone --recursive https://github.com/GRMrGecko/nixos.git git clone --recursive https://github.com/GRMrGecko/nixos.git
cd nixos/ cd nixos/
``` ```
- Configure the machine to your liking.
After you get into the repo, configure the machine to your liking.
```bash ```bash
./configure.sh ./configure.sh
``` ```
- Install. You can define a tmpdir as the USB drive with `TMPDIR=/mnt/usb` if you want to reduce load on RAM.
After configuring, install. You can define a tmpdir as the USB drive with `TMPDIR=/mnt/usb` if you want to reduce load on RAM.
```bash ```bash
./install.sh --disk main /dev/sda ./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 ```bash
nix-shell -p rsync nix-shell -p rsync
mount -o compress=zstd /dev/mapper/crypted /mnt/hdd mount -o compress=zstd /dev/mapper/crypted /mnt/hdd

View File

@ -68,9 +68,25 @@ chooseYN() {
done 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. # Determine video drivers based on PCI devices.
videoDrivers="unknown" videoDrivers="unknown"
pciRaw=$(lspci | grep -E 'VGA') pciRaw=$($sshCmd lspci | grep -E 'VGA')
if [[ "$pciRaw" =~ QXL ]]; then if [[ "$pciRaw" =~ QXL ]]; then
videoDrivers="qxl" videoDrivers="qxl"
elif [[ "$pciRaw" =~ NVIDIA ]]; then elif [[ "$pciRaw" =~ NVIDIA ]]; then
@ -111,7 +127,7 @@ echo
echo "Select a disk from the list below:" echo "Select a disk from the list below:"
# List disks to allow a choice to be made without stopping # List disks to allow a choice to be made without stopping
# configuration and verifying available disks. # configuration and verifying available disks.
lsblk -o PATH,ID-LINK,SIZE -t $sshCmd lsblk -o PATH,ID-LINK,SIZE -t
echo echo
echo -n "Choose disk (/dev/disk/by-id/{ID-LINK}) [$diskDefault]: " echo -n "Choose disk (/dev/disk/by-id/{ID-LINK}) [$diskDefault]: "
read -r disk read -r disk
@ -143,7 +159,7 @@ if [[ "$CHOICE" == "y" ]]; then
echo "Passwords do not match, try again." echo "Passwords do not match, try again."
done done
# Save the password to the tmpfs for disko to pick up during partitioning. # 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 fi
# Get username for the main user. # Get username for the main user.
@ -171,7 +187,7 @@ while true; do
done done
# Use mkpasswd to create a hashed password with the lastest # Use mkpasswd to create a hashed password with the lastest
# linux password hashing algorithm. # linux password hashing algorithm.
password=$(mkpasswd "$mainPasswd") password=$($sshCmd mkpasswd "\"$mainPasswd\"")
# Determine SSH keys to allow into the system. # Determine SSH keys to allow into the system.
sshKeys=() sshKeys=()
@ -231,7 +247,7 @@ rec {
locale = "en_US.UTF-8"; locale = "en_US.UTF-8";
packages = "${PACKAGES}"; packages = "${PACKAGES}";
profile = "${PROFILE}"; profile = "${PROFILE}";
hostId = (builtins.substring 0 8 (builtins.readFile "/etc/machine-id")); hostId = "$(tr -dc a-f0-9 </dev/urandom | head -c 8)";
hostName = "${hostName}"; hostName = "${hostName}";
videoDrivers = "${videoDrivers}"; videoDrivers = "${videoDrivers}";
disk = { disk = {
@ -261,4 +277,4 @@ EOF
# Generate hardware-configuration.nix without filesystems as we use the disko partitoning flake. # Generate hardware-configuration.nix without filesystems as we use the disko partitoning flake.
echo echo
echo "Generating hardware-configuration.nix" echo "Generating hardware-configuration.nix"
nixos-generate-config --no-filesystems --show-hardware-config | tee "$nixosDir/hardware-configuration.nix" $sshCmd nixos-generate-config --no-filesystems --show-hardware-config | tee "$nixosDir/hardware-configuration.nix"

View File

@ -4,10 +4,30 @@
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit
nixosDir=$(pwd) 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. # Get short hostname to work with host specific configurations.
host=$(hostname -s) host=""
if [[ -n $nixHostOverride ]]; then if [[ "$(grep hostName settings.nix)" =~ \"(.*)\" ]]; then
host=$nixHostOverride host=${BASH_REMATCH[1]}
fi
# If hostname wasn't found, try using the hostname command.
if [[ -z $host ]]; then
host=$($sshCmd hostname -s)
fi fi
# Confirm host configuration is available; If not, we should not continue. # 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" host="default"
fi fi
# Install NixOS. # If remote address provided, use nixos-anywhere.
# shellcheck disable=SC2068 if [[ -n $remoteAddr ]]; then
nix --extra-experimental-features 'nix-command flakes' run 'github:nix-community/disko#disko-install' -- --flake "path:$nixosDir/#$host" $@ 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

View File

@ -4,5 +4,43 @@
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || exit
nixosDir=$(pwd) 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. # Sync configuration via rsync.
rsync -av --delete --exclude settings.nix --exclude hardware-configuration.nix "$nixosDir/" "$1:nixos/" eval rsync -av --delete "$excludes" "'$nixosDir/'" "'$remoteAddr:nixos/'"