about summary refs log tree commit diff stats
path: root/default.nix
diff options
context:
space:
mode:
authorAlan Pearce2024-06-22 16:07:12 +0200
committerAlan Pearce2024-06-22 19:18:24 +0200
commita049c9cc6b11018e7fc497876ad0402965d87397 (patch)
tree299060da298be0bdf8ea57e3c12f4de7260683a6 /default.nix
parent0fd65e3f27d6765983656c164b650fc7047ae03e (diff)
downloadwebsite-a049c9cc6b11018e7fc497876ad0402965d87397.tar.lz
website-a049c9cc6b11018e7fc497876ad0402965d87397.tar.zst
website-a049c9cc6b11018e7fc497876ad0402965d87397.zip
add CD to fly
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix66
1 files changed, 66 insertions, 0 deletions
diff --git a/default.nix b/default.nix
index b05d12e..4c29731 100644
--- a/default.nix
+++ b/default.nix
@@ -14,6 +14,27 @@ let
     runCommandLocal;
 
   version = "unstable";
+  mkDocker = type: { server, website }:
+    let
+      PORT = 80;
+    in
+    pkgs.dockerTools.${type} {
+      name = "registry.fly.io/alanpearce-eu";
+      config = {
+        Cmd = [ "${server}/bin/server" ];
+        Env = [
+          "PRODUCTION=true"
+          "LISTEN_ADDRESS=::"
+          "PORT=${builtins.toString PORT}"
+          "ROOT=${website}"
+        ];
+        ExposedPorts = {
+          "${builtins.toString PORT}/tcp" = { };
+        };
+      };
+    };
+  mkDockerStream = mkDocker "streamLayeredImage";
+  mkDockerImage = mkDocker "buildLayeredImage";
 in
 rec {
   pre-commit-check = pre-commit-hooks.run {
@@ -70,4 +91,49 @@ rec {
     } ''
     ${builder}/bin/build -s $src -d $out
   '';
+  server = buildGoApplication {
+    pname = "server";
+    inherit version;
+    CGO_ENABLED = 0;
+    src = with lib.fileset; toSource {
+      root = ./.;
+      fileset = unions [
+        ./go.mod
+        ./go.sum
+        ./cmd/server
+        ./internal
+      ];
+    };
+    modules = ./nix/gomod2nix.toml;
+    patchPhase = ''
+      ${pkgs.templ}/bin/templ generate
+    '';
+    subPackages = [ "cmd/server" ];
+    ldflags = [
+      "-s"
+      "-w"
+    ];
+  };
+  docker-stream = mkDockerStream { inherit server website; };
+  docker-image = mkDockerImage { inherit server website; };
+
+  server-amd64-linux = server.overrideAttrs (old: old // {
+    GOOS = "linux";
+    GOARCH = "amd64";
+    fixupPhase = ''
+      if [[ -d $out/bin/linux_amd64 ]]
+      then
+        mv $out/bin/linux_amd64/server $out/bin/server
+        rmdir $out/bin/linux_amd64
+      fi
+    '';
+  });
+  docker-image-amd64-linux = mkDockerImage {
+    inherit website;
+    server = server-amd64-linux;
+  };
+  docker-stream-amd64-linux = mkDockerStream {
+    inherit website;
+    server = server-amd64-linux;
+  };
 }