/ flake.nix
flake.nix
 1  {
 2    inputs = {
 3      nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
 4      systems.url = "github:nix-systems/default";
 5      devenv.url = "github:cachix/devenv";
 6  
 7      gomod2nix = {
 8        url = "github:nix-community/gomod2nix";
 9        inputs.nixpkgs.follows = "nixpkgs";
10      };
11    };
12  
13    outputs = {
14      self,
15      nixpkgs,
16      devenv,
17      systems,
18      gomod2nix,
19      ...
20    } @ inputs: let
21      forEachSystem = nixpkgs.lib.genAttrs (import systems);
22    in {
23      devShells = forEachSystem (system: let
24        pkgs = nixpkgs.legacyPackages.${system};
25        # The current default sdk for macOS fails to compile go projects, so we use a newer one for now.
26        # This has no effect on other platforms.
27        callPackage = pkgs.darwin.apple_sdk_11_0.callPackage or pkgs.callPackage;
28      in {
29        packages.default = callPackage ./. {
30          inherit (gomod2nix.legacyPackages.${system}) buildGoApplication;
31        };
32  
33        default = devenv.lib.mkShell {
34          inherit inputs pkgs;
35          modules = [
36            {
37              languages.go = {
38                enable = true;
39                package = pkgs.go_1_22;
40              };
41  
42              packages = with pkgs; [
43                gomod2nix.legacyPackages.${system}.gomod2nix
44                golangci-lint
45                pre-commit
46              ];
47  
48              pre-commit.hooks.gomod2nix = {
49                enable = true;
50                always_run = true;
51                name = "gomod2nix";
52                description = "Run gomod2nix before commit";
53                pass_filenames = false;
54                entry = "${gomod2nix.legacyPackages.${system}.gomod2nix}/bin/gomod2nix";
55              };
56            }
57          ];
58        };
59      });
60    };
61  }