Skip to content

Commit

Permalink
Add NixOS support (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroeichler authored Oct 25, 2024
1 parent 6899467 commit b2386fd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ try-import %workspace%/lre.bazelrc
# Generated by the darwin flake module.
try-import %workspace%/darwin.bazelrc

# Generated by the NixOS flake module.
try-import %workspace%/nixos.bazelrc

# Generated by the nativelink flake module.
try-import %workspace%/nativelink.bazelrc

Expand Down
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use flake --impure
use flake . --impure
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ MODULE.bazel.lock
trivy-results.sarif
Pulumi.dev.yaml
lre.bazelrc
nixos.bazelrc
rust-project.json
darwin.bazelrc
nativelink.bazelrc
19 changes: 19 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
inputs.git-hooks.flakeModule
./local-remote-execution/flake-module.nix
./tools/darwin/flake-module.nix
./tools/nixos/flake-module.nix
./flake-module.nix
];
perSystem = {
Expand Down Expand Up @@ -463,6 +464,16 @@
else lre-cc.meta.Env;
prefix = "linux";
};
nixos.settings = {
path = with pkgs; [
"/run/current-system/sw/bin"
"${binutils.bintools}/bin"
"${uutils-coreutils-noprefix}/bin"
"${customClang}/bin"
"${git}/bin"
"${python3}/bin"
];
};
devShells.default = pkgs.mkShell {
nativeBuildInputs = let
bazel = pkgs.writeShellScriptBin "bazel" ''
Expand Down Expand Up @@ -531,6 +542,13 @@
# to NativeLink's read-only cache.
${config.nativelink.installationScript}
# If on NixOS, generate nixos.bazelrc which adds the required
# NixOS binary paths to the bazel environment.
if [ -e /etc/nixos ]; then
${config.nixos.installationScript}
export CC=customClang
fi
# The Bazel and Cargo builds in nix require a Clang toolchain.
# TODO(aaronmondal): The Bazel build currently uses the
# irreproducible host C++ toolchain. Provide
Expand All @@ -556,6 +574,7 @@
default = ./flake-module.nix;
darwin = ./tools/darwin/flake-module.nix;
local-remote-execution = ./local-remote-execution/flake-module.nix;
nixos = ./tools/nixos/flake-module.nix;
};
};
}
44 changes: 44 additions & 0 deletions tools/nixos/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
lib,
flake-parts-lib,
...
}: {
options = {
perSystem = flake-parts-lib.mkPerSystemOption (
{
config,
options,
pkgs,
...
}: let
cfg = config.nixos;
in {
options = {
nixos = {
pkgs = lib.mkOption {
type = lib.types.uniq (lib.types.lazyAttrsOf (lib.types.raw or lib.types.unspecified));
description = "Nixpkgs to use.";
default = pkgs;
defaultText = lib.literalMD "`pkgs` (module argument)";
};
settings = lib.mkOption {
type = lib.types.submoduleWith {
modules = [./modules/nixos.nix];
specialArgs = {inherit (cfg) pkgs;};
};
default = {};
description = "Configuration for Bazel on NixOS.";
};
installationScript = lib.mkOption {
type = lib.types.str;
description = "Create nixos.bazelrc.";
default = cfg.settings.installationScript;
defaultText = lib.literalMD "bazelrc content";
readOnly = true;
};
};
};
}
);
};
}
54 changes: 54 additions & 0 deletions tools/nixos/modules/nixos.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
config,
lib,
pkgs,
...
}: let
pathString = builtins.concatStringsSep ":" config.path;
bazelrc = pkgs.writeText "nixos.bazelrc" ''
build --action_env=PATH=${pathString}
build --host_action_env=PATH=${pathString}
'';
in {
options = {
installationScript = lib.mkOption {
type = lib.types.str;
description = "A bash snippet which creates a nixos.bazelrc file in the
repository.";
};
path = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "List of paths to include in the Bazel environment.";
};
};
config = {
installationScript = ''
if ! type -t git >/dev/null; then
# In pure shells
echo 1>&2 "WARNING: nixos: git command not found; skipping installation."
elif ! ${pkgs.git}/bin/git rev-parse --git-dir &> /dev/null; then
echo 1>&2 "WARNING: nixos: .git not found; skipping installation."
else
GIT_WC=`${pkgs.git}/bin/git rev-parse --show-toplevel`
# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like
# lorri and prevents installation loops by lorri.
if ! readlink "''${GIT_WC}/nixos.bazelrc" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/nixos.bazelrc") != ${bazelrc} ]]; then
echo 1>&2 "nixos: updating $PWD repository"
[ -L nixos.bazelrc ] && unlink nixos.bazelrc
if [ -e "''${GIT_WC}/nixos.bazelrc" ]; then
echo 1>&2 "nixos: WARNING: Refusing to install because of pre-existing nixos.bazelrc"
echo 1>&2 " Remove the nixos.bazelrc file and add nixos.bazelrc to .gitignore."
else
ln -fs ${bazelrc} "''${GIT_WC}/nixos.bazelrc"
fi
fi
fi
'';
};
}
9 changes: 9 additions & 0 deletions web/platform/src/content/docs/docs/contribute/nix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ To view the tag of an image
```sh
nix eval github:TraceMachina/nativelink#image.imageTag --raw
```

## On NixOS

If you're on NixOS, add the following to your system configuration:

```nix
programs.nix-ld.enable = true;
services.envfs.enable = true;
```

0 comments on commit b2386fd

Please sign in to comment.