Skip to content

Commit

Permalink
Add Nix formatters and linters to pre-commit hooks (#561)
Browse files Browse the repository at this point in the history
Alejandra formats, statix lints antipatterns, deadnix scans dead code.
  • Loading branch information
aaronmondal committed Dec 20, 2023
1 parent 6c555bb commit d823964
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 244 deletions.
215 changes: 114 additions & 101 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,114 +13,126 @@
};
};

outputs = inputs @ { self, flake-parts, crane, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
outputs = inputs @ {
self,
flake-parts,
crane,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
imports = [ inputs.pre-commit-hooks.flakeModule ];
perSystem = { config, pkgs, system, ... }:
let
isDarwin = builtins.elem system [
"x86_64-darwin"
"aarch64-darwin"
];

maybeDarwinDeps = pkgs.lib.optionals isDarwin [
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.libiconv
];

customStdenv = import ./tools/llvmStdenv.nix { inherit pkgs isDarwin; };

# TODO(aaronmondal): This doesn't work with rules_rust yet.
# Tracked in https://github.com/TraceMachina/nativelink/issues/477.
customClang = pkgs.callPackage ./tools/customClang.nix {
inherit pkgs;
stdenv = customStdenv;
};
imports = [inputs.pre-commit-hooks.flakeModule];
perSystem = {
config,
pkgs,
system,
...
}: let
isDarwin = builtins.elem system [
"x86_64-darwin"
"aarch64-darwin"
];

maybeDarwinDeps = pkgs.lib.optionals isDarwin [
pkgs.darwin.apple_sdk.frameworks.Security
pkgs.libiconv
];

customStdenv = import ./tools/llvmStdenv.nix {inherit pkgs isDarwin;};

# TODO(aaronmondal): This doesn't work with rules_rust yet.
# Tracked in https://github.com/TraceMachina/nativelink/issues/477.
customClang = pkgs.callPackage ./tools/customClang.nix {
inherit pkgs;
stdenv = customStdenv;
};

craneLib = crane.lib.${system};
craneLib = crane.lib.${system};

src = pkgs.lib.cleanSourceWith {
src = craneLib.path ./.;
filter = path: type:
(builtins.match "^.+/data/SekienAkashita\\.jpg" path != null) ||
(craneLib.filterCargoSources path type);
};
src = pkgs.lib.cleanSourceWith {
src = craneLib.path ./.;
filter = path: type:
(builtins.match "^.+/data/SekienAkashita\\.jpg" path != null)
|| (craneLib.filterCargoSources path type);
};

commonArgs = {
inherit src;
strictDeps = true;
buildInputs = maybeDarwinDeps;
nativeBuildInputs = [
commonArgs = {
inherit src;
strictDeps = true;
buildInputs = maybeDarwinDeps;
nativeBuildInputs =
[
pkgs.cacert
] ++ maybeDarwinDeps ++ pkgs.lib.optionals (!isDarwin) [ pkgs.autoPatchelfHook ];
stdenv = customStdenv;
};
]
++ maybeDarwinDeps
++ pkgs.lib.optionals (!isDarwin) [pkgs.autoPatchelfHook];
stdenv = customStdenv;
};

# Additional target for external dependencies to simplify caching.
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Additional target for external dependencies to simplify caching.
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

nativelink = craneLib.buildPackage (commonArgs
// {
nativelink = craneLib.buildPackage (commonArgs
// {
inherit cargoArtifacts;
});

hooks = import ./tools/pre-commit-hooks.nix { inherit pkgs; };
hooks = import ./tools/pre-commit-hooks.nix {inherit pkgs;};

publish-ghcr = import ./tools/publish-ghcr.nix { inherit pkgs; };
publish-ghcr = import ./tools/publish-ghcr.nix {inherit pkgs;};

local-image-test = import ./tools/local-image-test.nix { inherit pkgs; };
local-image-test = import ./tools/local-image-test.nix {inherit pkgs;};

generate-toolchains = import ./tools/generate-toolchains.nix { inherit pkgs; };
in
{
apps = {
default = {
type = "app";
program = "${nativelink}/bin/cas";
};
generate-toolchains = import ./tools/generate-toolchains.nix {inherit pkgs;};
in {
apps = {
default = {
type = "app";
program = "${nativelink}/bin/cas";
};
packages = {
inherit publish-ghcr local-image-test;
default = nativelink;
lre = import ./local-remote-execution/image.nix { inherit pkgs nativelink; };
image = pkgs.dockerTools.streamLayeredImage {
name = "nativelink";
contents = [
nativelink
pkgs.dockerTools.caCertificates
];
config = {
Entrypoint = [ "/bin/cas" ];
Labels = {
"org.opencontainers.image.description" = "An RBE compatible, high-performance cache and remote executor.";
"org.opencontainers.image.documentation" = "https://github.com/TraceMachina/nativelink";
"org.opencontainers.image.licenses" = "Apache-2.0";
"org.opencontainers.image.revision" = "${self.rev or self.dirtyRev or "dirty"}";
"org.opencontainers.image.source" = "https://github.com/TraceMachina/nativelink";
"org.opencontainers.image.title" = "Native Link";
"org.opencontainers.image.vendor" = "Trace Machina, Inc.";
};
};
packages = {
inherit publish-ghcr local-image-test;
default = nativelink;
lre = import ./local-remote-execution/image.nix {inherit pkgs nativelink;};
image = pkgs.dockerTools.streamLayeredImage {
name = "nativelink";
contents = [
nativelink
pkgs.dockerTools.caCertificates
];
config = {
Entrypoint = ["/bin/cas"];
Labels = {
"org.opencontainers.image.description" = "An RBE compatible, high-performance cache and remote executor.";
"org.opencontainers.image.documentation" = "https://github.com/TraceMachina/nativelink";
"org.opencontainers.image.licenses" = "Apache-2.0";
"org.opencontainers.image.revision" = "${self.rev or self.dirtyRev or "dirty"}";
"org.opencontainers.image.source" = "https://github.com/TraceMachina/nativelink";
"org.opencontainers.image.title" = "Native Link";
"org.opencontainers.image.vendor" = "Trace Machina, Inc.";
};
};
};
checks = {
# TODO(aaronmondal): Fix the tests.
# tests = craneLib.cargoNextest (commonArgs
# // {
# inherit cargoArtifacts;
# cargoNextestExtraArgs = "--all";
# partitions = 1;
# partitionType = "count";
# });
};
pre-commit.settings = { inherit hooks; };
devShells.default = pkgs.mkShell {
nativeBuildInputs = [
};
checks = {
# TODO(aaronmondal): Fix the tests.
# tests = craneLib.cargoNextest (commonArgs
# // {
# inherit cargoArtifacts;
# cargoNextestExtraArgs = "--all";
# partitions = 1;
# partitionType = "count";
# });
};
pre-commit.settings = {inherit hooks;};
devShells.default = pkgs.mkShell {
nativeBuildInputs =
[
# Development tooling goes here.
pkgs.cargo
pkgs.rustc
Expand All @@ -138,20 +150,21 @@
local-image-test
generate-toolchains
customClang
] ++ maybeDarwinDeps;
shellHook = ''
# Generate the .pre-commit-config.yaml symlink when entering the
# development shell.
${config.pre-commit.installationScript}
# The Bazel and Cargo builds in nix require a Clang toolchain.
# TODO(aaronmondal): The Bazel build currently uses the
# irreproducible host C++ toolchain. Provide
# this toolchain via nix for bitwise identical
# binaries across machines.
export CC=clang
'';
};
]
++ maybeDarwinDeps;
shellHook = ''
# Generate the .pre-commit-config.yaml symlink when entering the
# development shell.
${config.pre-commit.installationScript}
# The Bazel and Cargo builds in nix require a Clang toolchain.
# TODO(aaronmondal): The Bazel build currently uses the
# irreproducible host C++ toolchain. Provide
# this toolchain via nix for bitwise identical
# binaries across machines.
export CC=clang
'';
};
};
};
}
58 changes: 30 additions & 28 deletions local-remote-execution/image.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{ pkgs, nativelink, ... }:

let
customStdenv = import ../tools/llvmStdenv.nix { inherit pkgs; };
{
pkgs,
nativelink,
...
}: let
customStdenv = import ../tools/llvmStdenv.nix {inherit pkgs;};
customClang = pkgs.callPackage ../tools/customClang.nix {
inherit pkgs;
stdenv = customStdenv;
Expand Down Expand Up @@ -49,14 +51,15 @@ let
# Add all tooling here so that the generated toolchains use `/nix/store/*`
# paths instead of `/bin` or `/usr/bin`. This way we're guaranteed to use
# binary identical toolchains during local and remote execution.
("PATH=" + (pkgs.lib.strings.concatStringsSep ":" [
"${customStdenv.cc.bintools}/bin"
"${customClang}/bin"
"${customStdenv}/bin"
"${pkgs.coreutils}/bin"
"${pkgs.findutils}/bin"
"${pkgs.gnutar}/bin"
]))
("PATH="
+ (pkgs.lib.strings.concatStringsSep ":" [
"${customStdenv.cc.bintools}/bin"
"${customClang}/bin"
"${customStdenv}/bin"
"${pkgs.coreutils}/bin"
"${pkgs.findutils}/bin"
"${pkgs.gnutar}/bin"
]))
"JAVA_HOME=${pkgs.jdk11_headless}/lib/openjdk"

"CC=${customClang}/bin/customClang"
Expand All @@ -74,11 +77,12 @@ let
"-L${pkgs.llvmPackages_17.libcxxabi}/lib"
"-L${pkgs.llvmPackages_17.libunwind}/lib"
"-lc++"
("-Wl," +
"-rpath,${pkgs.llvmPackages_17.libcxx}/lib," +
"-rpath,${pkgs.llvmPackages_17.libcxxabi}/lib," +
"-rpath,${pkgs.llvmPackages_17.libunwind}/lib," +
"-rpath,${pkgs.glibc}/lib"
(
"-Wl,"
+ "-rpath,${pkgs.llvmPackages_17.libcxx}/lib,"
+ "-rpath,${pkgs.llvmPackages_17.libcxxabi}/lib,"
+ "-rpath,${pkgs.llvmPackages_17.libunwind}/lib,"
+ "-rpath,${pkgs.glibc}/lib"
)
]}"
];
Expand All @@ -91,18 +95,16 @@ let

contents = autogenDeps;
};

in
pkgs.dockerTools.streamLayeredImage {
name = "nativelink-toolchain";

pkgs.dockerTools.streamLayeredImage {
name = "nativelink-toolchain";
# Override the toolchain container tag with the one from the autogen
# container. This way the nativelink doesn't influence this tag and and
# changes to its codebase don't invalidate existing toolchain containers.
tag = autogenContainer.imageTag;

# Override the toolchain container tag with the one from the autogen
# container. This way the nativelink doesn't influence this tag and and
# changes to its codebase don't invalidate existing toolchain containers.
tag = autogenContainer.imageTag;

inherit extraCommands config;
inherit extraCommands config;

contents = autogenDeps ++ [ nativelink ];
}
contents = autogenDeps ++ [nativelink];
}
5 changes: 2 additions & 3 deletions local-remote-execution/rbe-configs-gen.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{ pkgs, ... }:

{pkgs, ...}:
pkgs.buildGoModule rec {
pname = "bazel-toolchains";
version = "5.1.2";
Expand All @@ -21,6 +20,6 @@ pkgs.buildGoModule rec {
description = "Generate Bazel toolchain configs for remote execution.";
homepage = "https://github.com/bazelbuild/bazel-toolchains";
license = licenses.asl20;
maintainers = [ maintainers.aaronmondal ];
maintainers = [maintainers.aaronmondal];
};
}
6 changes: 4 additions & 2 deletions tools/customClang.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{ pkgs, stdenv }:

{
pkgs,
stdenv,
}:
# Bazel expects a single frontend for both C and C++. That works for GCC but
# not for clang. This wrapper selects `clang` or `clang++` depending on file
# ending.
Expand Down

0 comments on commit d823964

Please sign in to comment.