commit 47f98adfe9b414e323da612b3bde3740586e39ea Author: niten Date: Mon May 20 17:35:25 2024 -0700 Initial commit diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..f7774bb --- /dev/null +++ b/flake.nix @@ -0,0 +1,17 @@ +{ + description = "Immich Machine Learning running in a container"; + + inputs = { + nixpkgs.url = "nixpkgs/nixos-23.11"; + arion.url = "github:hercules-ci/arion"; + }; + + outputs = { self, nixpkgs, arion, ... }: { + nixosModules = rec { + default = immichMlContainer; + immichMlContainer = { ... }: { + imports = [ arion.nixosModules.arion ./immich-ml-container.nix ]; + }; + }; + }; +} diff --git a/immich-ml-container.nix b/immich-ml-container.nix new file mode 100644 index 0000000..b6c110c --- /dev/null +++ b/immich-ml-container.nix @@ -0,0 +1,67 @@ +{ config, lib, pkgs, ... }: + +let cfg = config.services.immichMlContainer; + +in { + options.services.immichMlContainer = with types; { + enable = mkEnableOption "Enable machine learning container."; + + hostnames = mkOption { + type = listOf str; + description = "List of hostnames at which this container can be reached."; + }; + + port = mkOption { + type = port; + description = "Port on which to listen for connections."; + }; + + state-directory = mkOption { + type = str; + description = "Path on which to store service state."; + }; + + immich-version = mkOption { + type = str; + description = ""; + }; + + # TODO: maybe have different types? + }; + + config = { + systemd.tmpfiles.rules = [ "d ${cfg.state-directory} 0750 root root - -" ]; + + virtualisation.arion.projects.immich.settings = let + image = { ... }: { + project.name = "immich-ml"; + services.immich-ml = { + image.rawConfig.deploy.resoruces.reservations.devices = [{ + driver = "nvidia"; + count = 1; + capabilities = [ "gpu" ]; + }]; + service = { + image = + "ghcr.io/immich-app/immich-machine-learning:${cfg.immich-version}-cuda"; + restart = "always"; + ports = [ "${toString cfg.port}:3003" ]; + volumes = [ "${cfg.state-directory}:/cache" ]; + }; + }; + }; + in { imports = [ image ]; }; + + services.nginx = { + enable = true; + virtualHosts = genAttrs cfg.hostnames (hostname: { + enableACME = false; + forceSSL = false; + locations."/" = { + proxyPass = "http://127.0.0.1:${toString cfg.port}"; + recommendedProxySettings = true; + }; + }); + }; + }; +}