2022-10-31 15:08:06 +00:00
# Experimental support for developing in nix. Please reach out to @keegan or @noah if
2021-08-23 14:37:07 +00:00
# you encounter any issues.
#
# Things it does differently:
#
# - Runs postgres under ~/.sourcegraph with a unix socket. No need to manage a
# service. Must remember to run "pg_ctl stop" if you want to stop it.
#
2023-04-28 20:06:33 +00:00
# Status: everything works on linux & darwin.
2023-05-31 15:20:32 +00:00
{ pkgs
, buildFHSEnv
, mkShell
, hostPlatform
, lib
, writeShellScriptBin
} :
2021-08-23 14:37:07 +00:00
let
2023-04-28 20:06:33 +00:00
# On darwin, we let bazelisk manage the bazel version since we actually need to run two
2023-03-28 13:56:37 +00:00
# different versions thanks to aspect. Additionally bazelisk allows us to do
# things like "bazel configure". So we just install a script called bazel
# which calls bazelisk.
#
# Additionally bazel seems to break when CC and CXX is set to a nix managed
# compiler on darwin. So the script unsets those.
2023-05-31 15:20:32 +00:00
bazel-wrapper = writeShellScriptBin " b a z e l " ( if hostPlatform . isMacOS then ''
2024-01-24 18:35:53 +00:00
export CC = /usr/bin/clang
2023-03-28 13:56:37 +00:00
exec $ { pkgs . bazelisk } /bin/bazelisk " $ @ "
2023-04-28 20:06:33 +00:00
'' e l s e ''
2024-01-09 13:38:14 +00:00
unset TMPDIR TMP
2024-01-04 13:43:37 +00:00
exec $ { pkgs . bazel_7 } /bin/bazel " $ @ "
2023-04-28 20:06:33 +00:00
'' ) ;
2023-05-31 15:20:32 +00:00
bazel-watcher = writeShellScriptBin " i b a z e l " ''
2024-01-24 18:35:53 +00:00
$ { lib . optionalString hostPlatform . isMacOS " e x p o r t C C = / u s r / b i n / c l a n g " }
2023-04-28 20:06:33 +00:00
exec $ { pkgs . bazel-watcher } /bin/ibazel \
2023-05-31 15:20:32 +00:00
$ { lib . optionalString hostPlatform . isLinux " - b a z e l _ p a t h = ${ bazel-fhs } / b i n / b a z e l " } " $ @ "
2023-03-28 13:56:37 +00:00
'' ;
2023-05-31 15:20:32 +00:00
bazel-fhs = buildFHSEnv {
2023-05-19 08:52:37 +00:00
name = " b a z e l " ;
runScript = " b a z e l " ;
targetPkgs = pkgs : ( with pkgs ; [
bazel-wrapper
zlib . dev
] ) ;
# unsharePid required to preserve bazel server between bazel invocations,
# the rest are disabled just in case
unsharePid = false ;
unshareUser = false ;
unshareIpc = false ;
unshareNet = false ;
unshareUts = false ;
unshareCgroup = false ;
2023-04-28 20:06:33 +00:00
} ;
2023-05-31 13:45:39 +00:00
2023-06-07 06:36:09 +00:00
# pkgs.universal-ctags installs the binary as "ctags", not "universal-ctags"
# like zoekt expects.
universal-ctags = pkgs . writeScriptBin " u n i v e r s a l - c t a g s " ''
#!${pkgs.stdenv.shell}
exec $ { pkgs . universal-ctags } /bin/ctags " $ @ "
'' ;
2023-05-31 13:45:39 +00:00
# We have scripts which use gsed on darwin since that is what homebrew calls
# the binary for GNU sed.
gsed = pkgs . writeShellScriptBin " g s e d " '' e x e c ${ pkgs . gnused } / b i n / s e d " $@ " '' ;
2022-02-21 20:37:38 +00:00
in
2024-01-24 18:35:53 +00:00
mkShell . override { stdenv = if hostPlatform . isMacOS then pkgs . clang11Stdenv else pkgs . stdenv ; } {
2021-08-23 14:37:07 +00:00
name = " s o u r c e g r a p h - d e v " ;
# The packages in the `buildInputs` list will be added to the PATH in our shell
nativeBuildInputs = with pkgs ; [
2024-01-02 16:37:00 +00:00
bashInteractive
2024-01-25 12:50:54 +00:00
zip
2024-01-02 16:37:00 +00:00
2023-05-31 15:20:32 +00:00
# nix language server.
2023-05-03 12:56:44 +00:00
nil
2022-02-21 20:37:38 +00:00
2021-08-23 14:37:07 +00:00
# Our core DB.
2024-04-11 17:00:21 +00:00
postgresql_12
2021-08-23 14:37:07 +00:00
2023-05-31 15:20:32 +00:00
# Cache and some store data.
2021-09-29 09:46:23 +00:00
redis
2021-08-23 14:37:07 +00:00
2023-04-28 20:06:33 +00:00
# Used by symbols and zoekt-git-index to extract symbols from sourcecode.
2021-09-29 09:46:23 +00:00
universal-ctags
2021-08-23 14:37:07 +00:00
2023-05-31 15:20:32 +00:00
# Build our backend. Sometimes newer :^)
2024-03-07 00:38:43 +00:00
go_1_22
2021-08-23 14:37:07 +00:00
2021-09-27 16:30:32 +00:00
# Lots of our tooling and go tests rely on git et al.
2023-05-31 13:57:55 +00:00
comby
2021-09-29 09:46:23 +00:00
git
2022-10-25 13:40:30 +00:00
git-lfs
2023-05-31 13:57:55 +00:00
gsed
2022-06-20 12:46:11 +00:00
nssTools
2023-05-31 13:57:55 +00:00
parallel
2021-08-23 14:37:07 +00:00
2023-05-31 15:20:32 +00:00
# CI lint tools you need locally.
2021-09-29 09:46:23 +00:00
shfmt
shellcheck
2021-08-23 14:37:07 +00:00
2023-05-31 15:20:32 +00:00
# Web tools.
2023-10-31 15:55:55 +00:00
nodejs-20_x
nodejs-20_x . pkgs . pnpm
nodejs-20_x . pkgs . typescript
2023-11-16 15:41:37 +00:00
nodejs-20_x . pkgs . typescript-language-server
2022-04-11 16:20:00 +00:00
2023-04-28 20:06:33 +00:00
# Rust utils for syntax-highlighter service, currently not pinned to the same versions.
2022-04-11 16:20:00 +00:00
cargo
rustc
rustfmt
libiconv
2022-06-20 12:46:11 +00:00
clippy
sg: add commands to wrap common bazel generating commands (#59833)
### The Old
Replaces `aspect configure` (used via `bazel configure`) which had a few issues:
- Uses host Go toolchain instead of bazel-managed one
- Didnt use our //:gazelle binary, which is configured with `rules_buf` gazelle rules
- Not able to configure other gazelle rules outside of the fixed set that comes with aspect cli
### The New
`sg bazel configure` and its targets (currently `builds`, `godeps` and `rustdeps`). Passing none only runs `sg bazel configure builds`, the pseudo-target `all` runs all of them.
`sg bazel [other arg]` passes to `bazel` underneath, as a convenience option for if people get more used to running `sg bazel ...` than `bazel` directly (or from shell history)
```
$ bazel-bin/dev/sg/sg_/sg bazel help configure
NAME:
sg bazel configure - Wrappers around some commands to generate various files required by Bazel
USAGE:
sg bazel configure [category...]
DESCRIPTION:
For convenience, a number of Bazel commands are wrapped by this command to update various files required by Bazel.
Available categories:
- builds: updates BUILD.bazel files for Go & Typescript targets.
- godeps: updates the bazel Go dependency targets based on go.mod changes.
- rustdeps: updates the cargo bazel lockfile.
- all: catch-all for the above
If no categories are referenced, then 'builds' is assumed as the default.
OPTIONS:
--help, -h show help
```
```
$ bazel-bin/dev/sg/sg_/sg bazel help
Additional commands from sg:
configure Wrappers around some commands to generate various files required by Bazel
[bazel release 7.0.0- (@non-git)]
Usage: bazel <command> <options> ...
Available commands:
analyze-profile Analyzes build profile data.
aquery Analyzes the given targets and queries the action graph.
build Builds the specified targets.
canonicalize-flags Canonicalizes a list of bazel options.
clean Removes output files and optionally stops the server.
coverage Generates code coverage report for specified test targets.
cquery Loads, analyzes, and queries the specified targets w/ configurations.
dump Dumps the internal state of the bazel server process.
fetch Fetches external repositories that are prerequisites to the targets.
help Prints help for commands, or the index.
info Displays runtime info about the bazel server.
license Prints the license of this software.
mobile-install Installs targets to mobile devices.
mod Queries the Bzlmod external dependency graph
print_action Prints the command line args for compiling a file.
query Executes a dependency graph query.
run Runs the specified target.
shutdown Stops the bazel server.
sync Syncs all repositories specified in the workspace file
test Builds and runs the specified test targets.
version Prints version information for bazel.
Getting more help:
bazel help <command>
Prints help and options for <command>.
bazel help startup_options
Options for the JVM hosting bazel.
bazel help target-syntax
Explains the syntax for specifying targets.
bazel help info-keys
Displays a list of keys used by the info command.
```
## Test plan
`bazel run //dev/sg:sg -- bazel configure` and others
2024-01-31 16:54:04 +00:00
bazel-buildtools
2024-04-11 17:00:21 +00:00
buf
2023-10-06 16:11:34 +00:00
] ++ lib . optional hostPlatform . isLinux ( with pkgs ; [
# bazel via nix is broken on MacOS for us. Lets just rely on bazelisk from brew.
2023-04-28 20:06:33 +00:00
# special sauce bazel stuff.
2023-05-19 08:52:37 +00:00
bazelisk # needed to please sg, but not used directly by us
2023-10-06 16:11:34 +00:00
bazel-fhs
2023-04-21 11:14:35 +00:00
bazel-watcher
2024-01-24 18:35:53 +00:00
] ) ++ lib . optional hostPlatform . isMacOS [ bazel-wrapper ] ;
2021-08-23 14:37:07 +00:00
2023-04-28 20:06:33 +00:00
# Startup postgres, redis & set nixos specific stuff
2021-08-23 14:37:07 +00:00
shellHook = ''
2023-04-28 20:06:33 +00:00
set - h # command hashmap is not guaranteed to be enabled, but required by sg
2021-08-23 14:37:07 +00:00
. ./dev/nix/shell-hook.sh
'' ;
2022-10-31 15:08:06 +00:00
# Fix for using Delve https://github.com/sourcegraph/sourcegraph/pull/35885
2022-05-23 18:07:43 +00:00
hardeningDisable = [ " f o r t i f y " ] ;
2021-08-23 14:37:07 +00:00
# By explicitly setting this environment variable we avoid starting up
# universal-ctags via docker.
2023-06-07 06:36:09 +00:00
CTAGS_COMMAND = " ${ universal-ctags } / b i n / u n i v e r s a l - c t a g s " ;
2022-04-11 16:20:00 +00:00
RUST_SRC_PATH = " ${ pkgs . rust . packages . stable . rustPlatform . rustLibSrc } " ;
2022-08-23 12:14:44 +00:00
2023-05-08 12:13:35 +00:00
# Some of the bazel actions require some tools assumed to be in the PATH defined by the "strict action env" that we enable
# through --incompatible_strict_action_env. We can poke a custom PATH through with --action_env=PATH=$BAZEL_ACTION_PATH.
# See https://sourcegraph.com/github.com/bazelbuild/bazel@6.1.2/-/blob/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java?L532-547
2024-02-20 13:57:56 +00:00
BAZEL_ACTION_PATH = with pkgs ; lib . makeBinPath [ file hexdump bashInteractive stdenv . cc coreutils unzip zip curl gzip gnutar gnugrep gnused git patch openssh findutils perl python39 which postgresql_13 docker-credential-helpers ] ;
2023-05-08 12:13:35 +00:00
2023-04-30 05:42:33 +00:00
# bazel complains when the bazel version differs even by a patch version to whats defined in .bazelversion,
# so we tell it to h*ck off here.
# https://sourcegraph.com/github.com/bazelbuild/bazel@1a4da7f331c753c92e2c91efcad434dc29d10d43/-/blob/scripts/packages/bazel.sh?L23-28
2024-01-24 18:35:53 +00:00
USE_BAZEL_VERSION = if hostPlatform . isMacOS then " " else pkgs . bazel_7 . version ;
2024-01-31 00:47:55 +00:00
LIBTOOL = if hostPlatform . isMacOS then " / u s r / b i n / l i b t o o l " else " " ;
2021-08-23 14:37:07 +00:00
}