Files
ForgeOS/overlay/rootfs/usr/bin/forgeos-app
T

175 lines
3.6 KiB
Bash
Raw Normal View History

2026-06-17 21:01:31 -04:00
#!/bin/sh
set -eu
PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH
usage() {
cat <<'EOF'
Usage:
forgeos-app status
forgeos-app install NAME|FLAKE...
forgeos-app remove PACKAGE...
forgeos-app list
forgeos-app search TERMS...
forgeos-app upgrade [PACKAGE...]
forgeos-app rollback
forgeos-app shell NAME|FLAKE...
forgeos-app gc
Short names are expanded to nixpkgs#NAME for install and shell.
EOF
}
as_installable() {
case "$1" in
*#*|/*|git+*|http://*|https://*)
printf '%s\n' "$1"
;;
*)
printf 'nixpkgs#%s\n' "$1"
;;
esac
}
show_mount() {
awk -v target="$1" '
$2 == target {
printf "%-12s <- %s (%s, %s)\n", target, $1, $3, $4
found = 1
}
END {
if (!found) {
printf "%-12s <- not mounted\n", target
}
}
' /proc/mounts
}
2026-06-17 21:21:38 -04:00
find_nix() {
if command -v nix >/dev/null 2>&1; then
command -v nix
return 0
fi
if [ -x /nix/var/nix/profiles/default/bin/nix ]; then
printf '%s\n' /nix/var/nix/profiles/default/bin/nix
return 0
fi
for candidate in /nix/store/*-nix-*/bin/nix; do
[ -x "$candidate" ] || continue
printf '%s\n' "$candidate"
return 0
done
return 1
}
diagnose_missing_nix() {
echo "nix is unavailable; is the ForgeOS app layer mounted?" >&2
show_mount /forge >&2
show_mount /nix >&2
echo "check: lsblk -f" >&2
echo "check: systemctl status forge.mount forgeos-nix-bootstrap.service nix-daemon.service nix-daemon.socket" >&2
echo "repair as root: mount LABEL=FORGE_APPS /forge && mount -a && systemctl restart forgeos-nix-bootstrap.service nix-daemon.socket nix-daemon.service" >&2
}
need_nix() {
if NIX_BIN=$(find_nix); then
export NIX_BIN
export NIX_REMOTE=${NIX_REMOTE:-daemon}
return 0
fi
diagnose_missing_nix
exit 1
}
run_nix() {
need_nix
"$NIX_BIN" "$@"
}
2026-06-17 21:01:31 -04:00
cmd=${1:-}
if [ -z "$cmd" ]; then
usage
exit 2
fi
shift || true
case "$cmd" in
status)
show_mount /
show_mount /forge
show_mount /nix
show_mount /home
show_mount /root
show_mount /var
show_mount /opt
show_mount /usr/local
df -h /forge /nix /home 2>/dev/null || true
;;
install)
[ "$#" -gt 0 ] || {
usage >&2
exit 2
}
for item in "$@"; do
2026-06-17 21:21:38 -04:00
run_nix profile install "$(as_installable "$item")"
2026-06-17 21:01:31 -04:00
done
;;
remove)
[ "$#" -gt 0 ] || {
usage >&2
exit 2
}
2026-06-17 21:21:38 -04:00
run_nix profile remove "$@"
2026-06-17 21:01:31 -04:00
;;
list)
2026-06-17 21:21:38 -04:00
run_nix profile list
2026-06-17 21:01:31 -04:00
;;
search)
[ "$#" -gt 0 ] || {
usage >&2
exit 2
}
2026-06-17 21:21:38 -04:00
run_nix search nixpkgs "$@"
2026-06-17 21:01:31 -04:00
;;
upgrade)
if [ "$#" -eq 0 ]; then
2026-06-17 21:21:38 -04:00
run_nix profile upgrade '.*'
2026-06-17 21:01:31 -04:00
else
2026-06-17 21:21:38 -04:00
run_nix profile upgrade "$@"
2026-06-17 21:01:31 -04:00
fi
;;
rollback)
2026-06-17 21:21:38 -04:00
run_nix profile rollback
2026-06-17 21:01:31 -04:00
;;
shell)
[ "$#" -gt 0 ] || {
usage >&2
exit 2
}
need_nix
set -- "$@"
expanded=
for item in "$@"; do
expanded="${expanded}${expanded:+ }$(as_installable "$item")"
done
2026-06-17 21:21:38 -04:00
need_nix
2026-06-17 21:01:31 -04:00
# shellcheck disable=SC2086
2026-06-17 21:21:38 -04:00
exec "$NIX_BIN" shell $expanded
2026-06-17 21:01:31 -04:00
;;
gc)
2026-06-17 21:21:38 -04:00
run_nix store gc
2026-06-17 21:01:31 -04:00
;;
help|--help|-h)
usage
;;
*)
usage >&2
exit 2
;;
esac