# Stable Diffusion Using Nix Flakes

**URL:** https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610
**Category:** Announcements
**Created:** [September 11, 2022, 6:23pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610 "2022-09-11T18:23:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![collinarnett](https://discourse.nixos.org/user_avatar/discourse.nixos.org/collinarnett/32/6626_2.png) [@collinarnett](https://discourse.nixos.org/u/collinarnett)
#### Post date: [September 11, 2022, 6:24pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/1 "2022-09-11T18:24:00Z")

</div>

Hello 👋

## Introduction

I made this repository which uses Flakes to quickly get up and running with Stable Diffusion in a Jupyter Notebook.

> **[GitHub - collinarnett/stable-diffusion-nix: JupyterLab Flake for Stable Diffusion](https://github.com/collinarnett/stable-diffusion-nix)**
>
> JupyterLab Flake for Stable Diffusion

## Setup

The only requirements before starting are:

- A [HuggingFace](https://huggingface.co/) account + [personal access token](https://huggingface.co/docs/hub/security-tokens).
- [Accept the License Agreement on the Model Card](https://huggingface.co/CompVis/stable-diffusion-v1-4).
- Nvidia GPU with at least 10GB of VRAM

## Running

Users here will be familiar with the setup process for flakes but after that running is as simple as:

```auto
nix run --impure github:collinarnett/stable-diffusion-nix#jupyterLab

```

place your user token in the `HF_TOKEN` variable at the top of the page and you should be able to immediately generate images.

## Info

This is built using the following awesome projects:

- [nixGL](https://github.com/guibou/nixGL) - A wrapper tool for nix OpenGL application.
- [jupyterWith](https://github.com/tweag/jupyterWith) - Declarative and reproducible Jupyter environments
- [flake-utils](https://github.com/numtide/flake-utils) - Pure Nix flake utility functions

* * *

**Please let me know if there are any problems since I haven’t been able to test this extensively**

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [September 11, 2022, 6:54pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/2 "2022-09-11T18:54:10Z")

</div>

Cool!

Would it be a lot of efforts to provide a flake for [GitHub - basujindal/stable-diffusion: Optimized Stable Diffusion modified to run on lower GPU VRAM](https://github.com/basujindal/stable-diffusion) ? This allows to use stable-diffusion with 2.4 GB of VRAM instead of 10, making it a lot more accessible.

---

<div class="post-metadata">

### Author: ![collinarnett](https://discourse.nixos.org/user_avatar/discourse.nixos.org/collinarnett/32/6626_2.png) [@collinarnett](https://discourse.nixos.org/u/collinarnett)
#### Post date: [September 11, 2022, 7:06pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/3 "2022-09-11T19:06:48Z")

</div>

That repo is neat but yes it does have quite a few more requirements than the small amount of work I’ve put in above to get `diffusers` packaged. You’re right though, it would make the flake much more accessible if I could package the 2.4GB version.

The entire goal of this small repo is to showcase how simple it is to share reproducible environments with Nix in a fun and interesting way so I will do my best in the coming week to package the repo you linked.

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [September 11, 2022, 7:09pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/4 "2022-09-11T19:09:37Z")

</div>

That will be the opportunity for me to try jupyterLab, I hear a lot about it but I still don’t understand how it works / what to do with it 😅

---

<div class="post-metadata">

### Author: ![collinarnett](https://discourse.nixos.org/user_avatar/discourse.nixos.org/collinarnett/32/6626_2.png) [@collinarnett](https://discourse.nixos.org/u/collinarnett)
#### Post date: [September 13, 2022, 4:00pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/5 "2022-09-13T16:00:52Z")

</div>

Good news! diffusers integrated that change into their library. I’ve gone ahead and configured the change by default so the repo only requires 3.2GB of VRAM now 😄

Go ahead and run

```auto
nix run --impure github:collinarnett/stable-diffusion-nix#jupyterLab

```

to get the latest version!

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [September 13, 2022, 5:12pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/6 "2022-09-13T17:12:41Z")

</div>

Super cool!!! Thanks 🙏🏼

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [September 17, 2022, 9:58am UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/7 "2022-09-17T09:58:42Z")

</div>

Is it possible to use a local weights file downloaded from HuggingFace instead of the API? What is doing the API exactly? 🤔

---

<div class="post-metadata">

### Author: ![brnix](https://discourse.nixos.org/user_avatar/discourse.nixos.org/brnix/32/32906_2.png) [@brnix](https://discourse.nixos.org/u/brnix)
#### Post date: [September 16, 2023, 5:17pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/8 "2023-09-16T17:17:38Z")

</div>

Is it possible to use an AMD card with this? I assume it is nvidia only. (Per requirements)

---

<div class="post-metadata">

### Author: ![collinarnett](https://discourse.nixos.org/user_avatar/discourse.nixos.org/collinarnett/32/6626_2.png) [@collinarnett](https://discourse.nixos.org/u/collinarnett)
#### Post date: [September 21, 2023, 1:11pm UTC](https://discourse.nixos.org/t/stable-diffusion-using-nix-flakes/21610/9 "2023-09-21T13:11:09Z")

</div>

You probably can use rocm as long as you compile rocm support with torch. I don’t have an AMD card to test so I can’t give you a clear answer.

I’m working on trying to make this repo better by updating diffusers to the latest version. I have a working repo with an up to date diffusers locally but unfortunately it requires compiling quite a few big packages. I am waiting for this [issue](https://github.com/numtide/nixpkgs-unfree/issues/24) to be addressed on nixpkgs-unfree to prevent people from having to deal with compiling these large packages. This way you can just change the input to the diffusers pytorch package with one that supports rocm and be good to go.

I will do my best to publish an update to the repo that will allow you to use rocm easily this weekend.

I am also considering merging my changes into [nixifiedai](https://github.com/nixified-ai/flake) but I will work on the above first.
