/ flake.nix
flake.nix
  1  # SPDX-FileCopyrightText: 2024 Mass Labs
  2  #
  3  # SPDX-License-Identifier: Unlicense
  4  {
  5    description = "MassMarket Typescript Packages";
  6    inputs = {
  7      nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  8      systems.url = "github:nix-systems/default";
  9      flake-parts.url = "github:hercules-ci/flake-parts";
 10      flake-root.url = "github:srid/flake-root";
 11      pre-commit-hooks = {
 12        url = "github:cachix/git-hooks.nix";
 13        inputs.nixpkgs.follows = "nixpkgs";
 14      };
 15  
 16      relay.url = "github:masslbs/relay/order-state-locking";
 17      contracts.follows = "relay/contracts";
 18      schema.follows = "relay/schema";
 19      # in case we need to test new vectors, etc. we can override the input like this:
 20      # schema.url = "github:masslbs/network-schema/myBranch
 21    };
 22  
 23    outputs = inputs @ {
 24      nixpkgs,
 25      flake-parts,
 26      schema,
 27      contracts,
 28      pre-commit-hooks,
 29      relay,
 30      self,
 31      systems,
 32      ...
 33    }:
 34      flake-parts.lib.mkFlake {inherit inputs;} {
 35        systems = import systems;
 36        imports = [
 37          inputs.pre-commit-hooks.flakeModule
 38          inputs.flake-root.flakeModule
 39        ];
 40        perSystem = {
 41          config,
 42          pkgs,
 43          system,
 44          ...
 45        }: {
 46          pre-commit = {
 47            check.enable = true;
 48            settings = {
 49              src = ./.;
 50              hooks = {
 51                alejandra.enable = true;
 52                typos = {
 53                  # this tell typos not to check excluded files even if pre-commit tell typos to check them
 54                  args = ["--force-exclude"];
 55                  enable = true;
 56                  settings = {
 57                    configPath = "./typos.toml";
 58                  };
 59                };
 60                #TODO: tries to download modules on nix flake check
 61                denolint = {
 62                  enable = true;
 63                  settings.configPath = "./deno.json";
 64                };
 65                denofmt = {
 66                  enable = true;
 67                  settings.configPath = "./deno.json";
 68                };
 69              };
 70            };
 71          };
 72          devShells.default = pkgs.mkShell {
 73            inputsFrom = [config.flake-root.devShell]; # Provides $FLAKE_ROOT in dev shell
 74            MASS_TEST_VECTORS = "${schema.packages.${system}.default}";
 75            NETWORK_SCHEMA_PATH = "${schema}";
 76            MASS_CONTRACTS_PATH = "${contracts.packages.${system}.default}";
 77  
 78            shellHook = ''
 79                          ${config.pre-commit.settings.installationScript}
 80                          export RELAY_ENDPOINT=http://localhost:4444/v5
 81  
 82                          if [ -z "$IPFS_PATH" ]; then
 83                            export IPFS_PATH=$FLAKE_ROOT/data/ipfs
 84                          fi
 85                          pushd $FLAKE_ROOT
 86                          # only runs when the contracts have changed
 87                          touch .last-input
 88                          if [[ "$(< .last-input)" != "${contracts}" ]]; then
 89                            echo ${contracts} > .last-input
 90                            deno task -r -f contracts build
 91                          fi
 92                          popd
 93                          # Bright blue ANSI color: \033[1;34m ... \033[0m
 94                          echo -e "\033[1;34m\
 95               ╔══════════════∘∴∴∵∴∘═════════════════╗\n\
 96              :║ ███████████████████████████████████ ║:\n\
 97              :║ ████  M Λ S S   M Λ R K Ξ T ███████ ║:\n\
 98              :║ ███████████████████████████████████ ║:\n\
 99               ╚══════════════∘∴∴∵∴∘═════════════════╝\033[0m"
100            '';
101  
102            buildInputs = with pkgs;
103              [
104                relay.packages.${system}.local-testnet
105                nodejs # needed for protobuf generation
106                reuse
107                # Language servers
108                typos-lsp # code spell checker
109                nixd
110              ]
111              # deno is automatically pulled in here
112              ++ config.pre-commit.settings.enabledPackages;
113          };
114        };
115      };
116  }