Commit Graph

2 Commits

Author SHA1 Message Date
Tilman Baumann
18996cdd78 Adding webviewer nixos option and improving tests
Use with
services.meshcore-bot = {
  enable = true;
  webviewer.enable = true;
 }
2026-01-08 19:17:18 +01:00
Tilman Baumann
a6c56ecd3c Add Nix flake with NixOS module and modernize Python packaging
This commit adds comprehensive Nix/NixOS support and modernizes the
Python packaging structure to enable declarative system-wide deployments.

Added complete Nix flake infrastructure in nix/:
- packages.nix: Package definition with all dependencies
- nixos-module.nix: NixOS module for declarative service configuration
- nixos-test.nix: Comprehensive NixOS VM tests (basic + web viewer)
- shell.nix: Development shell with all build tools

The NixOS module (services.meshcore-bot) provides:
- Automatic user/group creation (meshcore-bot:meshcore-bot)
- Serial port access (dialout group membership)
- Systemd service with security hardening
- Automatic directory management via StateDirectory/LogsDirectory
- INI config generation from Nix attribute sets
- Sensible defaults for system paths

Example NixOS configuration:
  services.meshcore-bot = {
    enable = true;
    settings = {
      Connection.connection_type = "serial";
      Connection.serial_port = "/dev/ttyUSB0";
      Bot.bot_name = "MyBot";
    };
  };

Migrated from requirements.txt to pyproject.toml:
- Defined proper package metadata and dependencies
- Created entry points: meshcore-bot, meshcore-viewer
- Enables standard 'pip install -e .' workflow
- Maintains compatibility with existing setups

Changes to existing code are minimal and only where necessary:

1. **Added --config parameter** (meshcore_bot.py, app.py)
   - Wrapped main logic in main() function for entry point
   - Added argparse to accept --config parameter
   - Required for NixOS to pass generated config from /nix/store
   - Backwards compatible: defaults to 'config.ini' in current directory
   - No changes to core bot logic

2. **Fixed web viewer subprocess spawning** (modules/web_viewer/integration.py)
   - Changed from sys.executable to 'meshcore-viewer' entry point
   - Ensures Nix wrapper script sets up correct PYTHONPATH
   - Resolves Flask module import issues in Nix environment
   - No functional change for traditional installations

Note: The dev branch's resolve_path() utility already handles both
relative and absolute paths correctly, making it compatible with
NixOS system paths (/var/lib, /var/log) without additional changes.

All NixOS VM tests passing:
- nixos-module-basic: Service startup, file creation, TCP connection
- nixos-module-webviewer: Flask available, port listening, HTTP responses

No breaking changes to existing workflows. The bot continues to work
exactly as before when run with traditional Python methods.
2026-01-07 14:01:39 +01:00