about summary refs log tree commit diff stats
path: root/nix/scripts.nix
diff options
context:
space:
mode:
authorAlan Pearce2024-04-19 16:11:33 +0200
committerAlan Pearce2024-04-19 22:13:42 +0200
commit30e4cb4e4af3a6eebd3b4fb431828d284a72a10a (patch)
treeca5ae02e140f9d1b35a9824da04a71e32a981a47 /nix/scripts.nix
parent0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126 (diff)
downloadwebsite-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.tar.lz
website-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.tar.zst
website-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.zip
use nix to build docker images
Diffstat (limited to 'nix/scripts.nix')
-rw-r--r--nix/scripts.nix49
1 files changed, 49 insertions, 0 deletions
diff --git a/nix/scripts.nix b/nix/scripts.nix
new file mode 100644
index 0000000..94e4d85
--- /dev/null
+++ b/nix/scripts.nix
@@ -0,0 +1,49 @@
+{ pkgs ? import <nixpkgs> { }, ... }:
+let
+  watchFlake = with pkgs; ''
+    ${watchexec}/bin/watchexec --restart \
+     --watch flake.nix \
+     --watch flake.lock \
+  '';
+  image = (builtins.fromTOML (builtins.readFile ../fly.toml)).build.image;
+  nonDarwinSystem = builtins.replaceStrings [ "darwin" ] [ "linux" ] pkgs.stdenv.system;
+  attr = "docker-${nonDarwinSystem}";
+  sh = (pkgs.lib.optionalString pkgs.stdenv.isDarwin "ssh linux-builder ")
+    + "sh";
+  stream = attr: "nix build --print-out-paths .#${attr} | ${sh}";
+in
+with pkgs; [
+  (writeShellScriptBin "watch-builder" ''
+    ${watchFlake} "direnv exec . watchexec -i server.go -i public -r go run ./cmd/build $@"
+  '')
+  (writeShellScriptBin "watch-server" ''
+    ${watchFlake} "direnv exec . watchexec -r go run ./server.go $@"
+  '')
+  (writeShellScriptBin "check-licenses" ''
+    ${go-licenses}/bin/go-licenses check --include_tests ./... --disallowed_types=restricted,forbidden
+  '')
+  (writeShellScriptBin "stream" "${stream attr}")
+  (writeShellScriptBin "stream-fly" "${stream "fly"}")
+  (writeShellScriptBin "load-locally" ''
+    ${stream attr} | ${docker-client}/bin/docker load "$@"
+  '')
+  (writeShellScriptBin "push-to-registry" ''
+    if test -z "''${1:-}"; then
+    cat <<-EOF
+        USAGE: $0 <docker-url> [skopeo-copy-options]
+
+        Example: $0 docker://some_docker_registry/myimage:tag
+
+        See: https://github.com/containers/skopeo/blob/main/docs/skopeo-copy.1.md
+    EOF
+      exit 1
+    fi
+    echo skopeo copy docker-archive:/dev/stdin "$@"
+    stream-fly | ${gzip}/bin/gzip --fast | ${skopeo}/bin/skopeo copy docker-archive:/dev/stdin "$@"
+  '')
+  (writeShellScriptBin "deploy" ''
+    set -eu
+    push-to-registry docker://${image}
+    ${pkgs.flyctl}/bin/flyctl deploy
+  '')
+]