nix/scripts.nix (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | { pkgs ? import <nixpkgs> { }, ... }:
let
watchFlake = with pkgs; ''
${watchexec}/bin/watchexec --restart \
--watch flake.nix \
--watch flake.lock \
'';
imageName = (builtins.fromTOML (builtins.readFile ../fly.toml)).build.image;
nonDarwinSystem = builtins.replaceStrings [ "darwin" ] [ "linux" ] pkgs.stdenv.system;
flySystem = "x86_64-linux";
mkAttr = type: system: ".#docker-${type}-${system}";
sh = (pkgs.lib.optionalString pkgs.stdenv.isDarwin "ssh linux-builder ")
+ "sh";
stream = system: "nix build --print-out-paths ${(mkAttr "stream") system} | ${sh}";
image = system: "nix build ${(mkAttr "image") system}";
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 nonDarwinSystem}")
(writeShellScriptBin "stream-fly" "${stream flySystem}")
(writeShellScriptBin "image" "${image nonDarwinSystem}")
(writeShellScriptBin "image-fly" "${image flySystem}")
(writeShellScriptBin "load-locally" ''
stream | ${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
TAG=$(git rev-parse HEAD)
IMAGE=${imageName}:$TAG
push-to-registry docker://$IMAGE
${pkgs.flyctl}/bin/flyctl deploy --image $IMAGE
'')
]
|