❄️ Add Nix flake and pre-commit hooks

Setting up the different tools for development can be hard for new
contributors. This commit adds a Nix flake that provides a reproducible
development environment which is easy to set up and ensures consistent
code style across all subprojects.

The initial flake contains `git`, `pre-commit`, `which` and `bazelisk`
wrapped as a `bazel` executable. Dependencies like Rust, Scala etc may
be added in the `packages` attribute in the future.

The current pre-commit configuration is a no-op. Future commits may
enable the currently disabled parts of `pre-commit-hooks.nix` for a
gradual transition.

Supports `(x86_64|aarch64) (Linux|MacOS)` systems.
This commit is contained in:
Aaron Siddhartha Mondal 2023-03-31 23:21:25 +02:00
parent ec83d01dca
commit eaae3e286e
No known key found for this signature in database
6 changed files with 500 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

4
.gitignore vendored
View File

@ -1,2 +1,6 @@
.DS_Store
# Autogenerated by the Nix flake.
.devenv/
.direnv/
.pre-commit-config.yaml

View File

@ -32,6 +32,23 @@ These are the main components of the Recommendation Algorithm included in this r
We include Bazel BUILD files for most components, but not a top level BUILD or WORKSPACE file.
## Development environment
The unified developement environment is still in an experimental state.
Contributions are welcome!
You can set up a development environment with various tooling via the nix flake:
1. Install the [nix package manager](https://nixos.org/download.html) and enable
[flakes](https://nixos.wiki/wiki/Flakes).
2. Enter the development shell by running `nix develop` in the root directory of
the repository.
3. Optionally, install [direnv](https://github.com/direnv/direnv/blob/master/docs/installation.md)
which will automatically drop you into the development environment when you
`cd` into your local clone.
## Contributing
We invite the community to submit GitHub issues and pull requests for suggestions on improving the recommendation algorithm. We are working on tools to manage these suggestions and sync changes to our internal repository. Any security concerns or issues should be routed to our official [bug bounty program](https://hackerone.com/twitter) through HackerOne. We hope to benefit from the collective intelligence and expertise of the global community in helping us identify issues and suggest improvements, ultimately leading to a better Twitter.

341
flake.lock generated Normal file
View File

@ -0,0 +1,341 @@
{
"nodes": {
"devenv": {
"inputs": {
"flake-compat": "flake-compat",
"nix": "nix",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
},
"locked": {
"lastModified": 1678113758,
"narHash": "sha256-mD3SkN43b1s5CJ8Rx3l2oK3Dqgs+6Ze0FfWrdMcrrYk=",
"owner": "cachix",
"repo": "devenv",
"rev": "6455f319fc90e0be2071327093c5458f9afc61bf",
"type": "github"
},
"original": {
"owner": "cachix",
"ref": "latest",
"repo": "devenv",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-compat_2": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"locked": {
"lastModified": 1678901627,
"narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_3": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"devenv",
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"gitignore_2": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks-nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660459072,
"narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "a20de23b925fd8264fd7fad6454652e142fd7f73",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"lowdown-src": {
"flake": false,
"locked": {
"lastModified": 1633514407,
"narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=",
"owner": "kristapsdz",
"repo": "lowdown",
"rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8",
"type": "github"
},
"original": {
"owner": "kristapsdz",
"repo": "lowdown",
"type": "github"
}
},
"nix": {
"inputs": {
"lowdown-src": "lowdown-src",
"nixpkgs": [
"devenv",
"nixpkgs"
],
"nixpkgs-regression": "nixpkgs-regression"
},
"locked": {
"lastModified": 1676545802,
"narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=",
"owner": "domenkozar",
"repo": "nix",
"rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f",
"type": "github"
},
"original": {
"owner": "domenkozar",
"ref": "relaxed-flakes",
"repo": "nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1677534593,
"narHash": "sha256-PuZSAHeq4/9pP/uYH1FcagQ3nLm/DrDrvKi/xC9glvw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "3ad64d9e2d5bf80c877286102355b1625891ae9a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-regression": {
"locked": {
"lastModified": 1643052045,
"narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1673800717,
"narHash": "sha256-SFHraUqLSu5cC6IxTprex/nTsI81ZQAtDvlBvGDWfnA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2f9fd351ec37f5d479556cd48be4ca340da59b8f",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-22.11",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable_2": {
"locked": {
"lastModified": 1678872516,
"narHash": "sha256-/E1YwtMtFAu2KUQKV/1+KFuReYPANM2Rzehk84VxVoc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9b8e5abb18324c7fe9f07cb100c3cd4a29cda8b8",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-22.11",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1680296362,
"narHash": "sha256-zI8rKfSoRf261clT5Fi5M+buLyN1z5FgWFgnPHgJd1Y=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "ff40c8086733f2104d416b6ffd4770068a1fd43f",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1678898370,
"narHash": "sha256-xTICr1j+uat5hk9FyuPOFGxpWHdJRibwZC+ATi0RbtE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ac718d02867a84b42522a0ece52d841188208f2c",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": [
"devenv",
"flake-compat"
],
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"devenv",
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1677160285,
"narHash": "sha256-tBzpCjMP+P3Y3nKLYvdBkXBg3KvTMo3gvi8tLQaqXVY=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "2bd861ab81469428d9c823ef72c4bb08372dd2c4",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"pre-commit-hooks-nix": {
"inputs": {
"flake-compat": "flake-compat_2",
"flake-utils": "flake-utils_3",
"gitignore": "gitignore_2",
"nixpkgs": "nixpkgs_3",
"nixpkgs-stable": "nixpkgs-stable_2"
},
"locked": {
"lastModified": 1680170909,
"narHash": "sha256-FtKU/edv1jFRr/KwUxWTYWXEyj9g8GBrHntC2o8oFI8=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "29dbe1efaa91c3a415d8b45d62d48325a4748816",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2",
"pre-commit-hooks-nix": "pre-commit-hooks-nix"
}
}
},
"root": "root",
"version": 7
}

63
flake.nix Normal file
View File

@ -0,0 +1,63 @@
{
description = "Twitter development environment";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
devenv.url = "github:cachix/devenv/latest";
pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix";
};
outputs = { self, nixpkgs, flake-utils, devenv, pre-commit-hooks-nix, ... } @ inputs:
flake-utils.lib.eachSystem [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
]
(system:
let
pkgs = import nixpkgs { inherit system; };
in
rec {
hooks = import ./pre-commit-hooks.nix {
inherit pkgs;
};
checks = {
pre-commit-check = pre-commit-hooks-nix.lib.${system}.run {
src = ./.;
hooks = hooks;
};
};
devShells = {
default = mkTwitterShell;
};
mkTwitterShell = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [{
pre-commit.hooks = hooks;
scripts.bazel.exec = "${pkgs.bazelisk}/bin/bazelisk $@";
packages = [
pkgs.git
pkgs.pre-commit
pkgs.which
];
enterShell = ''
# Remove potential previous Bazel aliases.
[[ $(type -t bazel) == "alias" ]] && unalias bazel
# Prettier color output for the ls command.
alias ls='ls --color=auto'
'';
}];
};
});
}

74
pre-commit-hooks.nix Normal file
View File

@ -0,0 +1,74 @@
{ pkgs, ... }:
{
# Default hooks
# trailing-whitespace-fixer = {
# enable = true;
# name = "trailing-whitespace";
# description = "Remove trailing whitespace";
# entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/trailing-whitespace-fixer";
# types = [ "text" ];
# };
# end-of-file-fixer = {
# enable = true;
# name = "end-of-file-fixer";
# description = "Remove trailing whitespace";
# entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/end-of-file-fixer";
# excludes = [ "png" ];
# types = [ "text" ];
# };
fix-byte-order-marker = {
enable = true;
name = "fix-byte-order-marker";
entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/fix-byte-order-marker";
types = [ "text" ];
};
mixed-line-ending = {
enable = true;
name = "mixed-line-ending";
entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/mixed-line-ending";
excludes = [ "png" ];
types = [ "text" ];
};
check-case-conflict = {
enable = true;
name = "check-case-conflict";
entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/check-case-conflict";
types = [ "text" ];
};
detect-private-key = {
enable = true;
name = "detect-private-key";
entry = "${pkgs.python311Packages.pre-commit-hooks}/bin/detect-private-key";
types = [ "text" ];
};
# Starlark
# bazel-buildifier-format = {
# enable = true;
# name = "buildifier format";
# description = "Format Starlark";
# entry = "${pkgs.bazel-buildtools}/bin/buildifier";
# types = [ "bazel" ];
# };
# bazel-buildifier-lint = {
# enable = true;
# name = "buildifier lint";
# description = "Lint Starlark";
# entry = "${pkgs.bazel-buildtools}/bin/buildifier -lint=warn";
# types = [ "bazel" ];
# };
# YAML
# yamllint = {
# enable = true;
# };
# Nix
nixpkgs-fmt.enable = true;
# Markdown
# markdownlint = {
# enable = true;
# types = [ "markdown" ];
# };
}