From 257ade944e49559b898fc548f6e65e65d3319d7e Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 17 Aug 2025 19:41:36 -0700 Subject: manage the repository with nix Add a flake.nix to manage the development shell, the formatters, add some pre-commit checks, and such. This might evolve a bit over time but this is a good starting point. --- .envrc | 1 + .gitignore | 1 + flake.lock | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ treefmt.nix | 30 +++++++++++++ 5 files changed, 288 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 treefmt.nix 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/.gitignore b/.gitignore new file mode 100644 index 0000000..e90b739 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pre-commit-config.yaml diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..6d1a15e --- /dev/null +++ b/flake.lock @@ -0,0 +1,142 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1747046372, + "narHash": "sha256-CIVLLkVgvHYbgI2UpXvIIBJ12HWgX+fjA8Xf8PUmqCY=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "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" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1755274400, + "narHash": "sha256-rTInmnp/xYrfcMZyFMH3kc8oko5zYfxsowaLv1LVobY=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "ad7196ae55c295f53a7d1ec39e4a06d922f3b899", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-25.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1755446520, + "narHash": "sha256-I0Ok1OGDwc1jPd8cs2VvAYZsHriUVFGIUqW+7uSsOUM=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "4b04db83821b819bbbe32ed0a025b31e7971f22e", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1754847726, + "narHash": "sha256-2vX8QjO5lRsDbNYvN9hVHXLU6oMl+V/PsmIiJREG4rE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "7d81f6fb2e19bf84f1c65135d1060d829fae2408", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d0c01b3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,114 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + flake-utils.url = "github:numtide/flake-utils"; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + pre-commit-hooks = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + outputs = + { + self, + nixpkgs, + treefmt-nix, + flake-utils, + pre-commit-hooks, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { inherit system; }; + + treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix; + + ciPackagesBackend = with pkgs; [ + go # Need that obviously + gofumpt # Go formatter + golangci-lint # Local/CI linter + gotestsum # Pretty tester + goperf # Go performance suite + ]; + + devPackages = with pkgs; [ + gopls + gotools + ]; + in + { + formatter = treefmtEval.config.build.wrapper; + + checks = { + # Throws an error if any of the source files are not correctly formatted + # when you run `nix flake check --print-build-logs`. Useful for CI + treefmt = treefmtEval.config.build.check self; + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + format = { + enable = true; + name = "Format with treefmt"; + pass_filenames = true; + entry = "${treefmtEval.config.build.wrapper}/bin/treefmt"; + stages = [ + "pre-commit" + "pre-push" + ]; + }; + golangci-lint = { + enable = true; + package = pkgs.golangci-lint; + pass_filenames = true; + stages = [ "pre-push" ]; + }; + unit-tests-go = { + enable = true; + name = "Run unit tests"; + entry = "gotestsum --format testdox ./..."; + pass_filenames = false; + stages = [ "pre-push" ]; + types = [ "go" ]; + }; + }; + }; + + # ci = + # pkgs.runCommand "ci-runner" + # { + # nativeBuildInputs = [ ci-script ] ++ ciPackages; # Include ci-script and ciPackages + # inherit (self.checks.${system}.pre-commit-check) shellHook; # Reuse pre-commit shellHook if needed + # } + # '' + # echo "Running CI script with essential packages..." + # # Ensure the ci-script is executable and in PATH for this check + # export PATH=$out/bin:$PATH + # run-ci + # touch $out # Ensure the output path is created + # ''; + }; + + devShells = { + default = pkgs.mkShell { + buildInputs = + [ ] ++ ciPackagesBackend ++ devPackages ++ self.checks.${system}.pre-commit-check.enabledPackages; + + shellHook = '' + ${self.checks.${system}.pre-commit-check.shellHook} + ''; + }; + + ci-backend = pkgs.mkShell { + buildInputs = [ ] ++ ciPackagesBackend; + CI = true; + shellHook = '' + echo "Entering CI backend shell. Only essential CI tools available." + ''; + }; + }; + } + ); +} diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 0000000..6d19624 --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,30 @@ +{ pkgs, ... }: +{ + # See https://github.com/numtide/treefmt-nix#supported-programs + projectRootFile = ".git/config"; + settings.global.includes = [ + "*.go" + "*.yaml" + "*.yml" + "*.md" + "*.nix" + ]; + settings.global.fail-on-change = true; + settings.global.no-cache = true; + programs.gofumpt = { + enable = true; + package = pkgs.gofumpt; + }; + programs.goimports.enable = true; + programs.golines.enable = true; + # GitHub Actions + programs.yamlfmt.enable = true; + programs.actionlint.enable = true; + # Markdown + programs.mdformat.enable = true; + # Nix + programs.nixfmt = { + enable = true; + package = pkgs.nixfmt-rfc-style; + }; +} -- cgit v1.2.3