Adding System Services to NixOS
This skill covers adding system-level services (Docker, databases, etc.) to the NixOS configuration.
Service Module Location
code
system/services/<service-name>.nix
Standard Service Pattern
nix
# system/services/<service-name>.nix
{ config, pkgs, user, ... }: {
# Enable the service
services.<service-name>.enable = true;
# Add user to service group if needed
users.users.${user}.extraGroups = [ "<service-group>" ];
# Install related tools
environment.systemPackages = with pkgs; [
<related-package>
];
}
Docker Example
nix
# system/services/docker.nix
{ pkgs, user, ... }: {
virtualisation.docker = {
enable = true;
enableOnBoot = true;
};
users.users.${user}.extraGroups = [ "docker" ];
environment.systemPackages = [ pkgs.docker-compose ];
}
PostgreSQL Example
nix
# system/services/postgresql.nix
{ pkgs, ... }: {
services.postgresql = {
enable = true;
package = pkgs.postgresql_15;
enableTCPIP = true;
authentication = ''
local all all trust
host all all 127.0.0.1/32 trust
'';
};
}
Integration Steps
- •
Create the service module in
system/services/ - •
Import in host config:
nix
# hosts/<hostname>/default.nix imports = [ ./hardware.nix ../../system/profiles/desktop.nix ../../system/services/<service-name>.nix # Add this ];
- •Rebuild and switch:
bash
sudo nixos-rebuild switch --flake .#<hostname>
- •For VM development, use dev-sync:
bash
./scripts/dev-sync rebuild
Common Services
| Service | NixOS Option | Group |
|---|---|---|
| Docker | virtualisation.docker | docker |
| Podman | virtualisation.podman | - |
| PostgreSQL | services.postgresql | - |
| MySQL | services.mysql | - |
| Redis | services.redis | - |
| nginx | services.nginx | - |
Verification
After rebuild, verify service is running:
bash
systemctl status <service-name> # For Docker docker run hello-world
Important Notes
- •The
uservariable comes from flake.nix extraSpecialArgs - •Services in
system/services/are opt-in per host - •Don't add services directly to
system/base/(those apply to all hosts)