about summary refs log tree commit diff stats
path: root/nix/default.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/default.nix
parent0fc79f324d54cc7a45c0ad0ffa9c75cd716e0126 (diff)
downloadwebsite-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.tar.lz
website-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.tar.zst
website-30e4cb4e4af3a6eebd3b4fb431828d284a72a10a.zip
use nix to build docker images
Diffstat (limited to 'nix/default.nix')
-rw-r--r--nix/default.nix71
1 files changed, 71 insertions, 0 deletions
diff --git a/nix/default.nix b/nix/default.nix
new file mode 100644
index 0000000..a04131b
--- /dev/null
+++ b/nix/default.nix
@@ -0,0 +1,71 @@
+{ pkgs, self }:
+let
+  revision = "${self.lastModifiedDate}-${self.shortRev or "dirty"}";
+  version = "unstable-${self.shortRev or "dirty"}";
+  mkDocker = server:
+    let
+      PORT = 3000;
+    in
+    pkgs.dockerTools.streamLayeredImage {
+      name = "registry.fly.io/alanpearce-eu";
+      tag = revision;
+      contents = [ server ];
+      config = {
+        Cmd = [ "website" ];
+        Env = [
+          "PRODUCTION=true"
+          "PORT=${builtins.toString PORT}"
+          "ROOT=public"
+        ];
+        ExposedPorts = {
+          "${builtins.toString PORT}/tcp" = { };
+        };
+      };
+    };
+in
+rec {
+  default = server;
+  builder = pkgs.buildGoApplication {
+    pname = "website-builder";
+    inherit version;
+    src = with pkgs.lib.fileset; toSource {
+      root = ./..;
+      fileset = unions [
+        ./../go.mod
+        ./../go.sum
+        ./../cmd
+        ./../internal
+      ];
+    };
+    modules = ./gomod2nix.toml;
+    subPackages = [ "cmd/build" ];
+  };
+  server = pkgs.buildGoApplication {
+    pname = "website";
+    inherit version;
+    CGO_ENABLED = 0;
+    src = with pkgs.lib.fileset; toSource {
+      root = ./..;
+      fileset = unions [
+        ./../go.mod
+        ./../go.sum
+        ./../server.go
+        ./../internal
+        ./../config.toml
+        ./../content
+        ./../static
+        ./../templates
+      ];
+    };
+    buildInputs = [ builder ];
+    prePatch = ''
+      ${builder}/bin/build
+    '';
+    modules = ./gomod2nix.toml;
+    ldflags = [ "-s" "-w" ];
+  };
+  docker = mkDocker server;
+  docker-aarch64-linux = mkDocker (self.packages.aarch64-linux.server);
+  docker-x86_64-linux = mkDocker (self.packages.x86_64-linux.server);
+  fly = docker-x86_64-linux;
+}