|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | + |
| 8 | +let |
| 9 | + cfg = config.programs.bazelisk; |
| 10 | + |
| 11 | + # Build a PATH containing common tools that Bazel actions need. |
| 12 | + defaultActionPath = lib.makeBinPath ( |
| 13 | + with pkgs; |
| 14 | + [ |
| 15 | + bash |
| 16 | + binutils |
| 17 | + coreutils |
| 18 | + findutils |
| 19 | + gawk |
| 20 | + gnugrep |
| 21 | + gnused |
| 22 | + gnutar |
| 23 | + gzip |
| 24 | + which |
| 25 | + unzip |
| 26 | + ] |
| 27 | + ++ cfg.extraPackages |
| 28 | + ); |
| 29 | + |
| 30 | + bazelrc = pkgs.writeText "bazel.bazelrc" '' |
| 31 | + # Generated by NixOS programs.bazelisk module. |
| 32 | + # NixOS needs an explicit PATH for Bazel actions since tools live in the |
| 33 | + # Nix store and are not at standard FHS locations. |
| 34 | + build --action_env=PATH=${defaultActionPath} |
| 35 | + build --host_action_env=PATH=${defaultActionPath} |
| 36 | +
|
| 37 | + ${lib.optionalString (cfg.cc != null) '' |
| 38 | + build --repo_env=CC=${cfg.cc} |
| 39 | + build --action_env=CC=${cfg.cc} |
| 40 | + ''} |
| 41 | + ${lib.optionalString (cfg.cxx != null) '' |
| 42 | + build --repo_env=CXX=${cfg.cxx} |
| 43 | + build --action_env=CXX=${cfg.cxx} |
| 44 | + ''} |
| 45 | + ${cfg.extraBazelrc} |
| 46 | + ''; |
| 47 | +in |
| 48 | +{ |
| 49 | + options.programs.bazelisk = { |
| 50 | + enable = lib.mkEnableOption '' |
| 51 | + bazelisk, a version-switching launcher for Bazel. |
| 52 | +
|
| 53 | + Enabling this module installs bazelisk with a `bazel` symlink, enables |
| 54 | + envfs (so unpatched Bazel binaries can find `/bin/bash`), and writes a |
| 55 | + system-wide `/etc/bazel.bazelrc` that sets `--action_env=PATH` and |
| 56 | + `--host_action_env=PATH` to Nix store paths for common tools. |
| 57 | +
|
| 58 | + Per-project settings (e.g. extra rustc flags, pkg-config paths) still |
| 59 | + belong in `.bazelrc.user` |
| 60 | + ''; |
| 61 | + |
| 62 | + package = lib.mkPackageOption pkgs "bazelisk" { }; |
| 63 | + |
| 64 | + cc = lib.mkOption { |
| 65 | + type = lib.types.nullOr lib.types.str; |
| 66 | + default = null; |
| 67 | + example = lib.literalExpression ''"''${pkgs.clang}/bin/clang"''; |
| 68 | + description = '' |
| 69 | + Path to the C compiler. Set via `--repo_env=CC` and `--action_env=CC` |
| 70 | + in the system bazelrc so that rules_cc toolchain detection works. |
| 71 | + ''; |
| 72 | + }; |
| 73 | + |
| 74 | + cxx = lib.mkOption { |
| 75 | + type = lib.types.nullOr lib.types.str; |
| 76 | + default = null; |
| 77 | + example = lib.literalExpression ''"''${pkgs.clang}/bin/clang++"''; |
| 78 | + description = '' |
| 79 | + Path to the C++ compiler. Set via `--repo_env=CXX` and `--action_env=CXX` |
| 80 | + in the system bazelrc so that rules_cc toolchain detection works. |
| 81 | + ''; |
| 82 | + }; |
| 83 | + |
| 84 | + extraPackages = lib.mkOption { |
| 85 | + type = lib.types.listOf lib.types.package; |
| 86 | + default = [ ]; |
| 87 | + description = '' |
| 88 | + Additional packages to include in the Bazel action PATH. |
| 89 | + ''; |
| 90 | + }; |
| 91 | + |
| 92 | + extraBazelrc = lib.mkOption { |
| 93 | + type = lib.types.lines; |
| 94 | + default = ""; |
| 95 | + description = '' |
| 96 | + Extra lines appended to the system `/etc/bazel.bazelrc`. |
| 97 | + ''; |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
| 101 | + config = lib.mkIf cfg.enable { |
| 102 | + # Install bazelisk with a `bazel` symlink so it's a drop-in replacement. |
| 103 | + environment.systemPackages = [ |
| 104 | + (pkgs.symlinkJoin { |
| 105 | + name = "bazelisk-with-bazel"; |
| 106 | + paths = [ cfg.package ]; |
| 107 | + postBuild = "ln -sf $out/bin/bazelisk $out/bin/bazel"; |
| 108 | + }) |
| 109 | + ]; |
| 110 | + |
| 111 | + # envfs provides /bin/bash and /usr/bin/env for unpatched Bazel binaries. |
| 112 | + services.envfs.enable = true; |
| 113 | + services.envfs.extraFallbackPathCommands = '' |
| 114 | + ln -s ${pkgs.bash}/bin/bash $out/bash |
| 115 | + ''; |
| 116 | + |
| 117 | + # System-wide bazelrc with NixOS-compatible defaults. |
| 118 | + environment.etc."bazel.bazelrc".source = bazelrc; |
| 119 | + }; |
| 120 | + |
| 121 | + meta = { |
| 122 | + doc = ./bazelisk.md; |
| 123 | + maintainers = with lib.maintainers; [ dhashs ]; |
| 124 | + }; |
| 125 | +} |
0 commit comments