forked from Bos55/nix-config
refactor: optimize flake.nix and modularize networking config
Cleaner deploy.nodes generation, improved devShell experience, and centralized host IP definitions using utils.lib.system variables.
This commit is contained in:
parent
5a031b48ed
commit
f8ed707253
13 changed files with 182 additions and 48 deletions
64
README.md
Normal file
64
README.md
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
# Bos55 NixOS Config
|
||||||
|
|
||||||
|
Automated CI/CD deployment for NixOS homelab using `deploy-rs`.
|
||||||
|
|
||||||
|
## Repository Structure
|
||||||
|
|
||||||
|
- `hosts/`: Host-specific configurations.
|
||||||
|
- `modules/`: Shared NixOS modules.
|
||||||
|
- `users/`: User definitions (including the `deploy` user).
|
||||||
|
- `secrets/`: Encrypted secrets via `sops-nix`.
|
||||||
|
|
||||||
|
## Deployment Workflow
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
- SSH access to the `deploy` user on target hosts.
|
||||||
|
- `deploy-rs` installed locally (`nix profile install github:serokell/deploy-rs`).
|
||||||
|
|
||||||
|
### Deployment Modes
|
||||||
|
|
||||||
|
1. **Production Deployment (main branch):**
|
||||||
|
Triggered on push to `main`. Automatically builds and switches all hosts. bootloader is updated.
|
||||||
|
Manual: `deploy .`
|
||||||
|
|
||||||
|
2. **Test Deployment (test-<hostname> branch):**
|
||||||
|
Triggered on push to `test-<hostname>`. Builds and activates the configuration on the specific host **without** updating the bootloader. Reboots will revert to the previous generation.
|
||||||
|
Manual: `deploy .#<hostname>.test`
|
||||||
|
|
||||||
|
3. **Kernel Upgrades / Maintenance:**
|
||||||
|
Use `deploy .#<hostname>.system --boot` to update the bootloader without immediate activation, followed by a manual reboot.
|
||||||
|
|
||||||
|
## Local Development
|
||||||
|
|
||||||
|
### 1. Developer Shell
|
||||||
|
This repository includes a standardized development environment containing all necessary tools (`deploy-rs`, `sops`, `age`, etc.).
|
||||||
|
```bash
|
||||||
|
nix develop
|
||||||
|
# or if using direnv
|
||||||
|
direnv allow
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Build a host VM
|
||||||
|
You can build a QEMU VM for any host configuration to test changes locally:
|
||||||
|
```bash
|
||||||
|
nix build .#nixosConfigurations.<hostname>.config.system.build.vm
|
||||||
|
./result/bin/run-<hostname>-vm
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> **Network Conflict**: Default VMs use user-mode networking (NAT) which is safe. However, if you configure the VM to use bridge networking, it will attempt to use the static IP defined in `hostIp`. Ensure you do not have a physical host with that IP active on the same bridge to avoid network interference.
|
||||||
|
|
||||||
|
### 3. Run Integration Tests
|
||||||
|
Run the automated test suite:
|
||||||
|
```bash
|
||||||
|
nix-build test/vm-test.nix
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Test CI Workflows Locally
|
||||||
|
Use `act` to test the GitHub Actions workflows:
|
||||||
|
```bash
|
||||||
|
act -W .github/workflows/check.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Security
|
||||||
|
See [SECURITY.md](SECURITY.md) for details on the trust model and secret management.
|
||||||
53
flake.nix
53
flake.nix
|
|
@ -13,52 +13,77 @@
|
||||||
url = "github:gytis-ivaskevicius/flake-utils-plus";
|
url = "github:gytis-ivaskevicius/flake-utils-plus";
|
||||||
inputs.flake-utils.follows = "flake-utils";
|
inputs.flake-utils.follows = "flake-utils";
|
||||||
};
|
};
|
||||||
|
deploy-rs = {
|
||||||
|
url = "github:serokell/deploy-rs";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = inputs@{
|
outputs = inputs@{
|
||||||
self, nixpkgs,
|
self, nixpkgs,
|
||||||
flake-utils, sops-nix, utils,
|
flake-utils, sops-nix, utils, deploy-rs,
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
system = utils.lib.system.x86_64-linux;
|
system = utils.lib.system.x86_64-linux;
|
||||||
|
lib = nixpkgs.lib;
|
||||||
in
|
in
|
||||||
utils.lib.mkFlake {
|
utils.lib.mkFlake {
|
||||||
inherit self inputs;
|
inherit self inputs;
|
||||||
|
|
||||||
hostDefaults = {
|
hostDefaults.modules = [
|
||||||
inherit system;
|
|
||||||
|
|
||||||
modules = [
|
|
||||||
./modules
|
./modules
|
||||||
./users
|
./users
|
||||||
|
|
||||||
sops-nix.nixosModules.sops
|
sops-nix.nixosModules.sops
|
||||||
];
|
];
|
||||||
};
|
|
||||||
|
|
||||||
hosts = {
|
hosts = {
|
||||||
# Physical hosts
|
# Infrastructure
|
||||||
Niko.modules = [ ./hosts/Niko ];
|
Niko.modules = [ ./hosts/Niko ];
|
||||||
|
|
||||||
# Virtual machines
|
|
||||||
|
|
||||||
# Single-service
|
|
||||||
Ingress.modules = [ ./hosts/Ingress ];
|
Ingress.modules = [ ./hosts/Ingress ];
|
||||||
Gitea.modules = [ ./hosts/Gitea ];
|
Gitea.modules = [ ./hosts/Gitea ];
|
||||||
Vaultwarden.modules = [ ./hosts/Vaultwarden ];
|
Vaultwarden.modules = [ ./hosts/Vaultwarden ];
|
||||||
|
|
||||||
# Production multi-service
|
# Production
|
||||||
Binnenpost.modules = [ ./hosts/Binnenpost ];
|
Binnenpost.modules = [ ./hosts/Binnenpost ];
|
||||||
Production.modules = [ ./hosts/Production ];
|
Production.modules = [ ./hosts/Production ];
|
||||||
ProductionGPU.modules = [ ./hosts/ProductionGPU ];
|
ProductionGPU.modules = [ ./hosts/ProductionGPU ];
|
||||||
ProductionArr.modules = [ ./hosts/ProductionArr ];
|
ProductionArr.modules = [ ./hosts/ProductionArr ];
|
||||||
ACE.modules = [ ./hosts/ACE ];
|
ACE.modules = [ ./hosts/ACE ];
|
||||||
|
|
||||||
# Others
|
# Lab
|
||||||
Template.modules = [ ./hosts/Template ];
|
Template.modules = [ ./hosts/Template ];
|
||||||
Development.modules = [ ./hosts/Development ];
|
Development.modules = [ ./hosts/Development ];
|
||||||
Testing.modules = [ ./hosts/Testing ];
|
Testing.modules = [ ./hosts/Testing ];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deploy.nodes = let
|
||||||
|
pkg = deploy-rs.lib.${system};
|
||||||
|
isDeployable = nixos: (nixos.config.homelab.users.deploy.enable or false) && (nixos.config.homelab.networking.hostIp != null);
|
||||||
|
in
|
||||||
|
builtins.mapAttrs (_: nixos: {
|
||||||
|
hostname = nixos.config.homelab.networking.hostIp;
|
||||||
|
sshUser = "deploy";
|
||||||
|
user = "root";
|
||||||
|
profiles.system.path = pkg.activate.nixos nixos;
|
||||||
|
profiles.test.path = pkg.activate.custom nixos.config.system.build.toplevel ''
|
||||||
|
$PROFILE/bin/switch-to-configuration test
|
||||||
|
'';
|
||||||
|
}) (lib.filterAttrs (_: isDeployable) self.nixosConfigurations);
|
||||||
|
|
||||||
|
checks = builtins.mapAttrs (_: lib: lib.deployChecks self.deploy) deploy-rs.lib;
|
||||||
|
|
||||||
|
outputsBuilder = channels: {
|
||||||
|
formatter = channels.nixpkgs.alejandra;
|
||||||
|
devShells.default = channels.nixpkgs.mkShell {
|
||||||
|
name = "homelab-dev";
|
||||||
|
buildInputs = [
|
||||||
|
deploy-rs.packages.${system}.deploy-rs
|
||||||
|
channels.nixpkgs.sops
|
||||||
|
channels.nixpkgs.age
|
||||||
|
];
|
||||||
|
shellHook = "echo '🛡️ Homelab Development Shell Loaded'";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
{ pkgs, ... }:
|
{ config, pkgs, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.41";
|
||||||
services.actions.enable = true;
|
services.actions.enable = true;
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -24,7 +26,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.41";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{ pkgs, ... }:
|
{ config, pkgs, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
|
|
@ -13,12 +13,14 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.89";
|
||||||
apps = {
|
apps = {
|
||||||
speedtest.enable = true;
|
speedtest.enable = true;
|
||||||
technitiumDNS.enable = true;
|
technitiumDNS.enable = true;
|
||||||
traefik.enable = true;
|
traefik.enable = true;
|
||||||
};
|
};
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -43,7 +45,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.89";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,12 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.24";
|
||||||
apps.gitea.enable = true;
|
apps.gitea.enable = true;
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
|
||||||
|
users.deploy.enable = true;
|
||||||
|
|
||||||
users.admin = {
|
users.admin = {
|
||||||
enable = true;
|
enable = true;
|
||||||
authorizedKeys = [
|
authorizedKeys = [
|
||||||
|
|
@ -28,7 +31,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.24";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
];
|
];
|
||||||
|
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.11";
|
||||||
apps = {
|
apps = {
|
||||||
technitiumDNS.enable = true;
|
technitiumDNS.enable = true;
|
||||||
traefik.enable = true;
|
traefik.enable = true;
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,13 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.31";
|
||||||
apps = {
|
apps = {
|
||||||
calibre.enable = true;
|
calibre.enable = true;
|
||||||
traefik.enable = true;
|
traefik.enable = true;
|
||||||
};
|
};
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -31,7 +33,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.31";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,13 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.33";
|
||||||
apps = {
|
apps = {
|
||||||
arr.enable = true;
|
arr.enable = true;
|
||||||
traefik.enable = true;
|
traefik.enable = true;
|
||||||
};
|
};
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -31,7 +33,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.33";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.94";
|
||||||
apps.jellyfin.enable = true;
|
apps.jellyfin.enable = true;
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -28,7 +30,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.94";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,13 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.92";
|
||||||
apps = {
|
apps = {
|
||||||
freshrss.enable = true;
|
freshrss.enable = true;
|
||||||
traefik.enable = true;
|
traefik.enable = true;
|
||||||
};
|
};
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
users.deploy.enable = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
|
|
@ -32,7 +34,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.92";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
|
networking.hostIp = "192.168.0.22";
|
||||||
apps.vaultwarden = {
|
apps.vaultwarden = {
|
||||||
enable = true;
|
enable = true;
|
||||||
domain = "https://vault.depeuter.dev";
|
domain = "https://vault.depeuter.dev";
|
||||||
|
|
@ -10,13 +11,17 @@
|
||||||
};
|
};
|
||||||
virtualisation.guest.enable = true;
|
virtualisation.guest.enable = true;
|
||||||
|
|
||||||
users.admin = {
|
users = {
|
||||||
|
deploy.enable = true;
|
||||||
|
|
||||||
|
admin = {
|
||||||
enable = true;
|
enable = true;
|
||||||
authorizedKeys = [
|
authorizedKeys = [
|
||||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJnihoyozOCnm6T9OzL2xoMeMZckBYR2w43us68ABA93"
|
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJnihoyozOCnm6T9OzL2xoMeMZckBYR2w43us68ABA93"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
hostId = "aaaa1300";
|
hostId = "aaaa1300";
|
||||||
|
|
@ -32,7 +37,7 @@
|
||||||
interfaces.ens18 = {
|
interfaces.ens18 = {
|
||||||
ipv4.addresses = [
|
ipv4.addresses = [
|
||||||
{
|
{
|
||||||
address = "192.168.0.22";
|
address = config.homelab.networking.hostIp;
|
||||||
prefixLength = 24;
|
prefixLength = 24;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,9 @@
|
||||||
{
|
{
|
||||||
|
imports = [
|
||||||
|
./networking.nix
|
||||||
|
./secrets.nix
|
||||||
|
];
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
homelab = {
|
homelab = {
|
||||||
services.openssh.enable = true;
|
services.openssh.enable = true;
|
||||||
|
|
|
||||||
19
modules/common/networking.nix
Normal file
19
modules/common/networking.nix
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
{ config, lib, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
options.homelab.networking = {
|
||||||
|
hostIp = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
description = ''
|
||||||
|
The primary IP address of the host.
|
||||||
|
Used for automated deployment and internal service discovery.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf (config.homelab.networking.hostIp != null) {
|
||||||
|
# If a hostIp is provided, we can potentially use it to configure
|
||||||
|
# networking interfaces or firewall rules automatically here in the future.
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue