8b42a81a8e
Flake lock file updates: • Updated input 'home-manager-unstable': 'github:nix-community/home-manager/a500de54b2e3067201a40cefa5f210f719423ddf?narHash=sha256-AfVYEQIhOK6vaYVndgqFV4Vb5REXG9R0ylv83QInsT0%3D' (2024-03-12) → 'github:nix-community/home-manager/022464438a85450abb23d93b91aa82e0addd71fb?narHash=sha256-2bNMraoRB4pdw/HtxgYTFeMhEekBZeQ53/a8xkqpbZc%3D' (2024-03-19) • Updated input 'nix-index-database': 'github:nix-community/nix-index-database/e76ff2df6bfd2abe06abd8e7b9f217df941c1b07?narHash=sha256-tlLuB73OCOKtU2j83bQzSYFyzjJo3rjpITZE5MoofG8%3D' (2024-03-11) → 'github:nix-community/nix-index-database/e25efda85e39fcdc845e371971ac4384989c4295?narHash=sha256-0fjbN5GYYDKPyPay0l8gYoH%2BtFfNqPPwP5sxxBreeA4%3D' (2024-03-17) • Updated input 'nixos-hardware': 'github:NixOS/nixos-hardware/ad2fd7b978d5e462048729a6c635c45d3d33c9ba?narHash=sha256-j3oWlxRZxB7cFsgEntpH3rosjFHRkAo/dhX9H3OfxtY%3D' (2024-03-11) → 'github:NixOS/nixos-hardware/1e679b9a9970780cd5d4dfe755a74a8f96d33388?narHash=sha256-eIsfu3c9JUBgm3cURSKTXLEI9Dlk1azo%2BMWKZVqrmkc%3D' (2024-03-18) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/ddcd7598b2184008c97e6c9c6a21c5f37590b8d2?narHash=sha256-i2R2bcnQp%2B85de67yjgZVvJhd6rRnJbSYNpGmB6Leb8%3D' (2024-03-11) → 'github:NixOS/nixpkgs/614b4613980a522ba49f0d194531beddbb7220d3?narHash=sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84%3D' (2024-03-17) • Updated input 'nixpkgs-unstable': 'github:NixOS/nixpkgs/3030f185ba6a4bf4f18b87f345f104e6a6961f34?narHash=sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw%3D' (2024-03-09) → 'github:NixOS/nixpkgs/b06025f1533a1e07b6db3e75151caa155d1c7eb3?narHash=sha256-qrxvLS888pNJFwJdK%2Bhf1wpRCSQcqA6W5%2BOx202NDa0%3D' (2024-03-19) |
||
---|---|---|
hosts | ||
modules | ||
overlays | ||
secrets | ||
.gitignore | ||
Containerfile | ||
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.
To bootstrap a new system, we must first generate a host key manually using ssh-keygen -A -f /mnt/nix/persist
during installation. Then, on an existing system, add the new host's public key to secrets.nix
and rekey all
secrets using agenix --rekey
. Commit and push the changes and proceed below.
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 -e foo.age
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 tmp
cd tmp/
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.
State Version
Nixpkgs uses stateVersion
so sparingly that auditing the entire nixpkgs repo is easy
enough.
Useful Commands
# upgrade system
sudo nixos-rebuild switch --flake .
# 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
:lf .
# 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