aboutsummaryrefslogblamecommitdiff
path: root/flake.nix
blob: f96be8bca7f7354fa1db3aef3ac13ccdd1b362b5 (plain) (tree)




















                                                     





                                         
                                                       

                     
                                       












                                            

             





                                                           


                                                                    
                                 












                                         
                                
                                       

          


































                                                                                  




                                  
                                                                                                         


                                                                 

                                             


               

                                            

                          

                                             
                                                                          





               
{
  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,
    }:
    let
      # Import our packages and overlay
      overlay = import ./nix/overlay.nix;

      # Import NixOS modules
      nixosModules = {
        fcuny-net = import ./nix/modules/fcuny.net.nix;
        default = {
          imports = [
            ./nix/modules/fcuny.net.nix
          ];
        };
      };
    in
    {
      # Export the overlay for others to use
      overlays.default = overlay;

      # Export NixOS modules
      inherit nixosModules;
      nixosModule = nixosModules.default;
    }
    // flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ overlay ];
        };

        packages = import ./nix/packages { inherit pkgs; };

        treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;

        ciPackages = 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
      {
        packages = packages // {
          default = packages.fcuny-net;
        };

        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" ];
              };
            };
          };
        };

        devShells = {
          default = pkgs.mkShell {
            buildInputs =
              [ ] ++ ciPackages ++ devPackages ++ self.checks.${system}.pre-commit-check.enabledPackages;

            shellHook = ''
              ${self.checks.${system}.pre-commit-check.shellHook}
              export GOTOOLCHAIN=local
              export GOPRIVATE=go.fcuny.net/*
            '';
          };

          ci = pkgs.mkShell {
            buildInputs = [ ] ++ ciPackages;
            CI = true;
            shellHook = ''
              export GOTOOLCHAIN=local
              export GOPRIVATE=go.fcuny.net/*
              echo "Entering CI shell. Only essential CI tools available."
            '';
          };
        };
      }
    );
}