diff --git a/README.md b/README.md index d3dde31..8031e10 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,21 @@ Currently supported APIs: * [x] [Google Vertex AI](https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings) There are also simple command line tools provided by this project that let you query the APIs for text embeddings passed in via cli flags. + +## nix + +The project provides a simple `nix` flake tha leverages [gomod2nix](https://github.com/nix-community/gomod2nix) for consistent Go environments and builds. + +To get started just run +```shell +nix develop +``` + +And you'll be dropped into development shell. + +In addition, each command is exposed as a `nix` app so you can run them as follows: +```shell +nix run ".#vertexai" -- -help +``` + +**NOTE:** `gomod2nix` vendors dependencies into `nix` store so every time you add a new dependency you must run `gomod2nix generate` that updates `gomod2nix.toml` diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..5403fca --- /dev/null +++ b/default.nix @@ -0,0 +1,22 @@ +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +, buildGoApplication ? pkgs.buildGoApplication +}: + +buildGoApplication { + pname = "go-embeddings"; + version = "0.1"; + pwd = ./.; + src = ./.; + modules = ./gomod2nix.toml; + doCheck = false; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..01fb1d6 --- /dev/null +++ b/flake.lock @@ -0,0 +1,85 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gomod2nix": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1699950847, + "narHash": "sha256-xN/yVtqHb7kimHA/WvQFrEG5WS38t0K+A/W+j/WhQWM=", + "owner": "nix-community", + "repo": "gomod2nix", + "rev": "05c993c9a5bd55a629cd45ed49951557b7e9c61a", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "gomod2nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1701068326, + "narHash": "sha256-vmMceA+q6hG1yrjb+MP8T0YFDQIrW3bl45e7z24IEts=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "8cfef6986adfb599ba379ae53c9f5631ecd2fd9c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "gomod2nix": "gomod2nix", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..83792d9 --- /dev/null +++ b/flake.nix @@ -0,0 +1,43 @@ +{ + description = "go-embeddings nix flake"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.gomod2nix.url = "github:nix-community/gomod2nix"; + inputs.gomod2nix.inputs.nixpkgs.follows = "nixpkgs"; + inputs.gomod2nix.inputs.flake-utils.follows = "flake-utils"; + + outputs = { self, nixpkgs, flake-utils, gomod2nix }: + (flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + # The current default sdk for macOS fails to compile go projects, so we use a newer one for now. + # This has no effect on other platforms. + callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage; + in + { + packages.default = callPackage ./. { + inherit (gomod2nix.legacyPackages.${system}) buildGoApplication; + }; + devShells.default = callPackage ./shell.nix { + inherit (gomod2nix.legacyPackages.${system}) mkGoEnv gomod2nix; + }; + apps = { + cohere = { + type = "app"; + program = "${self.packages.${system}.default}/bin/cohere"; + }; + openai = { + type = "app"; + program = "${self.packages.${system}.default}/bin/openai"; + }; + vertexai = { + type = "app"; + program = "${self.packages.${system}.default}/bin/vertexai"; + }; + }; + }) + ); +} diff --git a/gomod2nix.toml b/gomod2nix.toml new file mode 100644 index 0000000..d96f5d3 --- /dev/null +++ b/gomod2nix.toml @@ -0,0 +1,24 @@ +schema = 3 + +[mod] + [mod."cloud.google.com/go/compute"] + version = "v1.20.1" + hash = "sha256-HQvSLdoSagQsuqOb/D9+xMc8nE9Y44Z30rQgd2AGEr8=" + [mod."cloud.google.com/go/compute/metadata"] + version = "v0.2.3" + hash = "sha256-kYB1FTQRdTDqCqJzSU/jJYbVUGyxbkASUKbEs36FUyU=" + [mod."github.com/golang/protobuf"] + version = "v1.5.3" + hash = "sha256-svogITcP4orUIsJFjMtp+Uv1+fKJv2Q5Zwf2dMqnpOQ=" + [mod."golang.org/x/net"] + version = "v0.19.0" + hash = "sha256-3M5rKEvJx4cO/q+06cGjR5sxF5JpnUWY0+fQttrWdT4=" + [mod."golang.org/x/oauth2"] + version = "v0.15.0" + hash = "sha256-exA/abu6WOR7Cwqa41LpnTD2xQNRZMYU5CnBKvXHx8Y=" + [mod."google.golang.org/appengine"] + version = "v1.6.7" + hash = "sha256-zIxGRHiq4QBvRqkrhMGMGCaVL4iM4TtlYpAi/hrivS4=" + [mod."google.golang.org/protobuf"] + version = "v1.31.0" + hash = "sha256-UdIk+xRaMfdhVICvKRk1THe3R1VU+lWD8hqoW/y8jT0=" diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..64a40a7 --- /dev/null +++ b/shell.nix @@ -0,0 +1,26 @@ +{ pkgs ? ( + let + inherit (builtins) fetchTree fromJSON readFile; + inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix; + in + import (fetchTree nixpkgs.locked) { + overlays = [ + (import "${fetchTree gomod2nix.locked}/overlay.nix") + ]; + } + ) +, mkGoEnv ? pkgs.mkGoEnv +, gomod2nix ? pkgs.gomod2nix +, golangci-lint ? pkgs.golangci-lint +}: + +let + goEnv = mkGoEnv { pwd = ./.; }; +in +pkgs.mkShell { + packages = [ + goEnv + gomod2nix + golangci-lint + ]; +}