2025-10-05 09:44:40 +02:00
|
|
|
# Quick Start Guide - Port Management Module
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
Get started with centralized port management in 5 minutes!
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Installation
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Step 1: Add to your flake inputs
|
2025-10-04 15:53:48 +02:00
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
|
|
m3ta-nixpkgs.url = "git+https://code.m3ta.dev/m3tam3re/nixpkgs";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Step 2: Choose your configuration type
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## For NixOS Systems
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Basic Setup
|
2025-10-04 15:53:48 +02:00
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
2025-10-05 09:44:40 +02:00
|
|
|
imports = [ inputs.m3ta-nixpkgs.nixosModules.default ];
|
|
|
|
|
|
|
|
m3ta.ports = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
definitions = {
|
|
|
|
nginx = 80;
|
|
|
|
ssh = 22;
|
|
|
|
grafana = 3000;
|
|
|
|
};
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
hostOverrides = {
|
|
|
|
laptop = { nginx = 8080; ssh = 2222; };
|
|
|
|
server = {}; # Uses defaults
|
2025-10-04 15:53:48 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Using Ports
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
# In any NixOS service configuration:
|
|
|
|
services.nginx.defaultHTTPListenPort = config.m3ta.ports.get "nginx";
|
|
|
|
services.openssh.ports = [ (config.m3ta.ports.get "ssh") ];
|
|
|
|
|
|
|
|
# Firewall rules:
|
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
(config.m3ta.ports.get "ssh")
|
|
|
|
(config.m3ta.ports.get "nginx")
|
|
|
|
];
|
2025-10-04 15:53:48 +02:00
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## For Home Manager
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Basic Setup
|
2025-10-04 15:53:48 +02:00
|
|
|
|
|
|
|
```nix
|
|
|
|
{
|
2025-10-05 09:44:40 +02:00
|
|
|
imports = [ inputs.m3ta-nixpkgs.homeManagerModules.default ];
|
|
|
|
|
|
|
|
m3ta.ports = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
definitions = {
|
|
|
|
vite-dev = 5173;
|
|
|
|
jupyter = 8888;
|
|
|
|
local-api = 8000;
|
|
|
|
};
|
|
|
|
|
|
|
|
hostOverrides = {
|
|
|
|
laptop = { vite-dev = 5174; };
|
|
|
|
desktop = { jupyter = 9999; };
|
|
|
|
};
|
|
|
|
|
|
|
|
currentHost = "laptop"; # Set your hostname
|
|
|
|
generateEnvVars = true; # Creates PORT_* environment variables
|
2025-10-04 15:53:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Using Ports
|
2025-10-04 17:03:46 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
# Shell aliases:
|
|
|
|
programs.bash.shellAliases = {
|
|
|
|
dev = "PORT=${toString (config.m3ta.ports.get "vite-dev")} npm run dev";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Environment variables:
|
|
|
|
home.sessionVariables = {
|
|
|
|
DEV_URL = "http://localhost:${toString (config.m3ta.ports.get "vite-dev")}";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Config files:
|
|
|
|
home.file.".config/myapp/config.json".text = builtins.toJSON {
|
|
|
|
port = config.m3ta.ports.get "vite-dev";
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
## Common Patterns
|
2025-10-04 17:03:46 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Multi-Host Configuration
|
2025-10-04 17:03:46 +02:00
|
|
|
|
|
|
|
```nix
|
2025-10-05 09:44:40 +02:00
|
|
|
# shared-ports.nix
|
2025-10-04 17:03:46 +02:00
|
|
|
{
|
2025-10-05 09:44:40 +02:00
|
|
|
ports = {
|
|
|
|
nginx = 80;
|
|
|
|
ssh = 22;
|
|
|
|
grafana = 3000;
|
2025-10-04 17:03:46 +02:00
|
|
|
};
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
hostOverrides = {
|
|
|
|
laptop = { nginx = 8080; ssh = 2222; };
|
|
|
|
server = {};
|
2025-10-04 17:03:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
# In your flake.nix
|
|
|
|
let
|
|
|
|
sharedPorts = import ./shared-ports.nix;
|
|
|
|
in {
|
|
|
|
nixosConfigurations.laptop = nixpkgs.lib.nixosSystem {
|
|
|
|
modules = [
|
|
|
|
m3ta-nixpkgs.nixosModules.default
|
|
|
|
{
|
|
|
|
m3ta.ports = {
|
|
|
|
enable = true;
|
|
|
|
definitions = sharedPorts.ports;
|
|
|
|
hostOverrides = sharedPorts.hostOverrides;
|
|
|
|
currentHost = "laptop";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
2025-10-04 15:53:48 +02:00
|
|
|
```
|
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Proxy Configuration
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
services.nginx.virtualHosts."example.com" = {
|
|
|
|
locations."/grafana/" = {
|
|
|
|
proxyPass = "http://127.0.0.1:${toString (config.m3ta.ports.get "grafana")}";
|
|
|
|
};
|
|
|
|
locations."/prometheus/" = {
|
|
|
|
proxyPass = "http://127.0.0.1:${toString (config.m3ta.ports.get "prometheus")}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
```
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Container Services
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
virtualisation.oci-containers.containers.grafana = {
|
|
|
|
image = "grafana/grafana:latest";
|
|
|
|
ports = [
|
|
|
|
"${toString (config.m3ta.ports.get "grafana")}:3000"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
```
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Available Functions
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
| Function | Description | Example |
|
|
|
|
| ----------------------------------------------- | -------------------------- | ----------------------------------------------- |
|
|
|
|
| `config.m3ta.ports.get "service"` | Get port for current host | `config.m3ta.ports.get "nginx"` |
|
|
|
|
| `config.m3ta.ports.getForHost "host" "service"` | Get port for specific host | `config.m3ta.ports.getForHost "laptop" "nginx"` |
|
|
|
|
| `config.m3ta.ports.all` | All ports (merged) | `config.m3ta.ports.all` |
|
|
|
|
| `config.m3ta.ports.services` | List service names | `config.m3ta.ports.services` |
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Debugging
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### View all configured ports
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```bash
|
|
|
|
# NixOS
|
|
|
|
cat /etc/m3ta/ports.json | jq
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
# Home Manager
|
|
|
|
cat ~/.config/m3ta/ports.json | jq
|
|
|
|
```
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
### Check what port is being used
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
```nix
|
|
|
|
# Add to your config temporarily
|
|
|
|
environment.etc."debug-ports.txt".text = ''
|
|
|
|
nginx: ${toString (config.m3ta.ports.get "nginx")}
|
|
|
|
ssh: ${toString (config.m3ta.ports.get "ssh")}
|
|
|
|
all: ${builtins.toJSON config.m3ta.ports.all}
|
|
|
|
'';
|
|
|
|
```
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Next Steps
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
- See [examples/ports/README.md](examples/ports/README.md) for comprehensive documentation
|
|
|
|
- Check [examples/ports/nixos-example.nix](examples/ports/nixos-example.nix) for a full NixOS example
|
|
|
|
- Check [examples/ports/home-manager-example.nix](examples/ports/home-manager-example.nix) for a full Home Manager example
|
|
|
|
- Read [examples/ports/flake-example.nix](examples/ports/flake-example.nix) for multi-host setup
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Common Issues
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
**"Service not defined" error**
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
- Make sure the service is in your `definitions` block
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
**Wrong port being used**
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
- Check your `currentHost` matches your actual hostname
|
|
|
|
- Verify overrides in `/etc/m3ta/ports.json` or `~/.config/m3ta/ports.json`
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
**Type errors**
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
- Ports must be integers: `nginx = 80;` not `nginx = "80";`
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
## Need Help?
|
2025-10-04 15:53:48 +02:00
|
|
|
|
2025-10-05 09:44:40 +02:00
|
|
|
Open an issue at: https://code.m3ta.dev/m3tam3re/nixpkgs
|