From 9bc61077d57421e140885347dafbb44dd51b083d Mon Sep 17 00:00:00 2001 From: atagen Date: Thu, 2 May 2024 10:52:19 +1000 Subject: [PATCH] create localWebApps option --- flake.lock | 36 ++++----- home/bolt.nix | 31 +++++++- home/programs/ollama.nix | 12 +-- home/programs/stable-diffusion.nix | 12 +-- home/util/local-webapp.nix | 119 +++++++++++++++++++++++++++++ 5 files changed, 178 insertions(+), 32 deletions(-) create mode 100644 home/util/local-webapp.nix diff --git a/flake.lock b/flake.lock index d986388..c97d75f 100644 --- a/flake.lock +++ b/flake.lock @@ -237,11 +237,11 @@ "rust-overlay": "rust-overlay" }, "locked": { - "lastModified": 1714317710, - "narHash": "sha256-djwrEnMHwGYMSKElkfD2TLKwHYOJmhnCa6gTjvDPc6w=", + "lastModified": 1714450407, + "narHash": "sha256-OYLQkzwEr2ON83j9ZcR2JY8aSY1XT9SlN0POCn4gxAM=", "owner": "helix-editor", "repo": "helix", - "rev": "e2594b64c0daf15bacdb5396d439f206fcbe11e6", + "rev": "752ed8eb15b7366ef0c9e835b261189141e38630", "type": "github" }, "original": { @@ -257,11 +257,11 @@ ] }, "locked": { - "lastModified": 1714377222, - "narHash": "sha256-UsDsjWCKlWn8vbXi8Zza9Hkq3xyk8fpvFNo2VM5S74E=", + "lastModified": 1714515075, + "narHash": "sha256-azMK7aWH0eUc3IqU4Fg5rwZdB9WZBvimOGG3piqvtsY=", "owner": "nix-community", "repo": "home-manager", - "rev": "2af7c78b7bb9cf18406a193eba13ef9f99388f49", + "rev": "6d3b6dc9222c12b951169becdf4b0592ee9576ef", "type": "github" }, "original": { @@ -372,11 +372,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1714353612, - "narHash": "sha256-YTnNNPc4c4n8XxWP9FdddzEY7/4uatIZ8vliNKkFv0o=", + "lastModified": 1714536133, + "narHash": "sha256-zVRNhlvRaWxdwO1w77uZwolrUOCEA71OUtzlNwM+DEg=", "owner": "nix-community", "repo": "nixd", - "rev": "dad49ea93797833eed2dee288c15c8130953406b", + "rev": "891b5487b7dba00c85de0ef79bbc29ffeb936005", "type": "github" }, "original": { @@ -393,11 +393,11 @@ ] }, "locked": { - "lastModified": 1714354228, - "narHash": "sha256-os1TJOIoAl24tlsE1iU3FpoeM7+BHNU+DDrNlcbRUlc=", + "lastModified": 1714582465, + "narHash": "sha256-qeQpjgbqJ7Fe8zzt2pBrzncGxr52QbFRbkaRh6tA2c8=", "owner": "lilyinstarlight", "repo": "nixos-cosmic", - "rev": "d0e668fdb5e5e1912ea5d7a40ac0570d1a11ad85", + "rev": "8f5f08a8718e990814fb371b9637f4e66318edae", "type": "github" }, "original": { @@ -521,11 +521,11 @@ }, "nur": { "locked": { - "lastModified": 1714387460, - "narHash": "sha256-p2WP/Mr7hh9QVXCbtlqf9bj62JOIqaP+C3/09DpCoaY=", + "lastModified": 1714596740, + "narHash": "sha256-V3ZR38xu3JSUrg04wMTp0fzDSP+ogNJOOU5ckreCzLQ=", "owner": "nix-community", "repo": "NUR", - "rev": "a16bed921e49adac4f59bbaa4d2a28c69dafdf80", + "rev": "c8456769a0904ac761020a76bf0bb72a92c27c7c", "type": "github" }, "original": { @@ -612,11 +612,11 @@ ] }, "locked": { - "lastModified": 1714356894, - "narHash": "sha256-W6Mss7AG6bnFT1BqRApHXvLXBrFOu7V0+EUe9iML30s=", + "lastModified": 1714529851, + "narHash": "sha256-YMKJW880f7LHXVRzu93xa6Ek+QLECIu0IRQbXbzZe38=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "d9b44509b4064f0a3fc9c7c92a603861f52fbedc", + "rev": "9ca720fdcf7865385ae3b93ecdf65f1a64cb475e", "type": "github" }, "original": { diff --git a/home/bolt.nix b/home/bolt.nix index cc1b148..57b6ae0 100644 --- a/home/bolt.nix +++ b/home/bolt.nix @@ -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"; + }; + }; + }; } diff --git a/home/programs/ollama.nix b/home/programs/ollama.nix index 63866da..dbe12a3 100644 --- a/home/programs/ollama.nix +++ b/home/programs/ollama.nix @@ -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 ''}"; diff --git a/home/programs/stable-diffusion.nix b/home/programs/stable-diffusion.nix index c6dbc97..70c561e 100644 --- a/home/programs/stable-diffusion.nix +++ b/home/programs/stable-diffusion.nix @@ -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 ''}"; diff --git a/home/util/local-webapp.nix b/home/util/local-webapp.nix new file mode 100644 index 0000000..cb597ad --- /dev/null +++ b/home/util/local-webapp.nix @@ -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; + }; +}