diff --git a/flake.nix b/flake.nix index b83b587..e06aedd 100644 --- a/flake.nix +++ b/flake.nix @@ -219,17 +219,9 @@ }; in testPkgs.testers.runNixOSTest ./tests/run/synapse.nix; - - # NOTE: disabled for now since the test takes too long to execute - # open-webui-oci-test = - # let - # testPkgs = import nixpkgs { - # inherit system; - # }; - # in - # testPkgs.testers.runNixOSTest ./tests/run/open-webui-oci.nix; + open-webui-oci-test = pkgs.testers.runNixOSTest ./tests/run/open-webui-oci.nix; + # librechat-oci-test = pkgs.testers.runNixOSTest ./tests/run/librechat-oci.nix; # FIXME: unable to copy from source docker://quay.io/mongo:7.0 } - ); hydraJobs = { diff --git a/modules/nixos/default.nix b/modules/nixos/default.nix index 60c31b9..89d0f42 100644 --- a/modules/nixos/default.nix +++ b/modules/nixos/default.nix @@ -14,6 +14,7 @@ i2pd = import ./i2pd; jellyfin = import ./jellyfin; jirafeau = import ./jirafeau; + librechat-oci = import ./librechat-oci; mailserver = import ./mailserver; matrix-synapse = import ./matrix-synapse; maubot = import ./maubot; diff --git a/modules/nixos/librechat-oci/default.nix b/modules/nixos/librechat-oci/default.nix new file mode 100644 index 0000000..afbdd2b --- /dev/null +++ b/modules/nixos/librechat-oci/default.nix @@ -0,0 +1,247 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + cfg = config.services.librechat-oci; + + image = pkgs.dockerTools.pullImage { + imageName = "ghcr.io/danny-avila/librechat"; + imageDigest = "sha256:a46254938507971e0d4f7ed3f9d116bd9b118f4810b5b75eb716baf575645068"; + hash = "sha256-zevUN6vrs3hymwCGFmk/YXlUzYjN37H+EO5aLxYchyc="; + finalImageName = "ghcr.io/danny-avila/librechat"; + finalImageTag = "v0.8.5"; + }; + + defaultEnv = { + HOST = "0.0.0.0"; + PORT = "3080"; + NO_INDEX = "true"; + DEBUG_LOGGING = "false"; + CONSOLE_JSON = "false"; + ALLOW_REGISTRATION = "true"; + ALLOW_EMAIL_LOGIN = "true"; + SEARCH = "true"; + MEILI_NO_ANALYTICS = "true"; + }; + + inherit (lib) + literalExpression + mkEnableOption + mkIf + mkOption + mkOverride + types + ; +in +{ + options.services.librechat-oci = { + enable = mkEnableOption "LibreChat container with Podman."; + image = mkOption { + type = types.package; + default = image; + description = "The Docker image to use (`pkgs.dockerTools.pullImage`)."; + }; + externalUrl = mkOption { + type = types.nullOr types.str; + default = null; + example = literalExpression "http://${config.networking.domain}"; + description = "Public URL to configure for LibreChat."; + }; + port = mkOption { + type = types.port; + default = 3080; + description = "Which port the LibreChat server listens to."; + }; + environment = mkOption { + default = { }; + type = types.attrsOf types.str; + description = '' + Extra environment variables for LibreChat. + For more details see + ''; + }; + environmentFile = mkOption { + description = "Environment file to be passed to the LibreChat container."; + type = types.nullOr types.path; + default = null; + example = "config.sops.templates.librechat-env.path"; + }; + }; + + config = mkIf cfg.enable { + virtualisation.podman = { + enable = true; + autoPrune.enable = true; + dockerCompat = true; + }; + + networking.firewall.interfaces = + let + matchAll = if !config.networking.nftables.enable then "podman+" else "podman*"; + in + { + "${matchAll}".allowedUDPPorts = [ 53 ]; + }; + + virtualisation.oci-containers.backend = "podman"; + + virtualisation.oci-containers.containers."librechat-mongodb" = { + image = "mongo:7.0"; + environment = { + MONGO_INITDB_ROOT_USERNAME = "root"; + MONGO_INITDB_ROOT_PASSWORD = "librechat"; + MONGO_INITDB_DATABASE = "LibreChat"; + }; + volumes = [ + "librechat_mongodb_data:/data/db:rw" + ]; + log-driver = "journald"; + extraOptions = [ + "--network=host" + ]; + }; + + virtualisation.oci-containers.containers.librechat = { + image = toString cfg.image; + environment = + defaultEnv + // { + MONGO_URI = "mongodb://root:librechat@localhost:27017/LibreChat?authSource=admin"; + } + // cfg.environment; + volumes = [ + "librechat_data:/app/client/data:rw" + "librechat_images:/app/client/public/images:rw" + "librechat_uploads:/app/api/server/files/uploads:rw" + "librechat_logs:/app/logs:rw" + ]; + ports = [ + "${toString cfg.port}:${toString cfg.port}" + ]; + log-driver = "journald"; + extraOptions = [ + "--network=host" + ]; + }; + + systemd.services."podman-librechat-mongodb" = { + serviceConfig = { + Restart = mkOverride 90 "always"; + }; + after = [ + "podman-volume-librechat_mongodb_data.service" + ]; + requires = [ + "podman-volume-librechat_mongodb_data.service" + ]; + partOf = [ + "podman-compose-librechat-root.target" + ]; + wantedBy = [ + "podman-compose-librechat-root.target" + ]; + }; + + systemd.services."podman-librechat" = { + serviceConfig = { + Restart = mkOverride 90 "always"; + }; + after = [ + "podman-volume-librechat_data.service" + "podman-volume-librechat_images.service" + "podman-volume-librechat_uploads.service" + "podman-volume-librechat_logs.service" + "podman-librechat-mongodb.service" + ]; + requires = [ + "podman-volume-librechat_data.service" + "podman-volume-librechat_images.service" + "podman-volume-librechat_uploads.service" + "podman-volume-librechat_logs.service" + "podman-librechat-mongodb.service" + ]; + partOf = [ + "podman-compose-librechat-root.target" + ]; + wantedBy = [ + "podman-compose-librechat-root.target" + ]; + }; + + systemd.services."podman-volume-librechat_data" = { + path = [ pkgs.podman ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + podman volume inspect librechat_data || podman volume create librechat_data + ''; + partOf = [ "podman-compose-librechat-root.target" ]; + wantedBy = [ "podman-compose-librechat-root.target" ]; + }; + + systemd.services."podman-volume-librechat_images" = { + path = [ pkgs.podman ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + podman volume inspect librechat_images || podman volume create librechat_images + ''; + partOf = [ "podman-compose-librechat-root.target" ]; + wantedBy = [ "podman-compose-librechat-root.target" ]; + }; + + systemd.services."podman-volume-librechat_uploads" = { + path = [ pkgs.podman ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + podman volume inspect librechat_uploads || podman volume create librechat_uploads + ''; + partOf = [ "podman-compose-librechat-root.target" ]; + wantedBy = [ "podman-compose-librechat-root.target" ]; + }; + + systemd.services."podman-volume-librechat_logs" = { + path = [ pkgs.podman ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + podman volume inspect librechat_logs || podman volume create librechat_logs + ''; + partOf = [ "podman-compose-librechat-root.target" ]; + wantedBy = [ "podman-compose-librechat-root.target" ]; + }; + + systemd.services."podman-volume-librechat_mongodb_data" = { + path = [ pkgs.podman ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + podman volume inspect librechat_mongodb_data || podman volume create librechat_mongodb_data + ''; + partOf = [ "podman-compose-librechat-root.target" ]; + wantedBy = [ "podman-compose-librechat-root.target" ]; + }; + + systemd.targets."podman-compose-librechat-root" = { + unitConfig = { + Description = "Root target generated by compose2nix."; + }; + wantedBy = [ "multi-user.target" ]; + }; + }; +} diff --git a/tests/run/librechat-oci.nix b/tests/run/librechat-oci.nix new file mode 100644 index 0000000..70aabed --- /dev/null +++ b/tests/run/librechat-oci.nix @@ -0,0 +1,38 @@ +let + port = 3080; +in +{ + name = "librechat-oci-test"; + + nodes.machine = + { ... }: + { + imports = [ + ../../modules/nixos/librechat-oci + ]; + + config = { + virtualisation.diskSize = 32768; + virtualisation.memorySize = 8192; + + services.librechat-oci = { + enable = true; + inherit port; + }; + + networking.firewall.enable = false; + }; + }; + + testScript = '' + start_all() + + machine.wait_for_unit("default.target") + + machine.wait_for_unit("podman-librechat-mongodb.service") + machine.wait_for_unit("podman-librechat.service") + machine.wait_for_open_port(${toString port}) + + machine.succeed("curl --fail --retry 10 --retry-delay 5 --retry-connrefused http://localhost:${toString port}/") + ''; +}