nix/home/programs/stable-diffusion.nix
2024-05-02 10:52:19 +10:00

86 lines
2.7 KiB
Nix

{
config,
pkgs,
lib,
...
}: {
# TODO: figure out if there's a meaningful way to add this to the nix store
# if nothing's reproducible should we even bother
# https://github.com/AbdBarho/stable-diffusion-webui-docker/
config.programs.firefox.webapps = {
"StableDiffusion" = {
url = "http://127.0.0.1:7860";
id = 5;
extraSettings = config.programs.firefox.profiles.default.settings;
name = "Stable Diffusion Client";
hidden = true;
};
};
config.systemd.user.services = {
stable-diffusion = {
Unit.Description = "Stable Diffusion Backend";
Service = {
WorkingDirectory = "${config.home.homeDirectory}/code/etc/stable-diffusion-webui-docker";
ExecStart = "${lib.getExe pkgs.docker} compose --profile comfy up --build";
};
};
};
config.xdg.desktopEntries = {
stable-diffusion = {
name = "Stable Diffusion";
type = "Application";
icon = ../icons/comfyui.png;
exec = "${let
notify-send = "${lib.getExe' pkgs.libnotify "notify-send"} -a Stable Diffusion";
systemctl = "${lib.getExe' pkgs.systemd "systemctl"}";
dex = "${lib.getExe pkgs.dex}";
curl = "${lib.getExe pkgs.curl}";
in
pkgs.writeShellScript "stable-diffusion"
''
${notify-send} "Launching backend.." "Please be patient."
${systemctl} --user start stable-diffusion
attempts=0
success=false
while [[ $attempts -lt $((20*9)) ]]; do
if [[ $(${curl} http://127.0.0.1:7860) ]]; then
${notify-send} "Backend up." "Launching client.."
success=true
break
else
attempts=$((attempts+1))
if [[ $(($attempts % 20)) -eq 0 ]]; then
${notify-send} "Launching backend.." "Still launching.. ($((attempts/2))s)"
fi
fi
sleep 0.5
done
if [[ ! $success ]]; then
${notify-send} "Failure" "Failed to launch backend!"
${systemctl} --user kill stable-diffusion
exit 1
fi
sleep 0.5 # give it a little time
${dex} -w ~/.nix-profile/share/applications/StableDiffusion.desktop
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop stable-diffusion
exit 0
''}";
};
# stable-diffusion-dl = {
# name = "Stable Diffusion Setup";
# type = "Application";
# settings = {
# Path = "${config.home.homeDirectory}/code/etc/stable-diffusion-webui-docker";
# };
# exec = "docker compose --profile download up --build";
# };
};
}