ebffe4e989
Flake lock file updates: • Updated input 'home-manager-unstable': 'github:nix-community/home-manager/04213d1ce4221f5d9b40bcee30706ce9a91d148d?narHash=sha256-NJPGK030Y3qETpWBhj9oobDQRbXdXOPxtu%2BYgGvZ84o%3D' (2024-09-22) → 'github:nix-community/home-manager/ffe2d07e771580a005e675108212597e5b367d2d?narHash=sha256-4/vacp3CwdGoPf8U4e/N8OsGYtO09WTcQK5FqYfJbKs%3D' (2024-09-26) • Updated input 'impermanence': 'github:nix-community/impermanence/63f4d0443e32b0dd7189001ee1894066765d18a5?narHash=sha256-4qWg9sNh5g1qPGO6d/GV2ktY%2BeDikkBTbWSg5/iD2nY%3D' (2024-09-07) → 'github:nix-community/impermanence/d0b38e550039a72aff896ee65b0918e975e6d48e?narHash=sha256-FA53of86DjFdeQzRDVtvgWF9o52rWK70VHGx0Y8fElQ%3D' (2024-09-29) • Updated input 'nix-index-database': 'github:nix-community/nix-index-database/c7515c2fdaf2e1f3f49856cef6cec95bb2138417?narHash=sha256-bPDZosnom0%2B02ywmMZAvmj7zvsQ6mVv/5kmvSgbTkaY%3D' (2024-09-22) → 'github:nix-community/nix-index-database/f9fdf8285690a351e8998f1e703ebdf9cdf51dee?narHash=sha256-YAePt2GldkkRJ08LvZNHcuS6shIVStj%2BK%2B1DZN3gbnM%3D' (2024-09-30) • Updated input 'nixos-hardware': 'github:NixOS/nixos-hardware/d0cb432a9d28218df11cbd77d984a2a46caeb5ac?narHash=sha256-19FNN5QT9Z11ZUMfftRplyNN%2B2PgcHKb3oq8KMW/hDA%3D' (2024-09-22) → 'github:NixOS/nixos-hardware/11c43c830e533dad1be527ecce379fcf994fbbb5?narHash=sha256-oKtfbQB1MBypqIyzkC8QCQcVGOa1soaXaGgcBIoh14o%3D' (2024-09-30) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/23cbb250f3bf4f516a2d0bf03c51a30900848075?narHash=sha256-8fnFlXBgM/uSvBlLWjZ0Z0sOdRBesyNdH0%2BesxqizGc%3D' (2024-09-22) → 'github:NixOS/nixpkgs/fbca5e745367ae7632731639de5c21f29c8744ed?narHash=sha256-40J9tW7Y794J7Uw4GwcAKlMxlX2xISBl6IBigo83ih8%3D' (2024-09-28) • Updated input 'nixpkgs-unstable': 'github:NixOS/nixpkgs/9357f4f23713673f310988025d9dc261c20e70c6?narHash=sha256-bvGoiQBvponpZh8ClUcmJ6QnsNKw0EMrCQJARK3bI1c%3D' (2024-09-21) → 'github:NixOS/nixpkgs/1925c603f17fc89f4c8f6bf6f631a802ad85d784?narHash=sha256-J%2BPeFKSDV%2BpHL7ukkfpVzCOO7mBSrrpJ3svwBFABbhI%3D' (2024-09-26) |
||
---|---|---|
.gitea/workflows | ||
hosts | ||
modules | ||
overlays | ||
.gitignore | ||
flake.lock | ||
flake.nix | ||
LICENSE | ||
README.md |
nixos
Installation
Follow the NixOS
manual to
obtain and boot the installation medium. Use the graphical ISO image since it
ships with useful programs such as nmtui
; the installation can still be done
through the terminal.
Disk Partitioning
For impermanence, partitioning should
be done as outlined in the tmpfs as
root blogpost, but with
/nix
as a LUKS-encrypted file
system.
The boot partition will not be encrypted, since that is poorly supported by
systemd-boot. Persistent files will be saved under /nix/persist
. To find out
which of our darlings will be erased on reboot do tree -x /
or ncdu -x /
.
The following is based on the tmpfs as root blogpost, the NixOS manual's partitioning, formatting and LUKS-Encrypted File Systems sections, ArchWiki's LVM on LUKS, the unofficial NixOS wiki Full Disk Encryption, and this GitHub gist.
We create a 1GiB EFI boot partition (/dev/sda1
) and the rest will be our
LUKS-encrypted volume:
# Create partition table
parted /dev/sda -- mklabel gpt
# Create /boot partition
parted /dev/sda -- mkpart ESP fat32 1MiB 1024MiB
parted /dev/sda -- set 1 esp on
# Create /nix partition
parted /dev/sda -- mkpart primary 1024MiB 100%
# Create and open LUKS-encrypted container
cryptsetup --type=luks2 luksFormat --label=crypted /dev/sda2
cryptsetup open /dev/sda2 crypted
# Create LVM volume group
pvcreate /dev/mapper/crypted
vgcreate vg /dev/mapper/crypted
# Create root logical volume
lvcreate -l 100%FREE vg -n root
# Format partitions
mkfs.fat -F32 -n BOOT /dev/sda1
mkfs.ext4 -L nix /dev/vg/root
The result should be the following (lsblk -f
):
NAME FSTYPE FSVER LABEL
sda
├─sda1 vfat FAT32 BOOT
└─sda2 crypto_LUKS 2 crypted
└─crypted LVM2_member LVM2 001
└─vg-root ext4 1.0 nix
Whereas the NixOS
manual
mounts the newly-created nixos
partition to /mnt
, we will follow the tmpfs
as root blogpost and mount /mnt
as tmpfs
:
mount -t tmpfs none /mnt
mount --mkdir /dev/disk/by-label/BOOT /mnt/boot
mount --mkdir /dev/disk/by-label/nix /mnt/nix
mkdir -p /mnt/nix/persist/
Secrets
All files in the Nix store are world-readable, so it is not a suitable place for including cleartext secrets, even if we had a scheme to securely transfer them to each system. Agenix solves this issue by encrypting the secrets using age, and then decrypting and symlinking them using the system's SSH host key during system activation.
All secrets, and other private configuration such as DNS zonefiles, are stored in a separate, private repo. To bootstrap a new system, we must generate a host key manually during installation:
mkdir -p /mnt/nix/persist/etc/ssh/
ssh-keygen -A -f /mnt/nix/persist
nc alpha.caspervk.net 1337 < /mnt/nix/persist/etc/ssh/ssh_host_ed25519.pub
Then, on an existing system, add the new host's public key to secrets.nix
in
the nixos-secrets
repo and rekey all secrets. When managing secrets, the
Keepass recovery key is used like so:
set AGE_KEY_FILE (mktemp); read -s > $AGE_KEY_FILE
agenix -i $AGE_KEY_FILE --rekey
agenix -i $AGE_KEY_FILE -e foo.age
The new system needs to be able to pull the nixos-secrets
repo temporarily
during installation:
ssh-keygen -t ed25519
nc alpha.caspervk.net 1337 < /root/.ssh/id_ed25519.pub
# https://git.caspervk.net/caspervk/nixos-secrets/settings/keys
After bootstrapping, servers will auto-upgrade using the shared autoUpgrade
SSH key. Desktops will need to add ~caspervk/.ssh/id_ed25519.pub
either as a
deploy key for the nixos-secrets
repo, or to the entire git user.
Installation
The remaining installation can be done (more or less) according to the NixOS manual.
cd /mnt/nix
git clone https://git.caspervk.net/caspervk/nixos.git
cd nixos/
nixos-generate-config --root /mnt --show-hardware-config
vim hosts/omega/hardware.nix
git add . # nix sometimes ignores files outside version control
nixos-install --no-root-passwd --flake .#omega
Hardware Configuration
hosts/*/hardware.nix
, while initially generated by nixos-generate-config --show-hardware-config
, is manually modified.
Upgrading
Nixpkgs uses stateVersion
so sparingly that auditing the entire nixpkgs repo
is easy
enough.
Important changes to home-manager is available at
https://nix-community.github.io/home-manager/release-notes.xhtml and
https://github.com/nix-community/home-manager/blob/master/modules/misc/news.nix.
Useful Commands
# development
sudo nixos-rebuild switch --flake . --override-input secrets ./../nixos-secrets/
# start build environment with user's default shell instead of bash
nix develop --command $SHELL
# nix shell with python packages
# https://discourse.nixos.org/t/nix-shell-for-python-packages/16575
# https://github.com/NixOS/nix/issues/5567
nix shell --impure --expr 'with builtins.getFlake "nixpkgs"; with legacyPackages.${builtins.currentSystem}; python3.withPackages (ps: with ps; [ numpy ])'
Debugging
# load flake into repl
nix repl . --override-input secrets ./../nixos-secrets/
# print a configuration option
:p nixosConfigurations.omega.options.services.openssh.ports.declarationPositions # declaration
:p nixosConfigurations.omega.options.services.openssh.ports.default # declaration default
:p nixosConfigurations.omega.options.services.openssh.ports.definitionsWithLocations # overwrites
:p nixosConfigurations.omega.options.services.openssh.ports.value # current value
# print derivation package names
:p builtins.map (d: d.name) outputs.nixosConfigurations.omega.options.environment.systemPackages.value
# print version of package in nixpkgs
:p inputs.nixpkgs.outputs.legacyPackages.${builtins.currentSystem}.openssh.version