nix-config/README.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

320 lines
7.7 KiB
Markdown
Raw Normal View History

2022-01-07 00:55:02 +01:00
---
gitea: none
title: Flockige Infrastruktur deklarativ
include_toc: yes
lang: en
---
2021-02-22 12:31:58 +01:00
# Setup
2022-01-07 00:35:39 +01:00
## Add this repo to your local Nix registry
As an alternative to a local checkout, always pull the latest code
from this repo.
```bash
nix registry add c3d2 git+https://gitea.c3d2.de/C3D2/nix-config
```
This enables `nix` commands to find this Flake given the `c3d2#`
prefix in some arguments.
2022-01-07 00:28:59 +01:00
## Working with this repo
If you checked out this git repository for working on the code,
replace `c3d2#` with `.#` and run commands from the repository root.
Don't forget to `git add` new files! Flakes require that.
## The secrets repo
Make sure you have access.
## Install Nix Flakes
2021-02-22 12:31:58 +01:00
2022-01-07 00:28:59 +01:00
> Nix Flakes ist gegenwärtig bei Nix (Version 20.09) noch keine standardmäßige Funktionalität für Nix. Die Bereitstellung der Kommandos für Nix Flakes müssen als experimentelle Funktionalität für das Kommando ''nix'' festgelegt werden, um sie verfügbar zu machen.
Set some configuration (do this only once):
```bash
2021-02-22 12:31:58 +01:00
echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.conf
```
2021-02-22 12:31:58 +01:00
2022-01-07 00:28:59 +01:00
### Permanent System with Nix Flakes
set this to your NixOS configuration:
```nix
2021-02-22 12:31:58 +01:00
{ pkgs, ... }: {
nix = {
extraOptions = "experimental-features = nix-command flakes";
};
}
```
# Deployment
2022-01-07 00:28:59 +01:00
## Deploy a NixOS system from this Flake locally
2021-03-28 21:10:52 +02:00
Running `nixos-rebuild --flake c3d2 switch` on a machine should be sufficient
to update that machine to the current configuration and Nixpkgs revision.
2021-02-22 12:31:58 +01:00
2022-01-07 00:28:59 +01:00
## Deploy to a remote NixOS system with this Flake
2021-02-22 12:31:58 +01:00
2022-01-07 00:28:59 +01:00
For every host that has a `nixosConfiguration` in our Flake, there are
two scripts that can be run for deployment via ssh.
2021-09-08 22:48:13 +02:00
2022-06-21 15:06:41 +02:00
- `nix run .#glotzbert-nixos-rebuild switch`
2021-09-08 22:48:13 +02:00
2022-01-07 00:28:59 +01:00
Copies the current state to build on the target system. This may
fail due to eg. container resource limits.
The target must already be a nixFlakes system.
2021-02-22 12:31:58 +01:00
2022-06-21 15:06:41 +02:00
- `nix run .#glotzbert-nixos-rebuild-local switch`
2022-01-07 00:28:59 +01:00
Builds locally, then uses `nix copy` to transfer the new NixOS
system to the target.
**Help!** It's needlessly rebuilding stuff that already runs on the
target? If so, use `nix copy` to transfer where
`/run/current-system` points to to your build machine.
2019-07-04 00:31:45 +02:00
2021-09-08 00:34:38 +02:00
## Checking for updates
```shell
nix run .#list-upgradable
```
![list-upgradable output](doc/list-upgradable.png)
Checks all hosts with a `nixosConfiguration` in `flake.nix`.
2021-09-08 00:34:38 +02:00
## Update from [Hydra build](https://hydra.hq.c3d2.de/jobset/c3d2/nix-config#tabs-jobs)
The fastest way to update a system, a manual alternative to setting
`c3d2.autoUpdate = true;`
Just run:
```shell
update-from-hydra
```
2022-06-16 21:45:27 +02:00
## Deploy a MicroVM
### Building spaceapi remotely, and deploy
```shell
nix run .#microvm-update-spaceapi
```
### Building spaceapi locally, and deploy
```shell
nix run .#microvm-update-spaceapi-local
```
2022-06-19 03:30:35 +02:00
### Update MicroVM from our Hydra
Our Hydra runs `nix flake update` daily in the `updater.timer`,
pushing it to the `flake-update` branch so that it can build fresh
systems. This branch is setup as the source flake in all the MicroVMs,
so the following is all that is needed on a MicroVM-hosting server:
```shell
microvm -Ru $hostname
```
2022-11-30 21:00:36 +01:00
# Cluster deployment with Skyflake
## About
2022-12-03 01:10:15 +01:00
[Skyflake](https://github.com/astro/skyflake) provides Hyperconverged
Infrastructure to run NixOS MicroVMs on a cluster. Our setup unifies
networking with one bridge per VLAN. Persistent storage is replicated
with Glusterfs.
2022-11-30 21:00:36 +01:00
2022-12-03 04:10:31 +01:00
Recognize nixosConfiguration for our Skyflake deployment by the
`self.nixosModules.cluster-options` module being included.
2022-11-30 21:00:36 +01:00
2022-12-03 01:10:15 +01:00
## User interface
2022-11-30 21:00:36 +01:00
2022-12-03 01:10:15 +01:00
We use the less-privileged `c3d2@` user for deployment. This flake's
name on the cluster is `config`. Other flakes can coexist in the same
user so that we can run separately developed projects like
*dump-dvb*. *leon* and potentially other users can deploy Flakes and
MicroVMs without name clashes.
### Deploying
2022-12-03 04:10:31 +01:00
**git push** this repo to any machine in the cluster, preferably to
Hydra because there building won't disturb any services.
2022-11-30 21:00:36 +01:00
You don't deploy all MicroVMs at once. Instead, Skyflake allows you to
2022-12-03 04:10:31 +01:00
select NixOS systems by the branches you push to. **You must commit
before you push!**
2022-11-30 21:00:36 +01:00
2022-12-03 04:10:31 +01:00
**Example:** deploy nixosConfigurations `mucbot` and `sdrweb` (`HEAD` is your
current commit)
2022-11-30 21:00:36 +01:00
```bash
git push c3d2@hydra.serv.zentralwerk.org:config HEAD:mucbot HEAD:sdrweb
```
2022-12-03 04:10:31 +01:00
This will:
1. Build the configuration on Hydra, refusing the branch update on
broken builds (through a git hook)
2. Copy the MicroVM package and its dependencies to the binary cache
that is accessible to all nodes with Glusterfs
3. Submit one job per MicroVM into the Nomad cluster
*Deleting* a nixosConfiguration's branch will **stop** the MicroVM in Nomad.
2022-12-03 01:10:15 +01:00
### Updating
**TODO:** how would you like it?
### MicroVM status
```bash
ssh c3d2@hydra.serv.zentralwerk.org status
```
## Debugging for cluster admins
2022-11-30 21:00:36 +01:00
### Glusterfs
2022-12-03 01:10:15 +01:00
Glusterfs holds our MicroVMs' state. They *must always be mounted* or
brains are split.
2022-11-30 21:00:36 +01:00
```bash
gluster volume info
gluster volume status
```
#### Restart glusterd
```bash
systemctl restart glusterd
```
#### Remount volumes
```bash
systemctl restart /glusterfs/fast
systemctl restart /glusterfs/big
```
### Nomad
#### Check the cluster state
```shell
nomad server members
```
2022-12-03 01:10:15 +01:00
Nomad *servers* **coordinate** the cluster.
Nomad *clients* **run** the tasks.
2022-11-30 21:00:36 +01:00
#### Browse in the terminal
2022-12-03 01:10:15 +01:00
[wander](https://github.com/robinovitch61/wander) and
[damon](https://github.com/hashicorp/damon) are nice TUIs that are
preinstalled on our cluster nodes.
2022-11-30 21:00:36 +01:00
#### Browse with a browser
First, tunnel TCP port `:4646` from a cluster server:
```bash
ssh -L 4646:localhost:4646 root@server10.cluster.zentralwerk.org
```
Then, visit https://localhost:4646 for for full klickibunti.
#### Reset the Nomad state on a node
2022-12-03 01:10:15 +01:00
After upgrades, Nomad servers may fail rejoining the cluster. Do this
to make a *Nomad server* behave like a newborn:
2022-11-30 21:00:36 +01:00
```shell
systemctl stop nomad
rm -rf /var/lib/nomad/server/raft/
systemctl start nomad
```
2022-01-07 00:28:59 +01:00
# Secrets management
## Secrets managment with PGP
2019-04-30 23:21:34 +02:00
2019-05-01 00:55:11 +02:00
Add your gpg-id to the .gpg-id file in secrets and let somebody reencrypt it for you.
Maybe this works for you, maybe not. I did it somehow:
2020-01-01 13:40:42 +01:00
2022-01-08 21:22:24 +01:00
```bash
2020-01-01 13:40:42 +01:00
PASSWORD_STORE_DIR=`pwd` tr '\n' ' ' < .gpg-id | xargs -I{} pass init {}
```
2019-05-01 00:55:11 +02:00
Your gpg key has to have the Authenticate flag set. If not update it and push it to a keyserver and wait.
This is necessary, so you can login to any machine with your gpg key.
2020-01-01 13:40:42 +01:00
2022-01-07 00:28:59 +01:00
## Secrets Management Using `sops-nix`
2022-01-08 21:22:24 +01:00
### Adding a new host
Edit `secrets/.sops.yaml`:
1. Add an AGE key for this host. Comments in this file tell you how to
do it.
2. Add a `creation_rules` section for `host/$host/*yaml` files
### Editing a hosts secrets
2022-01-07 00:28:59 +01:00
Edit `secrets/.sops.yaml` to add files for a new host and its SSH pubkey.
2022-01-08 21:22:24 +01:00
```bash
# Enter the secrets flake
2022-01-07 00:28:59 +01:00
cd secrets
2022-01-08 21:22:24 +01:00
# Get sops
2022-01-07 00:28:59 +01:00
nix develop
2022-01-08 21:22:24 +01:00
# Decrypt, start en EDITOR, encrypt
2022-01-07 00:28:59 +01:00
sops hosts/.../secrets.yaml
2022-01-08 21:22:24 +01:00
# Push
2022-01-07 00:28:59 +01:00
git commit -a -m YOLO
git push origin HEAD:master
2022-01-08 21:22:24 +01:00
# Go back to this flake
2022-01-07 00:28:59 +01:00
cd ..
2022-01-08 21:22:24 +01:00
# Update flake.lock file
2022-01-07 00:28:59 +01:00
nix flake lock . --update-input secrets
```
2020-01-01 13:40:42 +01:00
# Laptops / Desktops
2021-02-22 12:31:58 +01:00
This repository contains a NixOS module that can be used with personal machines
as well. This module appends `/etc/ssh/ssh_known_hosts` with the host keys of
registered HQ hosts, and optionally appends `/etc/hosts` with static IPv6
addresses local to HQ. Simply import the `lib` directory to use the module. As
2020-01-01 13:40:42 +01:00
an example:
```nix
# /etc/nixos/configuration.nix
{ config, pkgs, lib, ... }:
let
c3d2Config =
builtins.fetchGit { url = "https://gitea.c3d2.de/C3D2/nix-config.git"; };
in {
imports = [
# ...
"${c3d2Config}/modules/c3d2.nix"
2020-01-01 13:40:42 +01:00
];
c3d2 = {
isInHq = false; # not in HQ, this is the default.
enableMotd = true; # Set the login shell message to the <<</>> logo.
};
# ...
}
```