aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-08-11 10:05:02 -0400
committerXe Iaso <me@xeiaso.net>2023-08-11 10:05:02 -0400
commitc822591c5a46ad9e8f13d14ac96d2e9d26e7c828 (patch)
tree70d5a97a396b86706c7593fe435b3d9586e6496b /cmd
parent2b4d36a61e4ccb9815b3b42acecc1fec308a8ed0 (diff)
downloadx-c822591c5a46ad9e8f13d14ac96d2e9d26e7c828.tar.xz
x-c822591c5a46ad9e8f13d14ac96d2e9d26e7c828.zip
cmd/yeet: nix tagged templates
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/robocadey2/yeetfile.js4
-rw-r--r--cmd/within.website/yeetfile.js26
-rw-r--r--cmd/yeet/main.go58
-rw-r--r--cmd/yeet/yeetfile.js14
4 files changed, 97 insertions, 5 deletions
diff --git a/cmd/robocadey2/yeetfile.js b/cmd/robocadey2/yeetfile.js
new file mode 100644
index 0000000..03c5849
--- /dev/null
+++ b/cmd/robocadey2/yeetfile.js
@@ -0,0 +1,4 @@
+nix.build(".#docker.robocadey2");
+docker.load();
+docker.push("registry.fly.io/xe-robocadey2:latest");
+fly.deploy();
diff --git a/cmd/within.website/yeetfile.js b/cmd/within.website/yeetfile.js
index 394ded8..676443e 100644
--- a/cmd/within.website/yeetfile.js
+++ b/cmd/within.website/yeetfile.js
@@ -5,4 +5,28 @@ go.build();
slug.build("within.website", {
"config.ts": "config.ts"
});
-log.info(slug.push("within.website")); \ No newline at end of file
+
+const url = slug.push("within.website");
+const hash = nix.hashURL(url);
+
+const expr = nix.expr`{ stdenv }:
+
+stdenv.mkDerivation {
+ name = "within.website";
+ src = builtins.fetchurl {
+ url = ${url};
+ sha256 = ${hash};
+ };
+
+ phases = "installPhase";
+
+ installPhase = ''
+ tar xf $src
+ mkdir -p $out/bin
+ cp web $out/bin/withinwebsite
+ cp config.ts $out/config.ts
+ '';
+}
+`;
+
+file.write("/home/cadey/code/nixos-configs/pkgs/x/withinwebsite.nix", expr);
diff --git a/cmd/yeet/main.go b/cmd/yeet/main.go
index 3889261..ed76fe7 100644
--- a/cmd/yeet/main.go
+++ b/cmd/yeet/main.go
@@ -2,12 +2,15 @@ package main
import (
"context"
+ "encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
+ "runtime/debug"
+ "strconv"
"github.com/dop251/goja"
"golang.org/x/exp/slog"
@@ -61,6 +64,9 @@ func gittag() string {
}
func dockerload(fname string) {
+ if fname == "" {
+ fname = "./result"
+ }
yeet.DockerLoadResult(context.Background(), fname)
}
@@ -106,6 +112,44 @@ func slugpush(bin string) string {
return pubURL
}
+func buildNixExpr(literals []string, exprs ...any) string {
+ /*
+ function nix(strings, ...expressions) {
+ let result = "";
+ expressions.forEach((value, i) => {
+ let formattedValue = `(builtins.fromJSON ${JSON.stringify(JSON.stringify(value))});`;
+ result += `${strings[i]} ${formattedValue}`;
+ });
+
+ result += strings[strings.length - 1]
+
+ return result;
+ }
+ */
+
+ result := ""
+ for i, value := range exprs {
+ formattedValue, _ := json.Marshal(value)
+ formattedValue = []byte(fmt.Sprintf(`(builtins.fromJSON %s)`, strconv.Quote(string(formattedValue))))
+ result += literals[i] + string(formattedValue)
+ }
+
+ result += literals[len(literals)-1]
+
+ return result
+}
+
+func evalNixExpr(literals []string, exprs ...any) any {
+ expr := buildNixExpr(literals, exprs...)
+ data := []byte(runcmd("nix", "eval", "--json", "--expr", expr))
+ var result any
+ if err := json.Unmarshal(data, &result); err != nil {
+ panic(err)
+ }
+
+ return result
+}
+
func main() {
internal.HandleStartup()
@@ -114,6 +158,7 @@ func main() {
defer func() {
if r := recover(); r != nil {
slog.Error("error in JS", "err", r)
+ debug.PrintStack()
}
}()
@@ -131,6 +176,14 @@ func main() {
"tag": dockertag,
})
+ vm.Set("file", map[string]any{
+ "write": func(fname, data string) {
+ if err := os.WriteFile(fname, []byte(data), 0660); err != nil {
+ panic(err)
+ }
+ },
+ })
+
vm.Set("fly", map[string]any{
"deploy": flydeploy,
})
@@ -144,12 +197,15 @@ func main() {
})
vm.Set("log", map[string]any{
- "info": lg.Println,
+ "info": lg.Println,
+ "println": fmt.Println,
})
vm.Set("nix", map[string]any{
"build": nixbuild,
"hashURL": func(fileURL string) string { return runcmd("nix-prefetch-url", fileURL) },
+ "expr": buildNixExpr,
+ "eval": evalNixExpr,
})
vm.Set("slug", map[string]any{
diff --git a/cmd/yeet/yeetfile.js b/cmd/yeet/yeetfile.js
index 744d6c1..1b9bebe 100644
--- a/cmd/yeet/yeetfile.js
+++ b/cmd/yeet/yeetfile.js
@@ -1,5 +1,13 @@
-log.info("cwd", yeet.cwd);
+log.println(nix.expr`let hi = ${"there"}; in hi`);
-log.info(yeet.runcmd("ls"))
+const url = "https://xena.greedo.xeserv.us/files/slugs/johaus-061520192052.tar.gz";
+const hash = "0cfx2skh7bz9w4p6xbcns14wgf2szkqlrga6dvnxrhlh3i0if519";
-log.info(yeet.goos, yeet.goarch) \ No newline at end of file
+log.println(nix.expr`src = builtins.fetchurl {
+ url = ${url};
+ sha256 = ${hash};
+}`)
+
+const greeting = "Hello"
+const data = nix.eval`{ greeting = ${greeting}; }`;
+log.info(`greeting = ${data.greeting}`);