Install NixOS on Hetzner Online: Difference between revisions

imported>Montchr
m Bash syntax highlighting for nixos-generators section
imported>Montchr
Use simplified Nix installation instructions from nixos-install-scripts
Line 77: Line 77:
== Bootstrap from the Rescue System ==
== Bootstrap from the Rescue System ==


Here are some quick notes on how to bootstrap. Inspiration comes from https://github.com/ofborg/infrastructure/commit/0712a5cf871b7a6d2fbbd2df539d3cd90ab8fa1f
Here are some quick notes on how to bootstrap.
 
The nixos-install-scripts repo may also be a valuable resource:
 
https://github.com/nix-community/nixos-install-scripts/tree/master/hosters/hetzner-dedicated
 
Otherwise, inspiration for the kexec approach below comes from https://github.com/ofborg/infrastructure/commit/0712a5cf871b7a6d2fbbd2df539d3cd90ab8fa1f
and https://github.com/andir/infra/tree/master/bootstrap
and https://github.com/andir/infra/tree/master/bootstrap


The main principle is that we will go from: Rescue system, kexec into a NixOS system, finally install the system.
The main principle is that we will go from: Rescue system, install Nix, kexec into a NixOS system, finally install the system.


First, reboot the machine in Rescue mode. Make sure to select your SSH public key. SSH into the machine:
First, reboot the machine in Rescue mode. Make sure to select your SSH public key. SSH into the machine:


<syntaxHighlight lang=bash>
<syntaxHighlight lang=bash>
# Create a user, because the nix installer disallows running as root
# Let root run the nix installer
adduser --add_extra_groups=sudo foo
mkdir -p /etc/nix
install -d -m700 -o foo /nix
su - foo
 
# Pre-configure Nix
echo "build-users-group =" > /etc/nix/nix.conf
echo "build-users-group =" > /etc/nix/nix.conf


# Install Nix
# Install Nix in single-user mode
sh <(curl -L https://nixos.org/nix/install) --daemon
curl -L https://nixos.org/nix/install | sh
 
. $HOME/.nix-profile/etc/profile.d/nix.sh
# As mentioned in the installer, you'll need to log out
# (back to root) and start a new session
exit
su - foo
 
# Optional: Enable flakes
echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf


# Install nixos-generators
# Install nixos-generators
# This might take a while, so the verbose flag `-v` is included to monitor progress
# This might take a while, so the verbose flag `-v` is included to monitor progress
nix-env -f https://github.com/nix-community/nixos-generators/archive/master.tar.gz -i -v
nix-env -f https://github.com/nix-community/nixos-generators/archive/master.tar.gz -i -v
# Or with flakes:
nix profile install github:nix-community/nixos-generators


# Create a initial config, just to kexec into
# Create a initial config, just to kexec into
cat <<EOF > /home/foo/config.nix
cat <<EOF > /root/config.nix
{
{
   services.openssh.enable = true;
   services.openssh.enable = true;
   users.users.root.openssh.authorizedKeys.keys = [
   users.users.root.openssh.authorizedKeys.keys = [
     # Replace with your public key
     # Replace with your public key
     "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGB1Pog97SWdV2UEA40V+3bML+lSZXEd48zCRlS/eGbY3rsXfgUXb5FIBulN9cET9g0OOAKeCZBR1Y2xXofiHDYkhk298rHDuir6cINuoMGUO7VsygUfKguBy63QMPHYnJBE1h+6sQGu/3X9G2o/0Ys2J+lZv4+N7Hqolhbg/Cu6/LUCsJM/udqTVwJGEqszDWPtuuTAIS6utB1QdL9EZT5WBb1nsNyHnIlCnoDKZvrrO9kM0FGKhjJG2skd3+NqmLhYIDhRhZvRnL9c8U8uozjbtj/N8L/2VCRzgzKmvu0Y1cZMWeAAdyqG6LoyE7xGO+SF4Vz1x6JjS9VxnZipIB zimbatm@nixos"
     "ssh-rsa AAAA..."
   ];
   ];
}
}
Line 122: Line 115:


# Generate the kexec script
# Generate the kexec script
nixos-generate -o /home/foo/result  -f kexec-bundle -c /home/foo/config.nix
nixos-generate -o /root/result  -f kexec-bundle -c /root/config.nix
 
# Switch back to root
exit


# Switch to the new system
# Switch to the new system
/home/foo/result
/root/result
</syntaxHighlight>
</syntaxHighlight>