Skip to content

Commit

Permalink
Start server setup from scratch with disko and nixos-anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
KubqoA committed Oct 24, 2024
1 parent f883fef commit dd32aa1
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 120 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Refer to the respective host directories for more information.
- [ ] Dev environments
- [ ] Full Neovim setup
- [x] Full nix-darwin setup with system configuration
- [ ] Server setup
- [ ] Explore [disko](https://github.com/nix-community/disko) for declarative disk management
- [ ] Explore [nixos-anywhere](https://github.com/nix-community/nixos-anywhere) for remote installation
- [x] Server setup
- [x] Explore [disko](https://github.com/nix-community/disko) for declarative disk management
- [x] Explore [nixos-anywhere](https://github.com/nix-community/nixos-anywhere) for remote installation

## Useful resources
- [NixOS & Flakes Book](https://nixos-and-flakes.thiscute.world/)
Expand Down
30 changes: 15 additions & 15 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions hosts/organ/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@

# organ
ARM-based server running on Hetzner cloud

## Setup
1. Create a new server on Hetzner Cloud
- Tested with Ubuntu 24.04
- Arm64 architecture
- Assign an IPv4 and IPv6 address
- Add your SSH key
2. Get the SSH keys from the server
```sh
ssh-keyscan <server-ip>
```
3. Update the [`secrets.nix`](../../secrets/secrets.nix) file with the SSH key, and re-key all the relevant secrets
4. Update [`networking.nix`](./networking.nix) with the correct IP addresses
5. Run the following command from the root of the repository
```sh
./hosts/organ/install.sh
```

And that's it!
27 changes: 19 additions & 8 deletions hosts/organ/default.nix
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
{
config,
lib,
pkgs,
modulesPath,
...
}: {
imports =
[
./git.nix
./hardware-configuration.nix
./mail.nix
(modulesPath + "/profiles/qemu-guest.nix")
./disko.nix
# ./git.nix
# ./mail.nix
./networking.nix
./nginx.nix
# ./nginx.nix
./ssh.nix
./syncthing.nix
# ./syncthing.nix
./users.nix
]
++ lib._.moduleImports [
"common/nix"
"common/packages"
"server/dns"
# "server/dns"
"server/tailscale"
];

age.secrets = lib._.defineSecrets ["organ-tailscale-auth-key"] {};

boot = {
loader = {
systemd-boot.enable = true;
systemd-boot.configurationLimit = 5;
efi.canTouchEfiVariables = true;
};
initrd.kernelModules = ["virtio_gpu"];
kernelParams = ["console=tty"];
};

time.timeZone = "Europe/Prague";

server = {
dns.zones."jakubarbet.me" = ./dns/jakubarbet.me.zone;
# dns.zones."jakubarbet.me" = ./dns/jakubarbet.me.zone;
tailscale = {
tailnet = "ide-vega.ts.net";
tailscaleIpv4 = "100.67.2.27";
Expand Down
64 changes: 64 additions & 0 deletions hosts/organ/disko.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
inputs,
lib,
...
}: {
imports = [inputs.disko.nixosModules.disko];

disko.devices.disk.main = {
type = "disk";
device = lib.mkDefault "/dev/sda";
content = {
type = "gpt";
partitions = {
ESP = {
priority = 1;
name = "ESP";
start = "1M";
end = "128M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
mountOptions = ["umask=0077"];
};
};
root = {
end = "-2G";
content = {
type = "btrfs";
extraArgs = ["-f"]; # Override existing partition
subvolumes = {
"/root" = {
mountpoint = "/";
};
"/home" = {
mountOptions = ["compress=zstd" "noatime"];
mountpoint = "/home";
};
"/persist" = {
mountOptions = ["compress=zstd" "noatime"];
mountpoint = "/persist";
};
"/nix" = {
mountOptions = ["compress=zstd" "noatime"];
mountpoint = "/nix";
};
"/log" = {
mountOptions = ["compress=zstd" "noatime"];
mountpoint = "/var/log";
};
};
};
};
swap = {
size = "100%";
content.type = "swap";
};
};
};
};

fileSystems."/var/log".neededForBoot = true;
}
74 changes: 0 additions & 74 deletions hosts/organ/hardware-configuration.nix

This file was deleted.

12 changes: 12 additions & 0 deletions hosts/organ/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

organ_dir="$(dirname "$0")"
networking_conf="$organ_dir/networking.nix"
ipv4=$(sed -n 's/.*ipv4 = "\(.*\)".*/\1/p' "$networking_conf")
ipv6=$(sed -n 's/.*ipv6 = "\(.*\)".*/\1/p' "$networking_conf")

nix run github:nix-community/nixos-anywhere -- \
--build-on-remote \
--copy-host-keys \
--flake ".#organ" \
root@$ipv4
11 changes: 5 additions & 6 deletions hosts/organ/networking.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{config, ...}: let
ipv4 = "116.203.250.61";
ipv6 = "2a01:4f8:c012:58f4::";
{...}: let
ipv4 = "116.202.110.124";
ipv6 = "2a01:4f8:c013:5899::";
in {
networking = {
hostName = "organ";
domain = "jakubarbet.me";
useDHCP = false;
nameservers = ["1.1.1.1" "1.0.0.1" "2606:4700:4700::1111" "2606:4700:4700::1001"];
Expand All @@ -20,8 +19,8 @@ in {
matchConfig.Name = "enp1s0";
networkConfig.DHCP = "no";
address = [
"${config.ipv4}/32"
"${config.ipv6}/64"
"${ipv4}/32"
"${ipv6}/64"
];
routes = [
{
Expand Down
1 change: 1 addition & 0 deletions hosts/organ/users.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
config,
lib,
pkgs,
...
}: {
age.secrets = lib._.defineSecrets ["organ-jakub-password-hash"] {};
Expand Down
2 changes: 2 additions & 0 deletions modules/common/nix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# Enable support for nix commands and flakes
settings.experimental-features = ["nix-command" "flakes"];

settings.trusted-users = ["root" "jakub"];

# Pinning the registry to the system pkgs on NixOS
registry.nixpkgs.flake = inputs.nixpkgs;

Expand Down
13 changes: 7 additions & 6 deletions modules/server/tailscale.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ with lib; {
services = {
# Let dnsmasq handle DNS resolution for the tailscale network
bind = {
listenOn = ["!${config.tailscaleIpv4}"];
listenOnIpv6 = ["!${config.tailscaleIpv6}"];
listenOn = ["!${config.server.tailscale.tailscaleIpv4}"];
listenOnIpv6 = ["!${config.server.tailscale.tailscaleIpv6}"];
};

# Used to define DNS override for FQDN to tailscale IPs so devices
# connected to the tailnet can access the site which is behind
# an tailscale-auth protection
dnsmasq = {
enable = true;
resolveLocalQueries = false;
settings = {
bind-interfaces = true;
listen-address = "${config.tailscaleIpv4},${config.tailscaleIpv6}";
address = ["/${config.networking.fqdn}/${config.tailscaleIpv4}" "/${config.networking.fqdn}/${config.tailscaleIpv6}"];
listen-address = "${config.server.tailscale.tailscaleIpv4},${config.server.tailscale.tailscaleIpv6}";
address = ["/${config.networking.fqdn}/${config.server.tailscale.tailscaleIpv4}" "/${config.networking.fqdn}/${config.server.tailscale.tailscaleIpv6}"];
};
};

nginx.tailscaleAuth = {
enable = true;
expectedTailnet = config.tailnet;
expectedTailnet = config.server.tailscale.tailnet;
virtualHosts = [config.networking.fqdn];
};

Expand All @@ -57,7 +58,7 @@ with lib; {
};

networking.firewall = {
trustedInterfaces = config.services.tailscale.interfaceName;
trustedInterfaces = [config.services.tailscale.interfaceName];
};
};
}
Binary file modified secrets/organ-jakub-password-hash.age
Binary file not shown.
Loading

0 comments on commit dd32aa1

Please sign in to comment.