Skip to content

Commit

Permalink
Generate a set of automatic tools
Browse files Browse the repository at this point in the history
* pull tools that can describe themselves to the AI
  • Loading branch information
slimslenderslacks committed Aug 29, 2024
1 parent 9c3a74d commit bdd7dfb
Show file tree
Hide file tree
Showing 15 changed files with 574 additions and 101 deletions.
33 changes: 33 additions & 0 deletions functions/hub/Dockerfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# syntax = docker/dockerfile:1.4
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--extra-trusted-substituters "https://cache.iog.io" \
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/entrypoint"]
CMD ["--help"]
61 changes: 61 additions & 0 deletions functions/hub/flake.lock.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1718447546,
"narHash": "sha256-JHuXsrC9pr4kA4n7LuuPfWFJUVlDBVJ1TXDVpHEuUgM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "842253bf992c3a7157b67600c2857193f126563a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
44 changes: 44 additions & 0 deletions functions/hub/flake.nix.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
description = "{{tool}}";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ...}@inputs:

flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};

in rec
{
packages = rec {

# this derivation just contains the init.clj script
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ./.;
installPhase = ''
cp init.clj $out
'';
};

run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
export PATH=${pkgs.lib.makeBinPath [pkgs.{{tool}} pkgs.man]}
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
/usr/local/bin/bb ${scripts} "$@"
'';

default = pkgs.buildEnv {
name = "{{tool}}";
paths = [ run-entrypoint ];
};
};
});
}

42 changes: 42 additions & 0 deletions functions/hub/init.clj.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns init
(:require
[babashka.process]
[cheshire.core]))

(defn output [args]
(try
(->
(apply babashka.process/process
{:out :string}
args)
(deref)
(babashka.process/check))
(catch Throwable _ nil)))

(defn man [t]
(output ["man" t]))
(defn help [t]
(output [t "--help"]))
(defn h [t]
(output [t "-h"]))

(try
(let [[json-string & extra-args] *command-line-args*
{:keys [args]} (cheshire.core/parse-string json-string true)]
(println
(-> (if (= "man" (first extra-args))
(let [t "{{tool}}"] (or (man t) (help t) (h t)))
(-> (babashka.process/process
{:out :string}
(format "{{tool}} %s" args))
(deref)
(babashka.process/check)))
(deref)
(babashka.process/check)
:out)))
(catch Throwable t
(binding [*out* *err*]
(println (str "Error: " (.getMessage t)))
(System/exit 1))))


Loading

0 comments on commit bdd7dfb

Please sign in to comment.