Skip to content

Commit

Permalink
Add NixOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroeichler committed Aug 30, 2024
1 parent 689d099 commit 742d7b0
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,8 @@ build --extra_toolchains=@rust_toolchains//:all
# Generated by the LRE flake module.
try-import %workspace%/lre.bazelrc

# Generated the Nix environment if on NixOS.
try-import %workspace%/nixos.bazelrc

# Allow user-side customization.
try-import %workspace%/user.bazelrc
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 @@ -17,4 +17,5 @@ MODULE.bazel.lock
trivy-results.sarif
Pulumi.dev.yaml
lre.bazelrc
nixos.bazelrc
rust-project.json
9 changes: 9 additions & 0 deletions docs/src/content/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;
```
21 changes: 20 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
imports = [
inputs.git-hooks.flakeModule
./local-remote-execution/flake-module.nix
./nixos/flake-module.nix
];
perSystem = {
config,
Expand Down Expand Up @@ -410,6 +411,16 @@
else lre-cc.meta.Env;
prefix = "lre";
};
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 @@ -468,6 +479,13 @@
# this toolchain via nix for bitwise identical
# binaries across machines.
export CC=clang
# 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
''
+ pkgs.lib.optionalString (!pkgs.stdenv.isDarwin) ''
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
Expand All @@ -477,6 +495,7 @@
};
}
// {
flakeModule = ./local-remote-execution/flake-module.nix;
flakeModules.default = ./local-remote-execution/flake-module.nix;
flakeModules.nixos = ./nixos/flake-module.nix;
};
}
44 changes: 44 additions & 0 deletions 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 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
'';
};
}

0 comments on commit 742d7b0

Please sign in to comment.