Unverified Commit 15072f38 authored by Bruno BELANYI's avatar Bruno BELANYI Committed by GitHub
Browse files

nixos/homebox: add 'database.createLocally' (#396915)

parents 1d12b55d 52cb257e
Loading
Loading
Loading
Loading
+39 −8
Original line number Diff line number Diff line
@@ -35,6 +35,15 @@ in
        [documentation](https://homebox.software/en/configure-homebox.html).
      '';
    };
    database = {
      createLocally = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Configure local PostgreSQL database server for Homebox.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
@@ -43,16 +52,37 @@ in
      group = "homebox";
    };
    users.groups.homebox = { };
    services.homebox.settings = {
      HBOX_STORAGE_DATA = mkDefault "/var/lib/homebox/data";
      HBOX_DATABASE_DRIVER = mkDefault "sqlite3";
      HBOX_DATABASE_SQLITE_PATH = mkDefault "/var/lib/homebox/data/homebox.db?_pragma=busy_timeout=999&_pragma=journal_mode=WAL&_fk=1";
      HBOX_OPTIONS_ALLOW_REGISTRATION = mkDefault "false";
      HBOX_OPTIONS_CHECK_GITHUB_RELEASE = mkDefault "false";
      HBOX_MODE = mkDefault "production";
    services.homebox.settings = lib.mkMerge [
      (lib.mapAttrs (_: mkDefault) {
        HBOX_STORAGE_DATA = "/var/lib/homebox/data";
        HBOX_DATABASE_DRIVER = "sqlite3";
        HBOX_DATABASE_SQLITE_PATH = "/var/lib/homebox/data/homebox.db?_pragma=busy_timeout=999&_pragma=journal_mode=WAL&_fk=1";
        HBOX_OPTIONS_ALLOW_REGISTRATION = "false";
        HBOX_OPTIONS_CHECK_GITHUB_RELEASE = "false";
        HBOX_MODE = "production";
      })

      (lib.mkIf cfg.database.createLocally {
        HBOX_DATABASE_DRIVER = "postgres";
        HBOX_DATABASE_HOST = "/run/postgresql";
        HBOX_DATABASE_USERNAME = "homebox";
        HBOX_DATABASE_DATABASE = "homebox";
        HBOX_DATABASE_PORT = toString config.services.postgresql.settings.port;
      })
    ];
    services.postgresql = lib.mkIf cfg.database.createLocally {
      enable = true;
      ensureDatabases = [ "homebox" ];
      ensureUsers = [
        {
          name = "homebox";
          ensureDBOwnership = true;
        }
      ];
    };
    systemd.services.homebox = {
      after = [ "network.target" ];
      requires = lib.optional cfg.database.createLocally "postgresql.service";
      after = lib.optional cfg.database.createLocally "postgresql.service";
      environment = cfg.settings;
      serviceConfig = {
        User = "homebox";
@@ -82,6 +112,7 @@ in
        ProcSubset = "pid";
        ProtectSystem = "strict";
        RestrictAddressFamilies = [
          "AF_UNIX"
          "AF_INET"
          "AF_INET6"
          "AF_NETLINK"
+28 −11
Original line number Diff line number Diff line
@@ -8,19 +8,36 @@ import ./make-test-python.nix (
    meta = with pkgs.lib.maintainers; {
      maintainers = [ patrickdag ];
    };
    nodes.machine = {
    nodes =
      let
        self = {
          simple = {
            services.homebox = {
              enable = true;
              settings.HBOX_WEB_PORT = port;
            };
          };

          postgres = {
            imports = [ self.simple ];
            services.homebox.database.createLocally = true;
          };
        };
      in
      self;
    testScript = ''
      machine.wait_for_unit("homebox.service")
      machine.wait_for_open_port(${port})
      def test_homebox(node):
        node.wait_for_unit("homebox.service")
        node.wait_for_open_port(${port})

      machine.succeed("curl --fail -X GET 'http://localhost:${port}/'")
      out = machine.succeed("curl --fail 'http://localhost:${port}/api/v1/status'")
        node.succeed("curl --fail -X GET 'http://localhost:${port}/'")
        out = node.succeed("curl --fail 'http://localhost:${port}/api/v1/status'")
        assert '"health":true' in out

      test_homebox(simple)
      simple.send_monitor_command("quit")
      simple.wait_for_shutdown()
      test_homebox(postgres)
    '';
  }
)