1{
2 lib,
3 buildPythonPackage,
4 fetchPypi,
5
6 # build-system
7 setuptools,
8 setuptools-scm,
9
10 # dependencies
11 bitsandbytes,
12 numpy,
13 packaging,
14 torch,
15 unsloth-zoo,
16 xformers,
17 tyro,
18 transformers,
19 datasets,
20 sentencepiece,
21 tqdm,
22 accelerate,
23 trl,
24 peft,
25 protobuf,
26 huggingface-hub,
27 hf-transfer,
28 diffusers,
29 torchvision,
30
31 # tests
32 fetchFromGitHub,
33 cudaPackages,
34 python,
35 gcc,
36}:
37
38let
39 # Test files are absent from the PyPI package, so we fetch them separately.
40 testSrc = fetchFromGitHub {
41 owner = "unslothai";
42 repo = "unsloth";
43 rev = "cb78f0e83dc2d61fb1571b6e904eb2f064510d63";
44 hash = "sha256-0oR3m8jnjSdfjH+NslW6SsVj+0cQ4VUhKXZ38U/VBy0=";
45 # Keep only the tests directory, use the PyPI package for everything else.
46 postFetch = ''
47 mv $out/tests $TMPDIR/tests
48 rm -rf $out/*
49 mv $TMPDIR/tests $out/tests
50 '';
51 };
52in
53
54buildPythonPackage rec {
55 pname = "unsloth";
56 version = "2025.9.4";
57 pyproject = true;
58
59 # Tags on the GitHub repo don't match
60 src = fetchPypi {
61 pname = "unsloth";
62 inherit version;
63 hash = "sha256-aT/RS48hBMZT1ab1Rx1lpSMi6yyEzJCASzDAP0d6ixA=";
64 };
65
66 build-system = [
67 setuptools
68 setuptools-scm
69 ];
70
71 dependencies = [
72 bitsandbytes
73 numpy
74 packaging
75 torch
76 unsloth-zoo
77 xformers
78 tyro
79 transformers
80 datasets
81 sentencepiece
82 tqdm
83 accelerate
84 trl
85 peft
86 protobuf
87 huggingface-hub
88 hf-transfer
89 diffusers
90 torchvision
91 ];
92
93 # pyproject.toml requires an obsolete version of protobuf,
94 # but it is not used.
95 # Upstream issue: https://github.com/unslothai/unsloth-zoo/pull/68
96 pythonRelaxDeps = [
97 "datasets"
98 "protobuf"
99 "transformers"
100 "torch"
101 ];
102
103 # The source repository contains no test
104 doCheck = false;
105
106 # Importing requires a GPU, else the following error is raised:
107 # NotImplementedError: Unsloth: No NVIDIA GPU found? Unsloth currently only supports GPUs!
108 dontUsePythonImportsCheck = true;
109
110 passthru.tests = {
111 qlora-train-and-merge =
112 # FIXME: Replace python3.pkgs with python3Packages once possible, as to unbeak splicing.
113 # Cf. https://github.com/NixOS/nixpkgs/pull/394838#issuecomment-3319287038
114 cudaPackages.writeGpuTestPython.override { python3Packages = python.pkgs; }
115 {
116 libraries = ps: [
117 ps.unsloth
118 ps.unsloth-zoo
119 ];
120 # In-derivation test would require fetching the model from hugging-face which will not be trivial.
121 gpuCheckArgs.meta.broken = true;
122 }
123 # Triton JIT requires a C compiler at runtime and imports files from the test directory.
124 ''
125 import os, sys, runpy
126
127 os.environ["CC"] = "${lib.getExe' gcc "cc"}";
128 os.environ["CXX"] = "${lib.getExe' gcc "cxx"}";
129
130 sys.path.insert(0, "${testSrc}")
131
132 # Execute the test file from the Nix store at runtime (no eval-time IFD).
133 runpy.run_path(
134 "${testSrc}/tests/qlora/test_unsloth_qlora_train_and_merge.py",
135 run_name="__main__"
136 )
137 '';
138 };
139
140 meta = {
141 description = "Finetune Llama 3.3, DeepSeek-R1 & Reasoning LLMs 2x faster with 70% less memory";
142 homepage = "https://github.com/unslothai/unsloth";
143 changelog = "https://github.com/unslothai/unsloth/releases/tag/${version}";
144 license = lib.licenses.asl20;
145 maintainers = with lib.maintainers; [ hoh ];
146 };
147}