diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.github/actions/setup-nix/action.yml b/.github/actions/setup-nix/action.yml new file mode 100644 index 0000000..5b12402 --- /dev/null +++ b/.github/actions/setup-nix/action.yml @@ -0,0 +1,31 @@ +name: Setup Nix +description: Install Nix, restore/save the Nix store cache, and run flake check. + +inputs: + github-token: + description: Token passed to install-nix-action for authenticated fetches. + required: true + +runs: + using: composite + steps: + - name: Install Nix + uses: cachix/install-nix-action@v31 + with: + github_access_token: ${{ inputs.github-token }} + + - name: Cache Nix store + uses: nix-community/cache-nix-action@v7 + with: + primary-key: nix-${{ runner.os }}-${{ hashFiles('flake.nix', 'flake.lock') }} + restore-prefixes-first-match: nix-${{ runner.os }}- + # Cap store at ~5 GiB on save so cache fits under GHA's 10 GB repo budget + gc-max-store-size-linux: 5368709120 + purge: true + purge-prefixes: nix-${{ runner.os }}- + purge-created: 0 + purge-primary-key: never + + - name: Nix flake check + shell: bash + run: nix flake check diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a35a28..7b72725 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,14 +18,11 @@ jobs: - name: Checkout uses: actions/checkout@v7.0.0 - - name: Install Go - uses: actions/setup-go@v6.5.0 - with: - go-version-file: go.mod - cache: true + - name: Setup Nix + uses: ./.github/actions/setup-nix - name: Check for vulnerabilities - run: go run golang.org/x/vuln/cmd/govulncheck@latest ./... + run: nix develop --command govulncheck ./... - name: golangci-lint uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0 @@ -33,4 +30,4 @@ jobs: version: v2.12.2 - name: Build and Test - run: make + run: nix develop --command make diff --git a/Makefile b/Makefile index 1647564..98e506b 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ bin/.generate: $(go_files) bin/.vendor fmt: bin/.generate $(go_files) # Formatting files... - @go run golang.org/x/tools/cmd/goimports -w $(go_files) + @goimports -w $(go_files) bin/.vet: bin/.generate $(go_files) go vet ./... @@ -29,9 +29,9 @@ bin/.vet: bin/.generate $(go_files) bin/.fmtcheck: bin/.generate $(go_files) # Checking format of Go files... - @GOIMPORTS=$$(go run golang.org/x/tools/cmd/goimports -l $(go_files)) && \ + @GOIMPORTS=$$(goimports -l $(go_files)) && \ if [ "$$GOIMPORTS" != "" ]; then \ - go run golang.org/x/tools/cmd/goimports -d $(go_files); \ + goimports -d $(go_files); \ exit 1; \ fi @touch bin/.fmtcheck diff --git a/README.md b/README.md index 64d0289..582f4ef 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,22 @@ func main() { } ``` +## Development + +The repo ships a [Nix flake](flake.nix) that pins the toolchain (Go, `golangci-lint`, `gotools`, `govulncheck`, GNU `make`) so local development matches CI. Combined with [direnv](https://direnv.net/), your shell automatically enters that environment when you `cd` into the repo. + +### Setup + +1. Install Nix with flakes enabled — the [Determinate Nix Installer](https://determinate.systems/nix-installer/) is the easiest option; the [official Nix installer](https://nixos.org/download/) works too but requires [enabling flakes manually](https://nixos.wiki/wiki/Flakes#Enable_flakes_permanently_in_NixOS). +2. Install [direnv](https://direnv.net/docs/installation.html) and hook it into your shell. +3. Install [nix-direnv](https://github.com/nix-community/nix-direnv) so direnv can load the flake's dev shell. +4. From the repo root, activate the environment: + ```shell + direnv allow + ``` + +Every subsequent `cd` into the repo brings up a shell with the pinned toolchain on `PATH`. Run `make` to execute the same checks CI runs. + ## Releasing Releases are published as Git tags of the form `vX.Y.Z`, so Go consumers pull a diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..1f0da5e --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1783279667, + "narHash": "sha256-/NAkDSsve+GNM0Bt6tleJdCGfsTlK89nPjkVOzZMo0s=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f205b5574fd0cb7da5b702a2da51507b7f4fdd1b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "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..92c0b2f --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + description = "Flake for Redis Cloud Go API"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + }; + in + { + devShells.default = pkgs.mkShell { + buildInputs = [ + pkgs.gnumake + + pkgs.go_1_25 + pkgs.golangci-lint + pkgs.gotools + pkgs.govulncheck + ]; + }; + } + ); +}