create localWebApps option

This commit is contained in:
atagen 2024-05-02 10:52:19 +10:00
parent a9e78be414
commit 9bc61077d5
5 changed files with 178 additions and 32 deletions

View file

@ -8,8 +8,7 @@
}: {
imports = [
./home.nix
./programs/stable-diffusion.nix
./programs/ollama.nix
./util/local-webapp.nix
];
home = {
@ -19,4 +18,32 @@
nicotine-plus
];
};
localWebApps = {
stable-diffusion = {
name = "ComfyUI (Stable Diffusion)";
icon = ./icons/comfyui.png;
id = 5;
port = 7860;
service = {
WorkingDirectory = "${config.home.homeDirectory}/code/etc/stable-diffusion-webui-docker";
ExecStart = "${lib.getExe pkgs.docker} compose --profile comfy up --build";
};
};
openwebui = rec {
name = "OpenWebUI (Ollama)";
icon = ./icons/openwebui.png;
id = 6;
port = 3021;
service = let
docker = lib.getExe pkgs.docker;
in {
Type = "exec";
ExecStartPre = "-${docker} create -e PORT=${builtins.toString port} --network host --gpus all -v ollama:/root/.ollama -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:ollama";
ExecStart = "${docker} start open-webui";
ExecStop = "${docker} stop open-webui";
};
};
};
}

View file

@ -34,34 +34,34 @@
type = "Application";
icon = ../icons/openwebui.png;
exec = "${let
notify-send = "${lib.getExe' pkgs.libnotify "notify-send"}";
notify-send = "${lib.getExe' pkgs.libnotify "notify-send"} -a \"Ollama OpenWebUI\"";
systemctl = "${lib.getExe' pkgs.systemd "systemctl"}";
dex = "${lib.getExe pkgs.dex}";
curl = "${lib.getExe pkgs.curl}";
in
pkgs.writeShellScript "ollama"
''
${notify-send} "Ollama OpenWebUI" "Launching backend.."
${notify-send} "Launching backend.." "Please be patient."
${systemctl} --user start open-webui
attempts=0
success=false
while [[ $attempts -lt $((20*9)) ]]; do
if [[ $(${curl} http://127.0.0.1:3021) ]]; then
${notify-send} "Ollama OpenWebUI" "Launching client.."
${notify-send} "Backend up." "Launching client.."
success=true
break
else
attempts=$((attempts+1))
if [[ $(($attempts % 20)) -eq 0 ]]; then
${notify-send} "Ollama OpenWebUI" "Still launching backend.. ($((attempts/2))s)"
${notify-send} "Launching backend.." "Still launching.. ($((attempts/2))s)"
fi
fi
sleep 0.5
done
if [[ ! $success ]]; then
${notify-send} "Ollama OpenWebUI" "Failed to launch backend!"
${notify-send} "Failure" "Failed to launch backend!"
${systemctl} --user kill open-webui
exit 1
fi
@ -69,7 +69,7 @@
sleep 0.5 # give it a little time
${dex} -w ~/.nix-profile/share/applications/OpenWebUI.desktop
${notify-send} "Ollama OpenWebUI" "Shutting down."
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop open-webui
exit 0
''}";

View file

@ -34,34 +34,34 @@
type = "Application";
icon = ../icons/comfyui.png;
exec = "${let
notify-send = "${lib.getExe' pkgs.libnotify "notify-send"}";
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} "Stable Diffusion" "Launching backend.."
${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} "Stable Diffusion" "Launching client.."
${notify-send} "Backend up." "Launching client.."
success=true
break
else
attempts=$((attempts+1))
if [[ $(($attempts % 20)) -eq 0 ]]; then
${notify-send} "Stable Diffusion" "Still launching backend.. ($((attempts/2))s)"
${notify-send} "Launching backend.." "Still launching.. ($((attempts/2))s)"
fi
fi
sleep 0.5
done
if [[ ! $success ]]; then
${notify-send} "Stable Diffusion" "Failed to launch backend!"
${notify-send} "Failure" "Failed to launch backend!"
${systemctl} --user kill stable-diffusion
exit 1
fi
@ -69,7 +69,7 @@
sleep 0.5 # give it a little time
${dex} -w ~/.nix-profile/share/applications/StableDiffusion.desktop
${notify-send} "Stable Diffusion" "Shutting down."
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop stable-diffusion
exit 0
''}";

119
home/util/local-webapp.nix Normal file
View file

@ -0,0 +1,119 @@
{
config,
pkgs,
lib,
...
}: let
inherit (lib) mkOption;
inherit (lib.attrsets) mapAttrs mapAttrs' nameValuePair;
make-firefox = cfg:
mapAttrs' (
name: cfg:
nameValuePair "${name}-client"
{
inherit (cfg) name id;
url = "http://127.0.0.1:${builtins.toString cfg.port}";
extraSettings = config.programs.firefox.profiles.default.settings;
hidden = true;
}
)
cfg;
make-systemd = cfg:
mapAttrs (
name: cfg: {
Unit.Description = "${cfg.name} Backend";
Service = cfg.service;
}
)
cfg;
make-xdg = cfg:
mapAttrs (
name: cfg: {
name = cfg.name;
type = "Application";
icon = cfg.icon;
exec = "${let
notify-send = "${lib.getExe' pkgs.libnotify "notify-send"} -a \"${cfg.name}\"";
systemctl = "${lib.getExe' pkgs.systemd "systemctl"}";
dex = "${lib.getExe pkgs.dex}";
curl = "${lib.getExe pkgs.curl}";
in
pkgs.writeShellScript "${name}"
''
${notify-send} "Launching backend.." "Please be patient."
${systemctl} --user start ${name}
attempts=0
success=false
while [[ $attempts -lt $((20*9)) ]]; do
if [[ $(${curl} http://127.0.0.1:${builtins.toString cfg.port}) ]]; 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 ${name}
exit 1
fi
${dex} -w ~/.nix-profile/share/applications/${name}-client.desktop
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop ${name}
exit 0
''}";
}
)
cfg;
in {
options.localWebApps = mkOption {
default = {};
type = with lib.types;
attrsOf (submodule {
options = {
name = mkOption {
type = str;
description = "Display name of the webapp.";
};
icon = mkOption {
type = either str path;
description = "Path to a file to use for application icon.";
default = "";
};
id = mkOption {
type = int;
description = "Firefox profile ID for the webapp's client";
};
port = mkOption {
type = int;
description = "Local port the webapp should host on.";
};
service = mkOption {
type = attrsOf string;
description = "The service settings for systemd user service. Requires at least ExecStart.";
};
};
});
};
config = {
programs.firefox.webapps = make-firefox config.localWebApps;
systemd.user.services = make-systemd config.localWebApps;
xdg.desktopEntries = make-xdg config.localWebApps;
};
}