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. --- flake.nix | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 flake.nix (limited to 'flake.nix') 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." + ''; + }; + }; + } + ); +} -- cgit v1.2.3