sourcegraph/shell.nix

151 lines
5.1 KiB
Nix
Raw Permalink Normal View History

2022-10-31 15:08:06 +00:00
# Experimental support for developing in nix. Please reach out to @keegan or @noah if
# 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.
#
# Status: everything works on linux & darwin.
{ pkgs
, buildFHSEnv
, mkShell
, hostPlatform
, lib
, writeShellScriptBin
}:
let
# On darwin, we let bazelisk manage the bazel version since we actually need to run two
# 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.
bazel-wrapper = writeShellScriptBin "bazel" (if hostPlatform.isMacOS then ''
export CC=/usr/bin/clang
exec ${pkgs.bazelisk}/bin/bazelisk "$@"
'' else ''
unset TMPDIR TMP
exec ${pkgs.bazel_7}/bin/bazel "$@"
'');
bazel-watcher = writeShellScriptBin "ibazel" ''
${lib.optionalString hostPlatform.isMacOS "export CC=/usr/bin/clang"}
exec ${pkgs.bazel-watcher}/bin/ibazel \
${lib.optionalString hostPlatform.isLinux "-bazel_path=${bazel-fhs}/bin/bazel"} "$@"
'';
bazel-fhs = buildFHSEnv {
name = "bazel";
runScript = "bazel";
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;
};
# pkgs.universal-ctags installs the binary as "ctags", not "universal-ctags"
# like zoekt expects.
universal-ctags = pkgs.writeScriptBin "universal-ctags" ''
#!${pkgs.stdenv.shell}
exec ${pkgs.universal-ctags}/bin/ctags "$@"
'';
# We have scripts which use gsed on darwin since that is what homebrew calls
# the binary for GNU sed.
gsed = pkgs.writeShellScriptBin "gsed" ''exec ${pkgs.gnused}/bin/sed "$@"'';
2022-02-21 20:37:38 +00:00
in
mkShell.override { stdenv = if hostPlatform.isMacOS then pkgs.clang11Stdenv else pkgs.stdenv; } {
name = "sourcegraph-dev";
# The packages in the `buildInputs` list will be added to the PATH in our shell
nativeBuildInputs = with pkgs; [
bashInteractive
zip
# nix language server.
nil
2022-02-21 20:37:38 +00:00
# Our core DB.
postgresql_12
# Cache and some store data.
redis
# Used by symbols and zoekt-git-index to extract symbols from sourcecode.
universal-ctags
# Build our backend. Sometimes newer :^)
go_1_22
# Lots of our tooling and go tests rely on git et al.
comby
git
git-lfs
gsed
2022-06-20 12:46:11 +00:00
nssTools
parallel
# CI lint tools you need locally.
shfmt
shellcheck
# Web tools.
nodejs-20_x
nodejs-20_x.pkgs.pnpm
nodejs-20_x.pkgs.typescript
nodejs-20_x.pkgs.typescript-language-server
# Rust utils for syntax-highlighter service, currently not pinned to the same versions.
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
buf
] ++ lib.optional hostPlatform.isLinux (with pkgs; [
# bazel via nix is broken on MacOS for us. Lets just rely on bazelisk from brew.
# special sauce bazel stuff.
bazelisk # needed to please sg, but not used directly by us
bazel-fhs
bazel-watcher
]) ++ lib.optional hostPlatform.isMacOS [ bazel-wrapper ];
# Startup postgres, redis & set nixos specific stuff
shellHook = ''
set -h # command hashmap is not guaranteed to be enabled, but required by sg
. ./dev/nix/shell-hook.sh
'';
2022-10-31 15:08:06 +00:00
# Fix for using Delve https://github.com/sourcegraph/sourcegraph/pull/35885
hardeningDisable = [ "fortify" ];
# By explicitly setting this environment variable we avoid starting up
# universal-ctags via docker.
CTAGS_COMMAND = "${universal-ctags}/bin/universal-ctags";
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
# 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
bazel: use transitions to apply cross-compile platform automatically to oci_image (#60569) Removes the need to pass `--config=docker-darwin` through the following mechanisms: 1. `--enable_platform_specific_config` to enable certain flags on macos only e.g. `--extra_toolchains @zig_sdk//toolchain:linux_amd64_gnu.2.34` and `--sandbox_add_mount_pair=/tmp` (see [.bazelrc change](https://github.com/sourcegraph/sourcegraph/pull/60569/files?file-filters%5B%5D=dotfile&show-viewed-files=true)) 2. Apply a transition (using https://github.com/fmeum/with_cfg.bzl, please view [the following great video on it](https://www.youtube.com/watch?v=U5bdQRQY-io)) on `oci_image` targets when on the `@platforms//os:macos` platform to transition to the `@zig_sdk//platform:linux_amd64` platform. - This will start at `oci_image` targets and propagate down to e.g. `go_{binary,library}` etc targets with the "transitioned" platform configuration, resulting in them being built with the transitioned-to platform 3. Remove `darwin_docker_e2e_go` config_setting and `darwin-docker` bool_flag. - These aren't necessary anymore, as the places where these were used were not in the transitive closure rooted at an `oci_image` target, meaning they wouldn't be transitioned. To review, view [the following (filtered) files](https://github.com/sourcegraph/sourcegraph/pull/60569/files?file-filters%5B%5D=.bzl&file-filters%5B%5D=.sh&file-filters%5B%5D=.yaml&file-filters%5B%5D=No+extension&file-filters%5B%5D=dotfile&show-viewed-files=true) along with [the root BUILD.bazel](https://github.com/sourcegraph/sourcegraph/pull/60569/files#diff-7fc57714ef13c3325ce2a1130202edced92fcccc0c6db34a72f7b57f60d552a3). All the other files are just changing the `load` statements from `@rules_oci` to `//dev:oci_defs.bzl` ## Test plan CI, checked image locally and `sg test bazel-backend-integration` & `sg test bazel-e2e`
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 ];
# 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
USE_BAZEL_VERSION = if hostPlatform.isMacOS then "" else pkgs.bazel_7.version;
nix: mount /tmp/zig-cache in sandbox actions (#59992) As detailed in the shell hook: Without this, zig cc is forced to rebuild on every sandboxed GoLink action, which adds ~1m of time to GoLink actions. The reason it's on _every_ GoLink action is because sandboxes are ephemeral and don't persist non-mounted paths between actions. We're already doing this for `darwin-docker` config, so this only affects us nixos users --- Smuggling in a libtool fix for macos that would otherwise yield the following error when building rust with bazel: <details> <summary>Details</summary> ``` cargo:rerun-if-env-changed=AR_aarch64-apple-darwin AR_aarch64-apple-darwin = None cargo:rerun-if-env-changed=AR_aarch64_apple_darwin AR_aarch64_apple_darwin = None cargo:rerun-if-env-changed=HOST_AR HOST_AR = None cargo:rerun-if-env-changed=AR AR = Some("/nix/store/42yck6r7y2jhcrd0ay0glz30w6pw4wzh-libtool-2.4.7") cargo:rerun-if-env-changed=ARFLAGS_aarch64-apple-darwin ARFLAGS_aarch64-apple-darwin = None cargo:rerun-if-env-changed=ARFLAGS_aarch64_apple_darwin ARFLAGS_aarch64_apple_darwin = None cargo:rerun-if-env-changed=HOST_ARFLAGS HOST_ARFLAGS = None cargo:rerun-if-env-changed=ARFLAGS ARFLAGS = None running: ZERO_AR_DATE="1" "/nix/store/42yck6r7y2jhcrd0ay0glz30w6pw4wzh-libtool-2.4.7" "cq" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/libparser.a" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/src/parser.o" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/src/scanner.o" --stderr: error occurred: Command ZERO_AR_DATE="1" "/nix/store/42yck6r7y2jhcrd0ay0glz30w6pw4wzh-libtool-2.4.7" "cq" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/libparser.a" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/src/parser.o" "/private/var/tmp/_bazel_noah/dcf2fbfa8ce2981c9fc4201fa6327d3b/sandbox/darwin-sandbox/6091/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/external/crate_index__tree-sitter-kotlin-0.2.11/tree-sitter-kotlin_build_script.out_dir/src/scanner.o" with args "" failed to start: Os { code: 13, kind: PermissionDenied, message: "Permission denied" } ``` </details> ## Test plan Observing `bazel build //dev/sg` times
2024-01-31 00:47:55 +00:00
LIBTOOL = if hostPlatform.isMacOS then "/usr/bin/libtool" else "";
}