linted
This commit is contained in:
atagen 2025-02-02 16:57:31 +11:00
parent 4e99a0e323
commit 7485de646a
95 changed files with 2743 additions and 2282 deletions

View file

@ -3,9 +3,9 @@
pkgs,
lib,
...
}: let
inherit
(lib)
}:
let
inherit (lib)
mkOption
mkForce
getExe
@ -20,106 +20,104 @@
replaceStrings
concatMapStringsSep
;
partOf = cfg: "${replaceStrings [" "] ["-"] (toLower cfg.name)}.target";
partOf = cfg: "${replaceStrings [ " " ] [ "-" ] (toLower cfg.name)}.target";
# make a firefox webapp + hidden .desktop entry for the client app
make-firefox = cfg:
make-firefox =
cfg:
mapAttrs' (
name: cfg:
nameValuePair "${name}-client"
{
inherit (cfg) name;
url = "http://127.0.0.1:${builtins.toString cfg.port}";
extraSettings = config.programs.firefox.profiles.default.settings;
hidden = true;
}
)
cfg;
# make a systemd service for running the frontend
make-systemd-service = cfg:
mapAttrs' (
name: cfg:
if
(cfg.service
!= null)
then
nameValuePair "${cfg.name}-frontend" {
Unit = {
Description = "${cfg.name} Frontend";
WantedBy = mkForce [];
};
Service = cfg.service;
}
else nameValuePair "" {}
)
cfg;
# modify systemd units to be PartOf this target
modify-systemd-services = cfg:
listToAttrs (flatten (mapAttrsToList (
name: cfg: (map (
req: {
name = "${req}";
value = {
Unit = {
PartOf = partOf cfg;
};
};
}
)
cfg.requires.services)
)
cfg));
modify-quadlets = cfg:
listToAttrs (flatten (mapAttrsToList (
name: cfg: (map (
req: {
name = "${req}";
value = {
extraConfig.Unit.PartOf = partOf cfg;
};
}
)
cfg.requires.containers)
)
cfg));
# make a systemd target to collate dependencies
make-systemd-target = cfg:
mapAttrs (
name: cfg: {
Unit = {
Description = "${cfg.name} Target";
WantedBy = mkForce [];
Requires =
(map (req: req + ".service") cfg.requires.services)
++ (map (req: "podman-" + req + ".service") cfg.requires.containers);
};
nameValuePair "${name}-client" {
inherit (cfg) name;
url = "http://127.0.0.1:${builtins.toString cfg.port}";
extraSettings = config.programs.firefox.profiles.default.settings;
hidden = true;
}
)
cfg;
) cfg;
# make a systemd service for running the frontend
make-systemd-service =
cfg:
mapAttrs' (
name: cfg:
if (cfg.service != null) then
nameValuePair "${cfg.name}-frontend" {
Unit = {
Description = "${cfg.name} Frontend";
WantedBy = mkForce [ ];
};
Service = cfg.service;
}
else
nameValuePair "" { }
) cfg;
# modify systemd units to be PartOf this target
modify-systemd-services =
cfg:
listToAttrs (
flatten (
mapAttrsToList (
name: cfg:
(map (req: {
name = "${req}";
value = {
Unit = {
PartOf = partOf cfg;
};
};
}) cfg.requires.services)
) cfg
)
);
modify-quadlets =
cfg:
listToAttrs (
flatten (
mapAttrsToList (
name: cfg:
(map (req: {
name = "${req}";
value = {
extraConfig.Unit.PartOf = partOf cfg;
};
}) cfg.requires.containers)
) cfg
)
);
# make a systemd target to collate dependencies
make-systemd-target =
cfg:
mapAttrs (name: cfg: {
Unit = {
Description = "${cfg.name} Target";
WantedBy = mkForce [ ];
Requires =
(map (req: req + ".service") cfg.requires.services)
++ (map (req: "podman-" + req + ".service") cfg.requires.containers);
};
}) cfg;
# make desktop shortcuts and a script which will handle starting everything
make-xdg = cfg:
mapAttrs (
name: cfg: {
inherit (cfg) name icon genericName;
type = "Application";
exec = "${let
make-xdg =
cfg:
mapAttrs (name: cfg: {
inherit (cfg) name icon genericName;
type = "Application";
exec = "${
let
notify-send = "${getExe' pkgs.libnotify "notify-send"} -a \"${cfg.name}\"";
systemctl = "${getExe' pkgs.systemd "systemctl"}";
dex = "${getExe pkgs.dex}";
podman = "${getExe pkgs.podman}";
makeContainerCheck = container: ''[ "$(${podman} inspect -f {{.State.Health.Status}} ${container})" == "healthy" ]'';
makeContainerCheck =
container: ''[ "$(${podman} inspect -f {{.State.Health.Status}} ${container})" == "healthy" ]'';
# makeContainerCheck = container: ''
# [ ${podman} inspect -f {{.State.Status}} ${container})" != "running" ]
# '';
containerChecks =
if (cfg.requires.containers != [])
then
if (cfg.requires.containers != [ ]) then
''
container_checks() {
if ''
+ (concatMapStringsSep " && "
(container: makeContainerCheck container)
cfg.requires.containers)
+ (concatMapStringsSep " && " makeContainerCheck cfg.requires.containers)
+ ''
; then
return 0
@ -128,54 +126,55 @@
fi
}
''
else ''
container_checks() {
return 0
}
'';
else
''
container_checks() {
return 0
}
'';
in
pkgs.writeShellScript "${name}"
''
set -euo pipefail
pkgs.writeShellScript "${name}" ''
set -euo pipefail
exit_error() {
${notify-send} -w "Failure" $1
exit 1
}
exit_error() {
${notify-send} -w "Failure" $1
exit 1
}
${containerChecks}
${containerChecks}
${notify-send} "Launching ${name} backend.." "Please be patient."
${systemctl} --user start ${name}.target || exit_error "Failed to launch!"
${notify-send} "Launching ${name} backend.." "Please be patient."
${systemctl} --user start ${name}.target || exit_error "Failed to launch!"
checks=0
until container_checks; do
sleep 2
checks=$((checks+1))
if [ $((checks%10)) -eq 0 ]; then
${notify-send} "Waiting for backend."
fi
if [ $checks -ge 60 ]; then
${systemctl} --no-block --user stop ${name}.target
exit_error "Failed to launch!"
fi
done
checks=0
until container_checks; do
sleep 2
checks=$((checks+1))
if [ $((checks%10)) -eq 0 ]; then
${notify-send} "Waiting for backend."
fi
if [ $checks -ge 60 ]; then
${systemctl} --no-block --user stop ${name}.target
exit_error "Failed to launch!"
fi
done
${notify-send} "Launching ${name}.."
${dex} -w ~/.nix-profile/share/applications/${name}-client.desktop
${notify-send} "Launching ${name}.."
${dex} -w ~/.nix-profile/share/applications/${name}-client.desktop
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop ${name}.target
exit 0
''}";
}
)
cfg;
${notify-send} "Goodbye" "Shutting down."
${systemctl} --user stop ${name}.target
exit 0
''
}";
}) cfg;
cfg = config.localWebApps;
in {
in
{
options.localWebApps = mkOption {
default = {};
type = with lib.types;
default = { };
type =
with lib.types;
attrsOf (submodule {
options = {
name = mkOption {
@ -200,11 +199,11 @@ in {
options = {
containers = mkOption {
type = listOf str;
default = [];
default = [ ];
};
services = mkOption {
type = listOf str;
default = [];
default = [ ];
};
};
});