|
2 | 2 |
|
3 | 3 | let |
4 | 4 | cfg = config.bloud.host-agent; |
| 5 | + postgresCfg = config.bloud.apps.postgres; |
5 | 6 |
|
6 | 7 | userHome = "/home/${cfg.user}"; |
7 | 8 | defaultDataDir = "${userHome}/.local/share/bloud"; |
8 | 9 |
|
| 10 | + # Build database URL from postgres config |
| 11 | + databaseURL = "postgres://${postgresCfg.user}:${postgresCfg.password}@localhost:5432/bloud?sslmode=disable"; |
| 12 | + |
9 | 13 | # For initial development, we'll use a manually built binary |
10 | 14 | # The binary should be built and placed at /tmp/host-agent |
11 | 15 | # Later: Use buildGoModule for proper Nix packaging |
|
40 | 44 | dataDir = lib.mkOption { |
41 | 45 | type = lib.types.str; |
42 | 46 | default = defaultDataDir; |
43 | | - description = "Directory for host agent data (SQLite, configs, catalog)"; |
| 47 | + description = "Directory for host agent data (configs, catalog)"; |
44 | 48 | }; |
45 | 49 | }; |
46 | 50 |
|
47 | 51 | config = lib.mkIf cfg.enable { |
48 | 52 | # Create data directories |
49 | 53 | system.activationScripts.bloud-host-agent-dirs = lib.stringAfter [ "users" ] '' |
50 | | - mkdir -p ${cfg.dataDir}/{state,nixos/apps,catalog} |
| 54 | + mkdir -p ${cfg.dataDir}/{nixos/apps,catalog} |
51 | 55 | chown -R ${cfg.user}:users ${cfg.dataDir} |
52 | 56 | ''; |
53 | 57 |
|
| 58 | + # Database initialization service |
| 59 | + systemd.services.bloud-host-agent-db-init = { |
| 60 | + description = "Initialize bloud host-agent database"; |
| 61 | + after = [ "podman-apps-postgres.service" ]; |
| 62 | + requires = [ "podman-apps-postgres.service" ]; |
| 63 | + before = [ "bloud-host-agent.service" ]; |
| 64 | + wantedBy = [ "multi-user.target" ]; |
| 65 | + |
| 66 | + serviceConfig = { |
| 67 | + Type = "oneshot"; |
| 68 | + RemainAfterExit = true; |
| 69 | + User = cfg.user; |
| 70 | + Group = "users"; |
| 71 | + ExecStart = pkgs.writeShellScript "bloud-db-init" '' |
| 72 | + set -e |
| 73 | +
|
| 74 | + # Wait for postgres to be ready |
| 75 | + echo "Waiting for PostgreSQL to be ready..." |
| 76 | + for i in $(seq 1 30); do |
| 77 | + if ${pkgs.podman}/bin/podman exec apps-postgres pg_isready -U ${postgresCfg.user} > /dev/null 2>&1; then |
| 78 | + echo "PostgreSQL is ready" |
| 79 | + break |
| 80 | + fi |
| 81 | + if [ $i -eq 30 ]; then |
| 82 | + echo "Timeout waiting for PostgreSQL" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + sleep 2 |
| 86 | + done |
| 87 | +
|
| 88 | + # Create database if not exists |
| 89 | + if ! ${pkgs.podman}/bin/podman exec apps-postgres psql -U ${postgresCfg.user} -tc "SELECT 1 FROM pg_database WHERE datname = 'bloud'" | grep -q 1; then |
| 90 | + echo "Creating bloud database..." |
| 91 | + ${pkgs.podman}/bin/podman exec apps-postgres psql -U ${postgresCfg.user} -c "CREATE DATABASE bloud" |
| 92 | + ${pkgs.podman}/bin/podman exec apps-postgres psql -U ${postgresCfg.user} -c "GRANT ALL PRIVILEGES ON DATABASE bloud TO ${postgresCfg.user}" |
| 93 | + echo "Database created successfully" |
| 94 | + else |
| 95 | + echo "Database bloud already exists" |
| 96 | + fi |
| 97 | + ''; |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
54 | 101 | # systemd service (system-wide, NOT user service) |
55 | 102 | # Runs as user but system-wide so it can manage system state |
56 | 103 | systemd.services.bloud-host-agent = { |
57 | 104 | description = "Bloud Host Agent - App Management & Web UI"; |
58 | | - after = [ "network-online.target" ]; |
| 105 | + after = [ "network-online.target" "bloud-host-agent-db-init.service" ]; |
59 | 106 | wants = [ "network-online.target" ]; |
| 107 | + requires = [ "bloud-host-agent-db-init.service" ]; |
60 | 108 | wantedBy = [ "multi-user.target" ]; |
61 | 109 |
|
62 | 110 | environment = { |
63 | 111 | BLOUD_PORT = toString cfg.port; |
64 | 112 | BLOUD_DATA_DIR = cfg.dataDir; |
| 113 | + DATABASE_URL = databaseURL; |
65 | 114 | }; |
66 | 115 |
|
67 | 116 | serviceConfig = { |
|
0 commit comments