Archived
Compare commits
5
Commits
9db659d54e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cae8a2fcd | ||
|
|
8d6ad63094 | ||
|
|
dd2d3b5914 | ||
|
|
0c70c63371 | ||
|
|
5a7bdea454 |
No files matched your search
@@ -4,10 +4,10 @@ This repository builds a custom NixOS installer environment that can automatical
|
||||
install any host exposed by a separate NixOS flake.
|
||||
|
||||
The installer provides a small NixOS netboot/install environment with SSH access,
|
||||
Git support, and an interactive installation script. When the `nixos` user logs
|
||||
in, it runs `/etc/auto-install.sh`, discovers available hosts from the target
|
||||
flake, allows the operator to choose a system profile, applies the matching
|
||||
Disko storage configuration, installs NixOS, and reboots.
|
||||
Git support, and an interactive installation script. Logging in as any user
|
||||
(root or `nixos`) runs `/etc/auto-install.sh`, discovers available hosts from
|
||||
the target flake, allows the operator to choose a system profile, applies the
|
||||
matching Disko storage configuration, installs NixOS, and reboots.
|
||||
|
||||
The installer is designed for a mixed environment where different machines may
|
||||
have different storage layouts, including Proxmox VMs, Linode instances, and
|
||||
@@ -18,17 +18,35 @@ defined by each host's NixOS configuration using Disko.
|
||||
|
||||
* `flake.nix`
|
||||
|
||||
* Defines the installer build outputs.
|
||||
* Builds an `install-iso` image using `nixos-generators`.
|
||||
* Defines the installer build outputs directly via `nixosSystem` (no
|
||||
external `nixos-generators` dependency).
|
||||
* `nixosConfigurations.installer` — ISO/netboot installer image.
|
||||
* `nixosConfigurations.proxmox-lxc` — Proxmox LXC-based installer image.
|
||||
* Builds netboot kernel, initrd, and iPXE scripts.
|
||||
|
||||
* `common.nix`
|
||||
|
||||
* Shared by every installer target (imported by both `installer.nix`
|
||||
and `proxmox-lxc.nix`).
|
||||
* Enables SSH access, configures Git credentials for the target flake
|
||||
repository, creates the `nixos` and `root` users.
|
||||
* Provides the generated `/etc/auto-install.sh` installation script and
|
||||
the `programs.bash.loginShellInit` hook that runs it on login.
|
||||
|
||||
* `installer.nix`
|
||||
|
||||
* Defines the installer environment.
|
||||
* Enables SSH access.
|
||||
* Configures Git access to the target flake repository.
|
||||
* Creates the `nixos` and `root` users.
|
||||
* Provides the generated `/etc/auto-install.sh` installation script.
|
||||
* ISO/netboot-specific: imports the stock `installation-cd-minimal.nix`
|
||||
module plus `common.nix`.
|
||||
|
||||
* `proxmox-lxc.nix`
|
||||
|
||||
* Proxmox LXC-specific: disables bootloader options (containers don't
|
||||
need one) plus `common.nix`.
|
||||
|
||||
* `scripts/prepare-host-key.sh`
|
||||
|
||||
* Admin-workstation pre-flight tool — see "Creating a New Machine"
|
||||
below.
|
||||
|
||||
The installed system configuration lives in a separate NixOS flake. Each target
|
||||
host is exposed through:
|
||||
@@ -53,41 +71,26 @@ The target machine requires:
|
||||
* A disk defined by its Disko configuration.
|
||||
* SSH access or local console access.
|
||||
|
||||
## Build The Installer ISO
|
||||
## Build Targets
|
||||
|
||||
From the repository root:
|
||||
Four packages, built directly from `nixosConfigurations.*` via
|
||||
`system.build.*` (no `nixos-generators` dependency):
|
||||
|
||||
```sh
|
||||
nix build
|
||||
nix build .#iso # installer ISO/netboot image (nixosConfigurations.installer)
|
||||
nix build .#lxc # Proxmox LXC installer tarball (nixosConfigurations.proxmox-lxc)
|
||||
nix build .#pxe # netboot-ipxe + netboot-initrd + netboot-kernel, bundled
|
||||
nix build .#all # all three of the above, bundled
|
||||
```
|
||||
|
||||
The generated installer ISO will be available through the `result` symlink.
|
||||
|
||||
The ISO is generated using:
|
||||
|
||||
```nix
|
||||
nixos-generators.nixosGenerate {
|
||||
format = "install-iso";
|
||||
}
|
||||
```
|
||||
|
||||
## Build Netboot Components
|
||||
|
||||
The flake also exposes netboot components:
|
||||
|
||||
```sh
|
||||
nix build .#netboot-ipxe
|
||||
nix build .#netboot-initrd
|
||||
nix build .#netboot-kernel
|
||||
```
|
||||
|
||||
These can be used to PXE boot the installer environment.
|
||||
Each one is available through the `result` symlink afterward (`result/iso/`,
|
||||
`result/tarball/`, or the bundle's subdirectories for `pxe`/`all`).
|
||||
|
||||
## Use The Installer
|
||||
|
||||
1. Boot the generated ISO or netboot environment on the target machine.
|
||||
|
||||
2. Log in as the `nixos` user.
|
||||
2. Log in as `root` or `nixos` (over SSH or local console).
|
||||
|
||||
3. The login shell launches:
|
||||
|
||||
@@ -200,9 +203,9 @@ The generated `/etc/auto-install.sh` performs the following steps:
|
||||
6. Prepare `/mnt` using the Disko-generated mount configuration.
|
||||
|
||||
7. Check for a pre-seeded SSH host key at `/root/host-keys/<host>_ssh_host_ed25519_key`
|
||||
and install it to `/mnt/etc/ssh/` if present (see "Pre-Seeding SSH Host
|
||||
Keys for sops-nix" below) — prompts for confirmation before continuing
|
||||
without one.
|
||||
and install it to `/mnt/etc/ssh/` if present (see "Creating a New
|
||||
Machine" below) — prompts for confirmation before continuing without
|
||||
one.
|
||||
|
||||
8. Install NixOS:
|
||||
|
||||
@@ -216,42 +219,97 @@ The generated `/etc/auto-install.sh` performs the following steps:
|
||||
|
||||
10. Reboot.
|
||||
|
||||
## Pre-Seeding SSH Host Keys for sops-nix
|
||||
## Creating a New Machine
|
||||
|
||||
Full walkthrough for installing a new host, including the sops-nix
|
||||
pre-seeding step. Do this instead of jumping straight to "Use The
|
||||
Installer" whenever the target host consumes any secret managed by the
|
||||
`nixos` flake's sops-nix setup (as of this writing, that's every host —
|
||||
`modules/common/configuration.nix` puts the root/nixos password hash and
|
||||
the GitHub token behind sops-nix for all of them).
|
||||
|
||||
### Why the extra step exists
|
||||
|
||||
The `nixos` flake manages secrets with sops-nix, using an age key derived
|
||||
from each host's own `/etc/ssh/ssh_host_ed25519_key`. That key is normally
|
||||
generated by a systemd service (`sshd-keygen`) the first time a host boots
|
||||
— but sops-nix decrypts secrets (including the login password) earlier
|
||||
than that, during system *activation*, which runs before systemd starts
|
||||
pursuing the target that `sshd-keygen` is gated behind. On a genuinely
|
||||
fresh install, this means secrets fail to decrypt on the very first boot
|
||||
unless the host key already exists beforehand.
|
||||
— but sops-nix decrypts secrets during system *activation*, which runs
|
||||
**before** systemd starts pursuing the target `sshd-keygen` is gated
|
||||
behind. On a genuinely fresh install, that means secrets — including the
|
||||
login password — fail to decrypt on the very first boot unless the host
|
||||
key already exists beforehand. Pre-seeding it is what fixes that.
|
||||
|
||||
To avoid this, generate the key ahead of time and register it with the
|
||||
`nixos` flake's sops-nix setup **before** installing:
|
||||
Machines that don't consume any sops-nix secret don't strictly need this,
|
||||
but since the shared password hash currently applies to every host, treat
|
||||
it as required unless you've specifically confirmed otherwise.
|
||||
|
||||
```sh
|
||||
./scripts/prepare-host-key.sh <hostname> [path-to-nixos-repo]
|
||||
```
|
||||
### Steps
|
||||
|
||||
This generates `host-keys/<hostname>_ssh_host_ed25519_key(.pub)` locally
|
||||
and prints the exact steps to add its derived age key to `nixos/.sops.yaml`,
|
||||
re-encrypt the relevant `secrets/*.yaml` files with `sops updatekeys`, and
|
||||
commit + push the `nixos` repo.
|
||||
**1. On your admin workstation** (the machine with `~/.config/sops/age/keys.txt`
|
||||
from the `nixos` repo's sops-nix setup), decide the new machine's flake
|
||||
target name (`<platform>-<buildtype>`, e.g. `proxmox-server`) and
|
||||
generate + register its host key:
|
||||
|
||||
Once that's done and the target machine is booted into the installer,
|
||||
copy the generated key onto it before running (or continuing)
|
||||
`/etc/auto-install.sh`:
|
||||
```sh
|
||||
./scripts/prepare-host-key.sh <hostname> [path-to-nixos-repo]
|
||||
```
|
||||
|
||||
```sh
|
||||
scp host-keys/<hostname>_ssh_host_ed25519_key{,.pub} root@<target-ip>:/root/host-keys/
|
||||
```
|
||||
This generates `host-keys/<hostname>_ssh_host_ed25519_key(.pub)`
|
||||
locally and prints:
|
||||
|
||||
`auto-install.sh` checks for this file automatically and installs it to
|
||||
the target's `/mnt/etc/ssh/` before running `nixos-install`. If it's
|
||||
missing, the script warns and asks for confirmation before continuing —
|
||||
useful for hosts that don't consume any sops-nix secrets, but skipping it
|
||||
for a host that does will lock secrets out of decrypting on first boot.
|
||||
- the line to add under `keys:` in `nixos/.sops.yaml`
|
||||
- which `creation_rules` key_groups to add it to (`secrets/common.yaml`
|
||||
always; a per-host `secrets/<hostname>.yaml` too if this build type
|
||||
has its own secrets, same pattern as `nix-cache`/`server`)
|
||||
|
||||
**2. Edit `nixos/.sops.yaml`** by hand with the printed snippet, then
|
||||
re-encrypt every secrets file you added the new host to:
|
||||
|
||||
```sh
|
||||
nix-shell -p sops --run 'sops updatekeys nixos/secrets/common.yaml'
|
||||
```
|
||||
|
||||
**3. Commit and push the `nixos` repo.** The flake build the installer
|
||||
uses has to see the new recipient before you install, or decryption
|
||||
will fail on first boot regardless of step 4 below.
|
||||
|
||||
**4. Build and boot the installer image** on the target machine (ISO,
|
||||
netboot, or Proxmox LXC — see "Build Targets" above).
|
||||
|
||||
**5. Copy the generated private key onto the live installer environment**
|
||||
once it's reachable over SSH:
|
||||
|
||||
```sh
|
||||
scp host-keys/<hostname>_ssh_host_ed25519_key{,.pub} root@<target-ip>:/root/host-keys/
|
||||
```
|
||||
|
||||
**6. Log in** (as `root` or `nixos`) to trigger `/etc/auto-install.sh`
|
||||
automatically, or run it manually if it doesn't (see Troubleshooting).
|
||||
Select `<hostname>`'s flake target from the menu and confirm. The
|
||||
script will:
|
||||
|
||||
- run Disko to format and mount `/mnt`
|
||||
- find the key you copied in at `/root/host-keys/<hostname>_ssh_host_ed25519_key`
|
||||
and install it to `/mnt/etc/ssh/` — if it's missing, the script
|
||||
warns and asks for confirmation before continuing without it (fine
|
||||
for a host with no sops-nix secrets; for any other host, continuing
|
||||
anyway means the machine will boot with no working login password)
|
||||
- run `nixos-install` and reboot
|
||||
|
||||
**7. Verify after reboot.** SSH into the new machine and confirm secrets
|
||||
actually decrypted:
|
||||
|
||||
```sh
|
||||
ls /run/secrets/
|
||||
```
|
||||
|
||||
If that's empty or login fails, the most likely cause is the host's
|
||||
age key not being in `.sops.yaml` (or not re-encrypted into the
|
||||
secrets file it needs) at the time `nixos-install` ran — re-check
|
||||
steps 1–3, fix `nixos/.sops.yaml`/`secrets/*.yaml`, push, then
|
||||
re-run `nixos-install --flake <flake>#<hostname> --no-root-password`
|
||||
from a rescue/live environment against the existing `/mnt` (or just
|
||||
redo the install).
|
||||
|
||||
`host-keys/` is gitignored — never commit private key material.
|
||||
|
||||
@@ -285,11 +343,11 @@ The installer currently assumes:
|
||||
|
||||
* The target host is responsible for defining its own storage layout.
|
||||
|
||||
Change these values in `installer.nix` if your environment differs.
|
||||
Change these values in `common.nix` if your environment differs.
|
||||
|
||||
## Security Notes
|
||||
|
||||
`installer.nix` currently contains:
|
||||
`common.nix` currently contains:
|
||||
|
||||
* Git credentials
|
||||
* Password hashes
|
||||
@@ -369,8 +427,18 @@ Verify:
|
||||
|
||||
### Installer does not start automatically
|
||||
|
||||
Run manually:
|
||||
It's triggered by `programs.bash.loginShellInit` (rendered into
|
||||
`/etc/profile`), which only fires for genuine login shells. If you're not
|
||||
seeing it — e.g. you attached to an existing shell rather than logging in
|
||||
fresh — run it manually:
|
||||
|
||||
```sh
|
||||
sudo /etc/auto-install.sh
|
||||
```
|
||||
|
||||
### Login succeeds but the machine has no working password after install
|
||||
|
||||
The new host's sops-nix secrets didn't decrypt on first boot — almost
|
||||
always because its age key wasn't registered in `nixos/.sops.yaml` and
|
||||
re-encrypted into the secrets file(s) it needs before `nixos-install` ran.
|
||||
See "Creating a New Machine" above, specifically step 7.
|
||||
+28
-1
@@ -5,6 +5,24 @@
|
||||
|
||||
time.timeZone = "Australia/Brisbane";
|
||||
|
||||
# Without this, the installer only ever sees cache.nixos.org, which
|
||||
# doesn't carry sops-install-secrets (it's built straight from the
|
||||
# sops-nix flake's own Go source, not part of nixpkgs) — every install
|
||||
# would otherwise compile it from scratch, which is what ran an 8GB LXC
|
||||
# container's disk out of space. Push a built copy to nix-cache once
|
||||
# (from a machine with real disk headroom) and every future install,
|
||||
# of any type, fetches instead of rebuilding.
|
||||
nix.settings = {
|
||||
substituters = [
|
||||
"http://nix-cache"
|
||||
"https://cache.nixos.org/"
|
||||
];
|
||||
trusted-public-keys = [
|
||||
"cache.local-1:usoWYanY3Kpq2+kDIS2nhWoLZiRxanmdysdzqCFBHW4="
|
||||
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
|
||||
];
|
||||
};
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
git
|
||||
curl
|
||||
@@ -120,7 +138,16 @@ if nix eval --refresh --json --extra-experimental-features "flakes nix-command"
|
||||
disko --mode destroy,format,mount \
|
||||
--flake "''${FLAKE_BASE_URL}#''${choice}" --yes
|
||||
else
|
||||
echo "Selected host has no Disko configuration."
|
||||
# No raw disk to partition — true for every LXC container, which has
|
||||
# no block device visible from inside it, only its already-mounted
|
||||
# root filesystem. nixos-install defaults to installing into /mnt; if
|
||||
# nothing points /mnt at the real root, the new system gets built and
|
||||
# written to a disconnected empty directory, never actually becomes
|
||||
# bootable, and the container silently keeps running this installer
|
||||
# environment forever. Bind-mount / onto /mnt so the install actually
|
||||
# lands on the filesystem this container boots from.
|
||||
echo "Selected host has no Disko configuration — installing in place via bind mount."
|
||||
mount --bind / /mnt
|
||||
fi
|
||||
|
||||
# sops-nix derives this host's decryption key from its own SSH host key
|
||||
|
||||
@@ -84,28 +84,26 @@
|
||||
# nix build .#name
|
||||
#
|
||||
|
||||
packages.${system} = {
|
||||
packages.${system} = rec {
|
||||
|
||||
netboot-ipxe =
|
||||
netbootSystem.config.system.build.netbootIpxeScript;
|
||||
iso =
|
||||
self.nixosConfigurations.installer.config.system.build.isoImage;
|
||||
|
||||
lxc =
|
||||
self.nixosConfigurations.proxmox-lxc.config.system.build.tarball;
|
||||
|
||||
netboot-initrd =
|
||||
netbootSystem.config.system.build.netbootRamdisk;
|
||||
pxe =
|
||||
pkgs.linkFarm "pxe" [
|
||||
{ name = "netboot.ipxe"; path = netbootSystem.config.system.build.netbootIpxeScript; }
|
||||
{ name = "initrd"; path = netbootSystem.config.system.build.netbootRamdisk; }
|
||||
{ name = "kernel"; path = netbootSystem.config.system.build.kernel; }
|
||||
];
|
||||
|
||||
|
||||
netboot-kernel =
|
||||
netbootSystem.config.system.build.kernel;
|
||||
images =
|
||||
pkgs.linkFarm "images" [
|
||||
{
|
||||
name = "installer-iso";
|
||||
path = self.nixosConfigurations.installer.config.system.build.isoImage;
|
||||
}
|
||||
{
|
||||
name = "proxmox-lxc";
|
||||
path = self.nixosConfigurations.proxmox-lxc.config.system.build.tarball;
|
||||
}
|
||||
all =
|
||||
pkgs.linkFarm "all" [
|
||||
{ name = "iso"; path = iso; }
|
||||
{ name = "lxc"; path = lxc; }
|
||||
{ name = "pxe"; path = pxe; }
|
||||
];
|
||||
};
|
||||
|
||||
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
{ pkgs, ... }:
|
||||
{ modulesPath, ... }:
|
||||
|
||||
{
|
||||
imports = [
|
||||
./common.nix
|
||||
(modulesPath + "/virtualisation/proxmox-lxc.nix")
|
||||
];
|
||||
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ if [[ ! -f "$sops_yaml" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
keydir="$(dirname "$0")/../host-keys"
|
||||
keydir="$(cd "$(dirname "$0")/.." && pwd)/host-keys"
|
||||
mkdir -p "$keydir"
|
||||
keyfile="${keydir}/${hostname}_ssh_host_ed25519_key"
|
||||
|
||||
@@ -35,16 +35,16 @@ if [[ -f "$keyfile" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
nix-shell -p openssh ssh-to-age --run "
|
||||
set -euo pipefail
|
||||
ssh-keygen -t ed25519 -N '' -C '${hostname}' -f '${keyfile}' >/dev/null
|
||||
age_pub=\$(ssh-to-age -i '${keyfile}.pub')
|
||||
cat <<EOF
|
||||
nix-shell -p openssh --run "ssh-keygen -t ed25519 -N '' -C '${hostname}' -f '${keyfile}'" >/dev/null
|
||||
|
||||
age_pub="$(nix-shell -p ssh-to-age --run "ssh-to-age -i '${keyfile}.pub'")"
|
||||
|
||||
cat <<EOF
|
||||
|
||||
Generated: ${keyfile}(.pub)
|
||||
|
||||
=== 1. Add this line under `keys:` in ${sops_yaml} ===
|
||||
- &${hostname} \${age_pub}
|
||||
=== 1. Add this line under keys: in ${sops_yaml} ===
|
||||
- &${hostname} ${age_pub}
|
||||
|
||||
=== 2. Add *${hostname} to whichever creation_rules key_groups this host needs ===
|
||||
(e.g. secrets/common.yaml always; add a per-host secrets/${hostname}.yaml
|
||||
@@ -62,4 +62,3 @@ Generated: ${keyfile}(.pub)
|
||||
Then continue with /etc/auto-install.sh as normal — it will find the
|
||||
pre-seeded key and install it before running nixos-install.
|
||||
EOF
|
||||
"
|
||||
Reference in new issue
Block a user