From 5bf9ff997e82937989b74a202a1a3bf5a7904e08 Mon Sep 17 00:00:00 2001 From: Denis Manherz Date: Sat, 25 Nov 2023 23:39:37 +0100 Subject: [PATCH] Update --- flake.lock | 17 ++ flake.nix | 5 +- overlays/python/default.nix | 98 ++++---- packages/pillow/default.nix | 42 ++++ packages/pillow/generic.nix | 81 +++++++ packages/pytorch-seed/default.nix | 2 +- projects/automatic1111/_outputpaths.patch | 35 +++ projects/automatic1111/default.nix | 182 +++++++-------- projects/automatic1111/nixos/default.nix | 131 +++++++++++ projects/automatic1111/package.nix | 255 ++++++++------------ projects/bark-gui/default.nix | 81 ++++--- projects/bark-gui/package.nix | 110 ++++----- projects/kohya_ss/default.nix | 3 +- projects/kohya_ss/package.nix | 272 ++++++++++++++-------- setup.log | 1 + 15 files changed, 809 insertions(+), 506 deletions(-) create mode 100644 packages/pillow/default.nix create mode 100644 packages/pillow/generic.nix create mode 100644 projects/automatic1111/_outputpaths.patch create mode 100644 projects/automatic1111/nixos/default.nix create mode 100644 setup.log diff --git a/flake.lock b/flake.lock index 6effc13..a596135 100644 --- a/flake.lock +++ b/flake.lock @@ -215,6 +215,22 @@ "type": "github" } }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1700403855, + "narHash": "sha256-Q0Uzjik9kUTN9pd/kp52XJi5kletBhy29ctBlAG+III=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0c5678df521e1407884205fe3ce3cf1d7df297db", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1697059129, @@ -240,6 +256,7 @@ "invokeai-src": "invokeai-src", "kohya_ss-src": "kohya_ss-src", "nixpkgs": "nixpkgs_2", + "nixpkgs-stable": "nixpkgs-stable", "sd-src": "sd-src", "sgm-src": "sgm-src", "textgen-src": "textgen-src" diff --git a/flake.nix b/flake.nix index 13ca07b..2c738c1 100644 --- a/flake.nix +++ b/flake.nix @@ -7,6 +7,9 @@ description = "A Nix Flake that makes AI reproducible and easy to run"; inputs = { + nixpkgs-stable = { + url = github:NixOS/nixpkgs/nixos-23.05; + }; bark-gui-src = { url = "github:C0untFloyd/bark-gui"; flake = false; @@ -49,7 +52,7 @@ }; outputs = { flake-parts, invokeai-src, hercules-ci-effects, ... }@inputs: flake-parts.lib.mkFlake { inherit inputs; } { - perSystem = { system, ... }: { + perSystem = { system, ... }:{ _module.args.pkgs = import inputs.nixpkgs { config.allowUnfree = true; inherit system; }; legacyPackages = { koboldai = builtins.throw '' diff --git a/overlays/python/default.nix b/overlays/python/default.nix index 373fbd2..1ad43b8 100644 --- a/overlays/python/default.nix +++ b/overlays/python/default.nix @@ -1,51 +1,57 @@ lib: { - fixPackages = final: prev: let - relaxProtobuf = pkg: pkg.overrideAttrs (old: { - nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; - pythonRelaxDeps = [ "protobuf" ]; - }); - in { - invisible-watermark = prev.invisible-watermark.overridePythonAttrs { - pythonImportsCheck = [ ]; - }; - torchsde = prev.torchsde.overridePythonAttrs { doCheck = false; - pythonImportsCheck = []; }; - pytorch-lightning = relaxProtobuf prev.pytorch-lightning; - wandb = relaxProtobuf (prev.wandb.overridePythonAttrs { - doCheck = false; - }); - anyio = prev.anyio.overridePythonAttrs { doCheck = false; dontUsePytestCheck = true;}; - mocket = prev.mocket.overridePythonAttrs { doCheck = false; dontUsePytestCheck = true;}; - markdown-it-py = prev.markdown-it-py.overrideAttrs (old: { - nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; - pythonRelaxDeps = [ "linkify-it-py" ]; - passthru = old.passthru // { - optional-dependencies = with final; { - linkify = [ linkify-it-py ]; - plugins = [ mdit-py-plugins ]; + fixPackages = final: prev: + let + relaxProtobuf = pkg: pkg.overrideAttrs (old: { + nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; + pythonRelaxDeps = [ "protobuf" ]; + }); + in + { + invisible-watermark = prev.invisible-watermark.overridePythonAttrs { + pythonImportsCheck = [ ]; + }; + torchsde = prev.torchsde.overridePythonAttrs { + doCheck = false; + pythonImportsCheck = [ ]; + }; + pytorch-lightning = relaxProtobuf prev.pytorch-lightning; + wandb = relaxProtobuf (prev.wandb.overridePythonAttrs { + doCheck = false; + }); + anyio = prev.anyio.overridePythonAttrs { doCheck = false; dontUsePytestCheck = true; }; + mocket = prev.mocket.overridePythonAttrs { doCheck = false; dontUsePytestCheck = true; }; + markdown-it-py = prev.markdown-it-py.overrideAttrs (old: { + nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; + pythonRelaxDeps = [ "linkify-it-py" ]; + passthru = old.passthru // { + optional-dependencies = with final; { + linkify = [ linkify-it-py ]; + plugins = [ mdit-py-plugins ]; + }; }; - }; - }); - filterpy = prev.filterpy.overrideAttrs (old: { - doInstallCheck = false; - }); - shap = prev.shap.overrideAttrs (old: { - doInstallCheck = false; - propagatedBuildInputs = old.propagatedBuildInputs ++ [ final.packaging ]; - pythonImportsCheck = [ "shap" ]; + }); + filterpy = prev.filterpy.overrideAttrs (old: { + doInstallCheck = false; + }); + shap = prev.shap.overrideAttrs (old: { + doInstallCheck = false; + propagatedBuildInputs = old.propagatedBuildInputs ++ [ final.packaging ]; + pythonImportsCheck = [ "shap" ]; - meta = old.meta // { - broken = false; - }; - }); - streamlit = let - streamlit = final.callPackage (final.pkgs.path + "/pkgs/applications/science/machine-learning/streamlit") { - protobuf3 = final.protobuf; - }; - in final.toPythonModule (relaxProtobuf streamlit); - opencv-python-headless = final.opencv-python; - opencv-python = final.opencv4; - }; + meta = old.meta // { + broken = false; + }; + }); + streamlit = + let + streamlit = final.callPackage (final.pkgs.path + "/pkgs/applications/science/machine-learning/streamlit") { + protobuf3 = final.protobuf; + }; + in + final.toPythonModule (relaxProtobuf streamlit); + opencv-python-headless = final.opencv-python; + opencv-python = final.opencv4; + }; torchRocm = final: prev: { torch = prev.torch.override { @@ -54,7 +60,7 @@ lib: { rocmSupport = true; }; torchvision = prev.torchvision.overridePythonAttrs (old: { - patches = (old.patches or []) ++ [ ./torchvision/fix-rocm-build.patch ]; + patches = (old.patches or [ ]) ++ [ ./torchvision/fix-rocm-build.patch ]; }); }; diff --git a/packages/pillow/default.nix b/packages/pillow/default.nix new file mode 100644 index 0000000..6dc894c --- /dev/null +++ b/packages/pillow/default.nix @@ -0,0 +1,42 @@ +{ lib +, stdenv +, buildPythonPackage +, pythonOlder +, fetchPypi +, isPyPy +, defusedxml, olefile, freetype, libjpeg, zlib, libtiff, libwebp, libxcrypt, tcl, lcms2, tk, libX11 +, libxcb, openjpeg, libimagequant, pyroma, numpy, pytestCheckHook +# for passthru.tests +, imageio, matplotlib, pilkit, pydicom, reportlab +}@args: + +import ./generic.nix (rec { + pname = "pillow"; + version = "9.4.0"; + format = "setuptools"; + + disabled = pythonOlder "3.7"; + + src = fetchPypi { + pname = "Pillow"; + inherit version; + hash = "sha256-ocLXeARI65P7zDeJvzkWqlcg2ULjeUX0BWaAMX8c0j4="; + }; + + passthru.tests = { + inherit imageio matplotlib pilkit pydicom reportlab; + }; + + meta = with lib; { + homepage = "https://python-pillow.org/"; + description = "The friendly PIL fork (Python Imaging Library)"; + longDescription = '' + The Python Imaging Library (PIL) adds image processing + capabilities to your Python interpreter. This library + supports many file formats, and provides powerful image + processing and graphics capabilities. + ''; + license = licenses.hpnd; + maintainers = with maintainers; [ goibhniu prikhi SuperSandro2000 ]; + }; +} // args ) diff --git a/packages/pillow/generic.nix b/packages/pillow/generic.nix new file mode 100644 index 0000000..272f3d5 --- /dev/null +++ b/packages/pillow/generic.nix @@ -0,0 +1,81 @@ +{ pname +, version +, disabled +, src +, patches ? [] +, meta +, passthru ? {} +, ... +}@args: + +with args; + +buildPythonPackage rec { + inherit pname version format src meta passthru patches; + + # Disable imagefont tests, because they don't work well with infinality: + # https://github.com/python-pillow/Pillow/issues/1259 + postPatch = '' + rm Tests/test_imagefont.py + ''; + + disabledTests = [ + # Code quality mismathch 9 vs 10 + "test_pyroma" + + # pillow-simd + "test_roundtrip" + "test_basic" + "test_custom_metadata" + "test_file_png" + ] ++ lib.optionals stdenv.isDarwin [ + # Disable darwin tests which require executables: `iconutil` and `screencapture` + "test_grab" + "test_grabclipboard" + "test_save" + ]; + + propagatedBuildInputs = [ olefile ] + ++ lib.optionals (lib.versionAtLeast version "8.2.0") [ defusedxml ]; + + nativeCheckInputs = [ pytestCheckHook pyroma numpy ]; + + buildInputs = [ freetype libjpeg openjpeg libimagequant zlib libtiff libwebp libxcrypt tcl lcms2 ] + ++ lib.optionals (lib.versionAtLeast version "7.1.0") [ libxcb ] + ++ lib.optionals (isPyPy) [ tk libX11 ]; + + # NOTE: we use LCMS_ROOT as WEBP root since there is not other setting for webp. + # NOTE: The Pillow install script will, by default, add paths like /usr/lib + # and /usr/include to the search paths. This can break things when building + # on a non-NixOS system that has some libraries installed that are not + # installed in Nix (for example, Arch Linux has jpeg2000 but Nix doesn't + # build Pillow with this support). We patch the `disable_platform_guessing` + # setting here, instead of passing the `--disable-platform-guessing` + # command-line option, since the command-line option doesn't work when we run + # tests. + preConfigure = let + libinclude' = pkg: ''"${pkg.out}/lib", "${pkg.out}/include"''; + libinclude = pkg: ''"${pkg.out}/lib", "${pkg.dev}/include"''; + in '' + sed -i "setup.py" \ + -e 's|^FREETYPE_ROOT =.*$|FREETYPE_ROOT = ${libinclude freetype}|g ; + s|^JPEG_ROOT =.*$|JPEG_ROOT = ${libinclude libjpeg}|g ; + s|^JPEG2K_ROOT =.*$|JPEG2K_ROOT = ${libinclude openjpeg}|g ; + s|^IMAGEQUANT_ROOT =.*$|IMAGEQUANT_ROOT = ${libinclude' libimagequant}|g ; + s|^ZLIB_ROOT =.*$|ZLIB_ROOT = ${libinclude zlib}|g ; + s|^LCMS_ROOT =.*$|LCMS_ROOT = ${libinclude lcms2}|g ; + s|^TIFF_ROOT =.*$|TIFF_ROOT = ${libinclude libtiff}|g ; + s|^TCL_ROOT=.*$|TCL_ROOT = ${libinclude' tcl}|g ; + s|self\.disable_platform_guessing = None|self.disable_platform_guessing = True|g ;' + export LDFLAGS="$LDFLAGS -L${libwebp}/lib" + export CFLAGS="$CFLAGS -I${libwebp}/include" + '' + lib.optionalString (lib.versionAtLeast version "7.1.0") '' + export LDFLAGS="$LDFLAGS -L${libxcb}/lib" + export CFLAGS="$CFLAGS -I${libxcb.dev}/include" + '' + lib.optionalString stdenv.isDarwin '' + # Remove impurities + substituteInPlace setup.py \ + --replace '"/Library/Frameworks",' "" \ + --replace '"/System/Library/Frameworks"' "" + ''; +} diff --git a/packages/pytorch-seed/default.nix b/packages/pytorch-seed/default.nix index cb8d566..a76aa38 100644 --- a/packages/pytorch-seed/default.nix +++ b/packages/pytorch-seed/default.nix @@ -2,8 +2,8 @@ , buildPythonPackage , fetchPypi , setuptools -, torch , numpy +, torch }: buildPythonPackage rec { diff --git a/projects/automatic1111/_outputpaths.patch b/projects/automatic1111/_outputpaths.patch new file mode 100644 index 0000000..e9b488a --- /dev/null +++ b/projects/automatic1111/_outputpaths.patch @@ -0,0 +1,35 @@ +diff --git a/modules/shared_options.py b/modules/shared_options.py +index 00b273fa..9dd8e2ce 100644 +--- a/modules/shared_options.py ++++ b/modules/shared_options.py +@@ -4,6 +4,8 @@ from modules import localization, ui_components, shared_items, shared, interroga + from modules.paths_internal import models_path, script_path, data_path, sd_configs_path, sd_default_config, sd_model_file, default_sd_model_file, extensions_dir, extensions_builtin_dir # noqa: F401 + from modules.shared_cmd_options import cmd_opts + from modules.options import options_section, OptionInfo, OptionHTML ++from modules.paths_internal import data_path ++import os + + options_templates = {} + hide_dirs = shared.hide_dirs +@@ -66,14 +68,14 @@ options_templates.update(options_section(('saving-images', "Saving images/grids" + + options_templates.update(options_section(('saving-paths', "Paths for saving"), { + "outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to three directories below", component_args=hide_dirs), +- "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images', component_args=hide_dirs), +- "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs), +- "outdir_extras_samples": OptionInfo("outputs/extras-images", 'Output directory for images from extras tab', component_args=hide_dirs), ++ "outdir_txt2img_samples": OptionInfo(os.path.join(data_path,"outputs/txt2img-images"), 'Output directory for txt2img images', component_args=hide_dirs), ++ "outdir_img2img_samples": OptionInfo(os.path.join(data_path,"outputs/img2img-images"), 'Output directory for img2img images', component_args=hide_dirs), ++ "outdir_extras_samples": OptionInfo(os.path.join(data_path,"outputs/extras-images"), 'Output directory for images from extras tab', component_args=hide_dirs), + "outdir_grids": OptionInfo("", "Output directory for grids; if empty, defaults to two directories below", component_args=hide_dirs), +- "outdir_txt2img_grids": OptionInfo("outputs/txt2img-grids", 'Output directory for txt2img grids', component_args=hide_dirs), +- "outdir_img2img_grids": OptionInfo("outputs/img2img-grids", 'Output directory for img2img grids', component_args=hide_dirs), +- "outdir_save": OptionInfo("log/images", "Directory for saving images using the Save button", component_args=hide_dirs), +- "outdir_init_images": OptionInfo("outputs/init-images", "Directory for saving init images when using img2img", component_args=hide_dirs), ++ "outdir_txt2img_grids": OptionInfo(os.path.join(data_path,"outputs/txt2img-grids"), 'Output directory for txt2img grids', component_args=hide_dirs), ++ "outdir_img2img_grids": OptionInfo(os.path.join(data_path,"outputs/img2img-grids"), 'Output directory for img2img grids', component_args=hide_dirs), ++ "outdir_save": OptionInfo(os.path.join(data_path,"log/images"), "Directory for saving images using the Save button", component_args=hide_dirs), ++ "outdir_init_images": OptionInfo(os.path.join(data_path,"outputs/init-images"), "Directory for saving init images when using img2img", component_args=hide_dirs), + })) + + options_templates.update(options_section(('saving-to-dirs', "Saving to a directory"), { diff --git a/projects/automatic1111/default.nix b/projects/automatic1111/default.nix index a110fe1..28d2f67 100644 --- a/projects/automatic1111/default.nix +++ b/projects/automatic1111/default.nix @@ -6,107 +6,95 @@ let in { - perSystem = { config, pkgs, ... }: let - commonOverlays = [ - overlays.python-fixPackages - (l.overlays.callManyPackages [ - - ../../packages/discord-webhook - ../../packages/deforum - ../../packages/ultralytics - ../../packages/rich - ../../packages/k_diffusion - ../../packages/openclip - ../../packages/safetensors - ../../packages/easing-functions - ../../packages/dynamicprompts - ../../packages/controlnet-aux - ../../packages/fastapi - ../../packages/fastapi-events - ../../packages/fastapi-socketio - ../../packages/pytorch-lightning - ../../packages/starlette - ../../packages/compel - ../../packages/taming-transformers-rom1504 - ../../packages/albumentations - ../../packages/qudida - ../../packages/gfpgan - ../../packages/basicsr - ../../packages/facexlib - ../../packages/realesrgan - ../../packages/codeformer - ../../packages/clipseg - ../../packages/kornia - ../../packages/picklescan - ../../packages/diffusers - ../../packages/pypatchmatch - ../../packages/torch-fidelity - ../../packages/resize-right - ../../packages/torchdiffeq - ../../packages/accelerate - ../../packages/clip-anytorch - ../../packages/clean-fid - ../../packages/getpass-asterisk - ../../packages/mediapipe - ../../packages/python-engineio - ../../packages/lpips - ../../packages/blip - ../../packages/gradio - ../../packages/gradio-client - ../../packages/analytics-python - ../../packages/tomesd - ../../packages/blendmodes - ../../packages/xformers - ../../packages/zipunicode - ]) - (final: prev: lib.mapAttrs - (_: pkg: pkg.overrideAttrs (old: { - nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; - pythonRemoveDeps = [ "opencv-python-headless" "opencv-python" "tb-nightly" "clip" ]; - })) - { - inherit (prev) - albumentations - qudida - gfpgan - basicsr - facexlib - realesrgan - clipseg - ; - } - ) - ]; + perSystem = { config, pkgs, system, ... }: + let + commonOverlays = [ + #need pillow < 10.0.0 for scripts like x,y,z plot to work + #cant i do like only for this for invoke other version? + ( + final: prev: { + pillow = pkgs.python3.pkgs.callPackage ../../packages/pillow { }; + pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [ + ( + python-final: python-prev: { + pillow = python-final.callPackage ../../packages/pillow { }; + } + ) + ]; + } + ) + overlays.python-fixPackages + (l.overlays.callManyPackages [ + ../../packages/analytics-python + ../../packages/basicsr + ../../packages/blendmodes + ../../packages/blip + ../../packages/codeformer + ../../packages/facexlib + ../../packages/gfpgan + ../../packages/gradio + ../../packages/gradio-client + ../../packages/k_diffusion + ../../packages/lpips + ../../packages/openclip + ../../packages/pillow + ../../packages/pytorch-lightning + ../../packages/realesrgan + ../../packages/taming-transformers-rom1504 + ../../packages/tomesd + ../../packages/torch-fidelity + ../../packages/torch-grammar + ../../packages/xformers + ]) + (final: prev: lib.mapAttrs + (_: pkg: pkg.overrideAttrs (old: { + nativeBuildInputs = old.nativeBuildInputs ++ [ final.pythonRelaxDepsHook ]; + pythonRemoveDeps = [ "opencv-python-headless" "opencv-python" "tb-nightly" "clip" ]; + } + )) + { + inherit (prev) + gfpgan + basicsr + facexlib + realesrgan + ; + } + ) + ]; - python3Variants = { - nvidia = l.overlays.applyOverlays pkgs.python3Packages (commonOverlays ++ [ - overlays.python-torchCuda - ]); - }; + python3Variants = { + nvidia = l.overlays.applyOverlays pkgs.python3Packages (commonOverlays ++ [ + overlays.python-torchCuda + ]); + }; - src = inputs.a1111-src; - mkAutomatic1111Variant = args: pkgs.callPackage ./package.nix ({ inherit src; sd-src = inputs.sd-src; sgm-src = inputs.sgm-src; } // args); - in { - packages = { - a1111-nvidia = mkAutomatic1111Variant { - python3Packages = python3Variants.nvidia; + src = inputs.a1111-src; + mkAutomatic1111Variant = args: pkgs.callPackage ./package.nix ({ inherit src; sd-src = inputs.sd-src; sgm-src = inputs.sgm-src; } // args); + in + { + packages = { + a1111-nvidia = mkAutomatic1111Variant { + python3Packages = python3Variants.nvidia; + }; }; }; - }; - flake.nixosModules = let - packageModule = pkgAttrName: { pkgs, ... }: { - services.a1111.package = withSystem pkgs.system ( - { config, ... }: lib.mkOptionDefault config.packages.${pkgAttrName} - ); + flake.nixosModules = + let + packageModule = pkgAttrName: { pkgs, ... }: { + services.a1111.package = withSystem pkgs.system ( + { config, ... }: lib.mkOptionDefault config.packages.${pkgAttrName} + ); + }; + in + { + a1111 = ./nixos; + invokeai-nvidia = { + imports = [ + config.flake.nixosModules.invokeai + (packageModule "a1111-nvidia") + ]; + }; }; - in { - a1111 = ./nixos; - invokeai-nvidia = { - imports = [ - config.flake.nixosModules.invokeai - (packageModule "a1111-nvidia") - ]; - }; - }; } diff --git a/projects/automatic1111/nixos/default.nix b/projects/automatic1111/nixos/default.nix new file mode 100644 index 0000000..716f371 --- /dev/null +++ b/projects/automatic1111/nixos/default.nix @@ -0,0 +1,131 @@ +{ config, lib, ... }: + +let + inherit (lib) + mkIf mkOption mkEnableOption mkRenamedOptionModule types + escapeShellArgs flatten getExe mapAttrsToList + isBool isFloat isInt isList isString + floatToString optionalString + ; + + cfg = config.services.invokeai; +in + +{ + imports = map ({ old, new ? old }: mkRenamedOptionModule [ "services" "invokeai" old ] [ "services" "invokeai" "settings" new ]) [ + { old = "host"; } + { old = "port"; } + { old = "dataDir"; new = "root"; } + { old = "precision"; } + ]; + options.services.invokeai = { + enable = mkEnableOption "InvokeAI Web UI for Stable Diffusion"; + + package = mkOption { + description = "Which InvokeAI package to use."; + type = types.package; + }; + + user = mkOption { + description = "Which user to run InvokeAI as."; + default = "invokeai"; + type = types.str; + }; + + group = mkOption { + description = "Which group to run InvokeAI as."; + default = "invokeai"; + type = types.str; + }; + + settings = mkOption { + description = "Structured command line arguments."; + default = { }; + type = types.submodule { + freeformType = with types; let + atom = nullOr (oneOf [ + bool + str + int + float + ]); + in attrsOf (either atom (listOf atom)); + options = { + host = mkOption { + description = "Which IP address to listen on."; + default = "127.0.0.1"; + type = types.str; + }; + + port = mkOption { + description = "Which port to listen on."; + default = 9090; + type = types.port; + }; + + root = mkOption { + description = "Where to store InvokeAI's state."; + default = "/var/lib/invokeai"; + type = types.path; + }; + + precision = mkOption { + description = "Set model precision."; + default = "auto"; + type = types.enum [ "auto" "float32" "autocast" "float16" ]; + }; + }; + }; + }; + + extraArgs = mkOption { + description = "Additional raw command line arguments."; + default = []; + type = with types; listOf str; + }; + }; + + config = let + + cliArgs = (flatten (mapAttrsToList (n: v: + if v == null then [] + else if isBool v then [ "--${optionalString (!v) "no-"}${n}" ] + else if isInt v then [ "--${n}" "${toString v}" ] + else if isFloat v then [ "--${n}" "${floatToString v}" ] + else if isString v then ["--${n}" v ] + else if isList v then [ "--${n}" (toString v) ] + else throw "Unhandled type for setting \"${n}\"" + ) cfg.settings)) ++ cfg.extraArgs; + + in mkIf cfg.enable { + users.users = mkIf (cfg.user == "invokeai") { + invokeai = { + isSystemUser = true; + inherit (cfg) group; + }; + }; + users.groups = mkIf (cfg.group == "invokeai") { + invokeai = {}; + }; + systemd.services.invokeai = { + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + environment = { + HOME = "${cfg.settings.root}/.home"; + INVOKEAI_ROOT = "${cfg.settings.root}"; + NIXIFIED_AI_NONINTERACTIVE = "1"; + }; + serviceConfig = { + User = cfg.user; + Group = cfg.group; + ExecStart = "${getExe cfg.package} ${escapeShellArgs cliArgs}"; + PrivateTmp = true; + }; + }; + systemd.tmpfiles.rules = [ + "d '${cfg.settings.root}' 0755 ${cfg.user} ${cfg.group} - -" + "d '${cfg.settings.root}/configs' 0755 ${cfg.user} ${cfg.group} - -" + "d '${cfg.settings.root}/.home' 0750 ${cfg.user} ${cfg.group} - -" + ]; + }; +} diff --git a/projects/automatic1111/package.nix b/projects/automatic1111/package.nix index 16fede8..df78687 100644 --- a/projects/automatic1111/package.nix +++ b/projects/automatic1111/package.nix @@ -1,183 +1,114 @@ -{ python3Packages, -sd-src, -sgm-src, -# misc - lib - , src -# extra deps +{ python3Packages +, sd-src +, sgm-src +, # misc + lib +, src + # extra deps }: -let - submodel = pkg: python3Packages.${pkg} + "/lib/python3.10/site-packages"; - taming-transformers = submodel "taming-transformers-rom1504"; - k_diffusion = submodel "k_diffusion"; - codeformer = (submodel "codeformer") + "/codeformer"; - blip = (submodel "blip") + "/blip"; - in python3Packages.buildPythonPackage { pname = "Automatic1111"; format = "other"; version = "v1.6.0"; inherit src; propagatedBuildInputs = with python3Packages; [ - moviepy #for prompt travel - zipunicode #for civitaui browser+ - imageio #animatediff create webm and shit - av #animatediff create webm and shit - discord-webhook #for dreambooth i think - numexpr #for some extension - deforum #for some extension - ultralytics #extension - k_diffusion - inflection - gdown - altair - aiofiles - openclip - semver - numpy - rich #extension - torchsde - uvicorn - pyperclip - invisible-watermark - fastapi - fastapi-events - fastapi-socketio - timm - scikit-image - controlnet-aux - compel - python-dotenv - uvloop - watchfiles - httptools - websockets - dnspython - albumentations - opencv4 - pudb - imageio - imageio-ffmpeg - compel - npyscreen - pytorch-lightning - protobuf - omegaconf - test-tube - einops - taming-transformers-rom1504 - torch-fidelity - torchmetrics - transformers - kornia - k-diffusion - picklescan - diffusers - pypatchmatch - realesrgan - pillow - send2trash - flask - flask-cors - dependency-injector - gfpgan - eventlet - clipseg - getpass-asterisk - safetensors - datasets - accelerate - easing-functions - dynamicprompts - torchvision - test-tube - lark - gradio - gradio-client - tomesd - piexif - blendmodes - xformers - python-multipart - ]; - nativeBuildInputs = with python3Packages; [ pythonRelaxDepsHook pip ]; - pythonRemoveDeps = [ "clip" "pyreadline3" "flaskwebgui" "opencv-python" ]; - pythonRelaxDeps = [ "dnspython" "flask" "requests" "numpy" "pytorch-lightning" "torchsde" "uvicorn" "invisible-watermark" "accelerate" "scikit-image" "safetensors" "torchvision" "test-tube" "fastapi" ]; + aiofiles + analytics-python + altair + blendmodes + codeformer + einops + facexlib + gfpgan + gradio + gradio-client + inflection + k-diffusion + kornia + lark + openclip + omegaconf + piexif + pytorch-lightning + realesrgan + safetensors + semantic-version + taming-transformers-rom1504 + timm + tomesd + torch + transformers + xformers + ]; - dontUsePytestCheck = true; + patches = [ ./_outputpaths.patch ]; - doCheck = false; + buildPhase = + '' + runHook preBuild + + mkdir -p dist + cp -r . $out + chmod -R +w $out + cd $out - buildPhase = '' - runHook preBuild - mkdir -p dist - cp -r . $out - chmod -R +w $out - cd $out - - #replace dirs in paths_internal.py - #mkdir -p /var/lib/.webui - sed -i 's#os\.path\.join(script_path, "config_states")#os\.path\.join(data_path, "config_states")#' ./modules/paths_internal.py - #delete lines trying to pull git repos and setting up tests in launch.py - sed -i '28,43d' launch.py + sed -i '28,43d' launch.py - #firstly, we need to make launch.py runnable by adding python shebang - cat <<-EOF > exec_launch.py.unwrapped - $(echo "#!/usr/bin/python") - $(cat launch.py) - EOF - chmod +x exec_launch.py.unwrapped + substituteInPlace ./modules/paths_internal.py \ + --replace 'os.path.join(script_path, "config_states")' \ + 'os.path.join(data_path, "config_states")' + + substituteInPlace ./modules/shared_gradio_themes.py \ + --replace script_path data_path - #creating wrapper around launch.py with PYTHONPATH correctly set - makeWrapper "$(pwd)/exec_launch.py.unwrapped" exec_launch.py \ - --set-default PYTHONPATH $PYTHONPATH + #make launch.py executable > shebang + mkdir -p $out/bin + cat <<-EOF > launch.py + $(echo "#!/usr/bin/python") + $(cat launch.py) + EOF + chmod +x launch.py + makeWrapper "$out/launch.py" $out/bin/launch-wrapped.py \ + --run 'export COMMANDLINE_ARGS="''${COMMANDLINE_ARGS:-\ + --data-dir $HOME/.webui --skip-install \ + --theme dark --ckpt-dir $HOME/.webui/models/ckpt \ + --embeddings-dir $HOME/.webui/models/embeddings \ + --medvram --no-half-vae}"' \ + --set-default PYTHONPATH $PYTHONPATH \ + --chdir $out - mkdir $out/bin - pushd $out/bin - ln -s ../exec_launch.py launch.py - buck='$' #escaping $ inside shell inside shell is tricky - #next is an additional shell wrapper, which sets sensible default args for CLI - #additional arguments will be passed further - cat <<-EOF > flake-launch - #!/usr/bin/env bash - pushd $out #For some reason, fastapi only works when current workdir is set inside the repo - trap "popd" EXIT + rm -rf dist - "$out/bin/launch.py" --skip-install "$buck{@}" - EOF - # below lie remnants of my attempt to make webui use similar paths as InvokeAI for models download - # additions of such options in upstream is a welcome sign, however they're mostly ignored and therefore useless - # TODO: check in 6 months, maybe it'll work - # For now, your best bet is to use ZFS dataset with dedup enabled or make symlinks after the fact - - #--codeformer-models-path "\$mp/codeformer" \ - #--gfpgan-models-path "\$mp/gfpgan" --esrgan-models-path "\$mp/esrgan" \ - #--bsrgan-models-path "\$mp/bsrgan" --realesrgan-models-path "\$mp/realesrgan" \ - #--clip-models-path "\$mp/clip" - chmod +x flake-launch - popd + runHook postBuild + ''; - runHook postBuild - ''; - installPhase = '' - runHook preInstall + installPhase = + let + submodel = pkg: python3Packages.${pkg} + "/lib/python3.10/site-packages"; + taming-transformers = submodel "taming-transformers-rom1504"; + k_diffusion = submodel "k_diffusion"; + codeformer = (submodel "codeformer") + "/codeformer"; + blip = (submodel "blip") + "/blip"; + in + '' + runHook preInstall - rm -rf repositories/ - mkdir repositories - pushd repositories - ln -s ${sd-src}/ stable-diffusion-stability-ai - ln -s ${sgm-src}/ generative-models - ln -s ${taming-transformers}/ taming-transformers - ln -s ${k_diffusion}/ k-diffusion - ln -s ${codeformer}/ CodeFormer - ln -s ${blip}/ BLIP - popd - echo $PATH - runHook postInstall - ''; + rm -rf repositories/ + mkdir repositories + pushd repositories + ln -s ${sd-src}/ stable-diffusion-stability-ai + ln -s ${sgm-src}/ generative-models + ln -s ${taming-transformers}/ taming-transformers + ln -s ${k_diffusion}/ k-diffusion + ln -s ${codeformer}/ CodeFormer + ln -s ${blip}/ BLIP + popd + + runHook postInstall + ''; meta = { description = "Fancy Web UI for Stable Diffusion"; homepage = "https://github.com/AUTOMATIC1111/stable-diffusion-webui"; - mainProgram = "flake-launch"; + mainProgram = "launch-wrapped.py"; }; } diff --git a/projects/bark-gui/default.nix b/projects/bark-gui/default.nix index f4ac45e..ec49cb8 100644 --- a/projects/bark-gui/default.nix +++ b/projects/bark-gui/default.nix @@ -1,44 +1,51 @@ + { config, inputs, lib, withSystem, ... }: +let + l = lib // config.flake.lib; + inherit (config.flake) overlays; +in { - perSystem = { config, pkgs, ... }: - let - src = inputs.bark-gui-src; - overlays = [ - ( - final: prev: { - final.python310 = prev.python310.override { - enableOptimizations = true; - reproducibleBuild = false; - self = final.python310; - buildInputs = [ final.ffmpeg-full ]; - }; - pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [ - ( - python-final: python-prev: { - pytorch-seed = python-final.callPackage ../../Packages/pytorch-seed { }; - audiolm-pytorch = python-final.callPackage ../../Packages/audiolm-pytorch { }; - vector-quantize-pytorch = python-final.callPackage ../../Packages/vector-quantize-pytorch { }; - local-attention = python-final.callPackage ../../Packages/local-attention { }; - ema-pytorch = python-final.callPackage ../../Packages/ema-pytorch { }; + perSystem = { config, pkgs, ... }: let + commonOverlays = [ + overlays.python-fixPackages + (l.overlays.callManyPackages [ + ../../packages/audiolm-pytorch + ../../packages/ema-pytorch + ../../packages/local-attention + ../../packages/pytorch-seed + ../../packages/vector-quantize-pytorch + ]) + ]; - openai-triton = python-prev.openai-triton-bin; - torch = python-prev.torch-bin; - torchaudio = python-prev.torchaudio-bin; + python3Variants = { + nvidia = l.overlays.applyOverlays pkgs.python3Packages (commonOverlays ++ [ + overlays.python-torchCuda + ]); + }; - #bark-gui = python-final.callPackage ../../Packages/bark-gui.nix { }; - } - ) - ]; - } - ) - ]; - mkbark-guiVariant = args: pkgs.callPackage ./package.nix ({ inherit src; } // args); - in - { - packages = { - bark-gui = mkbark-guiVariant { - inherit overlays; - }; + src = inputs.bark-gui-src; + mkbark-guiVariant = args: pkgs.callPackage ./package.nix ({ inherit src; } // args); + in { + packages = { + bark-gui-nvidia = mkbark-guiVariant { + python3Packages = python3Variants.nvidia; }; }; + }; + + flake.nixosModules = let + packageModule = pkgAttrName: { pkgs, ... }: { + services.a1111.package = withSystem pkgs.system ( + { config, ... }: lib.mkOptionDefault config.packages.${pkgAttrName} + ); + }; + in { + bark-gui = ./nixos; + invokeai-nvidia = { + imports = [ + config.flake.nixosModules.invokeai + (packageModule "bark-gui-nvidia") + ]; + }; + }; } diff --git a/projects/bark-gui/package.nix b/projects/bark-gui/package.nix index a344c0c..3c88e3c 100644 --- a/projects/bark-gui/package.nix +++ b/projects/bark-gui/package.nix @@ -1,77 +1,63 @@ -{ lib -, buildPythonPackage -, fetchFromGitHub -, writeShellScript -, setuptools - - # dependencies -, fairseq -, audiolm-pytorch -, gradio -, funcy -, linkify-it-py -, mutagen -, pytorch-seed -, pyyaml -, sentencepiece -, transformers - -, ffmpeg-full - +{ python3Packages +, # misc + lib , src + # extra deps }: - -buildPythonPackage rec { +python3Packages.buildPythonPackage { pname = "bark-gui"; - format = "pyproject"; - version = "0.7.1"; - - propagatedBuildInputs = [ - fairseq + format = "setuptools"; + version = "07.1"; + inherit src; + propagatedBuildInputs = with python3Packages; [ audiolm-pytorch - gradio + boto3 + ema-pytorch + encodec funcy - linkify-it-py - mutagen + gradio + local-attention pytorch-seed - pyyaml - sentencepiece + safetensors + scipy + torch-bin + torchaudio-bin transformers + vector-quantize-pytorch ]; - buildInputs = [ - setuptools - ffmpeg-full + #nativeBuildInputs = with python3Packages; [ pythonRelaxDepsHook pip ]; + nativeBuildInputs = with python3Packages; [ setuptools pip ]; + #pythonRemoveDeps = [ "clip" "pyreadline3" "flaskwebgui" "opencv-python" ]; + pythonRelaxDeps = [ "dnspython" "flask" "requests" "numpy" "pytorch-lightning" "torchsde" "uvicorn" "invisible-watermark" "accelerate" "scikit-image" "safetensors" "torchvision" "test-tube" "fastapi" ]; + + makeWrapperArgs = [ + '' --set-default PYTHONPATH=$PYTHONPATH '' ]; - dontWrapPythonPrograms = true; - postFixup = - let - setupScript = writeShellScript "bark-gui" '' - if [[ ! -d $HOME/.bark-gui ]]; then - mkdir -p $HOME - cp -r ${src} $HOME/.bark-gui - chmod 0755 $HOME/.bark-gui - chmod 0644 $HOME/.bark-gui/config.yaml - mkdir -p $HOME/.bark-gui/training/data/output - mkdir -p $HOME/.bark-gui/training/inputtext - chmod 755 $HOME/.bark-gui/training/data/output $HOME/.bark-gui/training/inputtext $HOME/.bark-gui/bark/assets/prompts/custom/ - fi - pushd "$PWD" - cd $HOME/.bark-gui/ - MYOLDPATH="$PATH" - export PATH="$PATH:${ffmpeg-full.bin}/bin/" - python webui.py "$@" - export PATH="$MYOLDPATH" - popd - ''; - in - '' - mkdir -p $out/bin - ln -s ${setupScript} $out/bin/bark-gui - ''; + buildPhase = '' + mkdir -p dist + runHook preBuild + cp -r . $out + chmod -R +w $out + cd $out + + chmod +x webui.py + #add shbang to webui.py + cat <<-EOF > webui.py + $(echo "#!/usr/bin/python") + $(cat webui.py) + EOF + + mkdir -p $out/bin + ln -s webui-wrapped.py $out/bin/bark-gui + makeWrapper "$(pwd)/webui.py" "$out/bin/bark-gui" --set-default PYTHONPATH=$PYTHONPATH + chmod +x $out/bin/bark-gui + + runHook postBuild + ''; meta = { - description = "A Gradio Web UI for an extended - easy to use - Bark Version"; + description = "A Gradio Web UI for an extended - easy to use - Bark Version."; homepage = "https://github.com/C0untFloyd/bark-gui"; mainProgram = "bark-gui"; }; diff --git a/projects/kohya_ss/default.nix b/projects/kohya_ss/default.nix index 74b3711..b756ff6 100644 --- a/projects/kohya_ss/default.nix +++ b/projects/kohya_ss/default.nix @@ -84,11 +84,10 @@ in src = inputs.kohya_ss-src; - mkkohya_ssVariant = args: pkgs.callPackage ./package.nix ({ inherit src; } // args); + mkkohya_ssVariant = args: python3Variants.nvidia.callPackage ./package.nix ({ inherit src; } // args); in { packages = { kohya_ss-nvidia = mkkohya_ssVariant { - python3Packages = python3Variants.nvidia; }; }; legacyPackages = { diff --git a/projects/kohya_ss/package.nix b/projects/kohya_ss/package.nix index 92ba976..aa329ed 100644 --- a/projects/kohya_ss/package.nix +++ b/projects/kohya_ss/package.nix @@ -1,114 +1,190 @@ -{ python3Packages - , lib - , src +{ lib +, pythonRelaxDepsHook +, pip +, src +, deforum +, inflection +, gdown +, altair +, aiofiles +, openclip +, semver +, numpy +, torchsde +, uvicorn +, pyperclip +, invisible-watermark +, fastapi +, fastapi-events +, fastapi-socketio +, timm +, scikit-image +, controlnet-aux +, compel +, python-dotenv +, uvloop +, watchfiles +, httptools +, websockets +, dnspython +, albumentations +, opencv4 +, pudb +, imageio +, imageio-ffmpeg +, npyscreen +, pytorch-lightning +, protobuf +, omegaconf +, test-tube +, einops +, taming-transformers-rom1504 +, torch-fidelity +, torchmetrics +, transformers +, kornia +, k-diffusion +, picklescan +, diffusers +, pypatchmatch +, realesrgan +, pillow +, send2trash +, flask +, flask-cors +, dependency-injector +, gfpgan +, eventlet +, clipseg +, getpass-asterisk +, safetensors +, datasets +, accelerate +, easing-functions +, dynamicprompts +, torchvision +, lark +, gradio +, gradio-client +, tomesd +, piexif +, blendmodes +, poetry-core +, xformers +, python-multipart +, buildPythonPackage +, easygui +, rich }: -python3Packages.buildPythonPackage { +buildPythonPackage { pname = "kohya_ss"; - format = "other"; + format = "pyproject"; version = "v22.1.1"; inherit src; - propagatedBuildInputs = with python3Packages; [ - deforum - inflection - gdown - altair - aiofiles - openclip - semver - numpy - torchsde - uvicorn - pyperclip - invisible-watermark - fastapi - fastapi-events - fastapi-socketio - timm - scikit-image - controlnet-aux - compel - python-dotenv - uvloop - watchfiles - httptools - websockets - dnspython - albumentations - opencv4 - pudb - imageio - imageio-ffmpeg - compel - npyscreen - pytorch-lightning - protobuf - omegaconf - test-tube - einops - taming-transformers-rom1504 - torch-fidelity - torchmetrics - transformers - kornia - k-diffusion - picklescan - diffusers - pypatchmatch - realesrgan - pillow - send2trash - flask - flask-cors - dependency-injector - gfpgan - eventlet - clipseg - getpass-asterisk - safetensors - datasets - accelerate - easing-functions - dynamicprompts - torchvision - test-tube - lark - gradio - gradio-client - tomesd - piexif - blendmodes - xformers - python-multipart - ]; - nativeBuildInputs = with python3Packages; [ pythonRelaxDepsHook pip ]; + propagatedBuildInputs = [ + deforum + rich + inflection + gdown + altair + aiofiles + openclip + semver + numpy + torchsde + uvicorn + pyperclip + invisible-watermark + fastapi + fastapi-events + fastapi-socketio + timm + scikit-image + controlnet-aux + compel + python-dotenv + uvloop + watchfiles + httptools + websockets + dnspython + albumentations + opencv4 + pudb + imageio + imageio-ffmpeg + compel + npyscreen + pytorch-lightning + protobuf + omegaconf + test-tube + einops + taming-transformers-rom1504 + torch-fidelity + torchmetrics + transformers + kornia + k-diffusion + picklescan + diffusers + pypatchmatch + realesrgan + pillow + send2trash + flask + flask-cors + dependency-injector + gfpgan + eventlet + clipseg + getpass-asterisk + safetensors + datasets + accelerate + easing-functions + dynamicprompts + torchvision + test-tube + lark + gradio + gradio-client + tomesd + piexif + blendmodes + poetry-core + xformers + python-multipart + easygui + ]; + nativeBuildInputs = [ pythonRelaxDepsHook pip ]; pythonRemoveDeps = [ "clip" "pyreadline3" "flaskwebgui" "opencv-python" ]; pythonRelaxDeps = [ "dnspython" "flask" "requests" "numpy" "pytorch-lightning" "torchsde" "uvicorn" "invisible-watermark" "accelerate" "scikit-image" "safetensors" "torchvision" "test-tube" "fastapi" ]; - - dontUsePytestCheck = true; - doCheck = false; - + dontUsePytestCheck = true; buildPhase = '' - runHook preBuild - mkdir -p dist - cp -r . $out - chmod -R +w $out - cd $out + runHook preBuild + mkdir -p dist + mkdir -p $out + cp -r . $out + cd $out - --run setup.sh - mkdir -p $out/bin - mv gui.sh $out/bin/gui.sh + mkdir -p $out/bin + sed -i "2i echo $OSTYPE" $out/setup.sh + chmod +x kohya_gui.py + sed -i '10d' $out/library/localization.py + sed -i "10i\ \ \ \ dirname = '$out/localizations'" $out/library/localization.py + sed -i "1i #!/bin/python" $out/kohya_gui.py + echo "$(python --version)" + makeWrapper $out/kohya_gui.py $out/bin/gui_wrapped.py --set-default PYTHONPATH $PYTHONPATH + chmod +x $out/bin/gui_wrapped.py - runHook postBuild - ''; - installPhase = '' - runHook preInstall + runHook postBuild + ''; - runHook postInstall - ''; meta = { description = "GUI for Kohya's Stable Diffusion trainers"; homepage = "https://github.com/bmaltais/kohya_ss.git"; - mainProgram = "gui.sh"; + mainProgram = "gui_wrapped.py"; }; } diff --git a/setup.log b/setup.log new file mode 100644 index 0000000..604d628 --- /dev/null +++ b/setup.log @@ -0,0 +1 @@ +2023-11-23 20:07:53,586 | INFO | /nix/store/6ccgy8vmpimlwx8cjqgrpwgpfdg0m3id-python3.10-kohya_ss-v22.1.1/kohya_gui.py | headless: False