This project demonstrates a microservices architecture using Go and Nix for reproducible builds and deployments.
.
├── flake.nix # Main Nix configuration
├── apps/ # Application services
├── pkg/ # Shared Go packages
├── modules/ # NixOS modules
└── nix/ # Additional Nix configurations
├── ci.nix # CI/CD configuration
└── test-config.nix # NixOS module tests
- API: HTTP API service (port 8080)
- Metrics: Metrics collection service (port 8081)
- Worker: Background job processor
- Nix with flakes enabled
- Enter development shell:
nix develop
- Build a service:
nix build .#api
nix build .#metrics
nix build .#worker
- Build a container:
nix build .#containers.api
nix build .#containers.metrics
nix build .#containers.worker
- Load container into Docker:
docker load < result
- Run NixOS module tests:
nix-build nix/test-config.nix -A test
- Build and run test VM:
nix-build nix/test-config.nix -A vm
./result/bin/run-*-vm
- Run service tests:
nix build .#checks.x86_64-linux.test-all
- Use
air
for hot reloading during development - Each service has its own
go.mod
for dependency management - Shared code lives in the
pkg
directory - Test new NixOS modules using
nix/test-config.nix
The project includes NixOS modules for deployment. Configure services in your NixOS configuration:
{ config, ... }:
{
imports = [ ./modules ];
services.myapp = {
api.enable = true;
metrics.enable = true;
worker.enable = true;
};
}
Container images are built using Nix's dockerTools
for maximum reproducibility. Images are minimal and contain only the necessary binaries.
When adding new modules or modifying existing ones:
- Add the module to
modules/
- Update
modules/default.nix
to import it - Add configuration to
nix/test-config.nix
- Test using:
nix-build nix/test-config.nix -A test
- For interactive testing, run the VM:
nix-build nix/test-config.nix -A vm ./result/bin/run-*-vm