feat: nix flake (#17757)

Co-authored-by: rob <mdnlss@outlook.com>
Co-authored-by: mdnlss <rob73hall@gmail.com>
Co-authored-by: Alexey Shekhirin <5773434+shekhirin@users.noreply.github.com>
This commit is contained in:
Brian Picciano
2025-08-08 01:34:07 +02:00
committed by GitHub
parent 9862481f18
commit 82bbed9795
3 changed files with 247 additions and 0 deletions

4
.gitignore vendored
View File

@@ -68,3 +68,7 @@ links-report.json
__pycache__/
*.py[cod]
*$py.class
# direnv
.envrc
.direnv/

116
flake.lock generated Normal file
View File

@@ -0,0 +1,116 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1754269165,
"narHash": "sha256-0tcS8FHd4QjbCVoxN9jI+PjHgA4vc/IjkUSp+N3zy0U=",
"owner": "ipetkov",
"repo": "crane",
"rev": "444e81206df3f7d92780680e45858e31d2f07a08",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1754549159,
"narHash": "sha256-47e1Ar09kZlv2HvZilaNRFzRybIiJYNQ2MSvofbiw5o=",
"owner": "nix-community",
"repo": "fenix",
"rev": "5fe110751342a023d8c7ddce7fbf8311dca9f58d",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1731603435,
"narHash": "sha256-CqCX4JG7UiHvkrBTpYC3wcEurvbtTADLbo3Ns2CEoL8=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8b27c1239e5c421a2bbc2c65d52e4a6fbf2ff296",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "24.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"fenix": "fenix",
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1754496778,
"narHash": "sha256-fPDLP3z9XaYQBfSCemEdloEONz/uPyr35RHPRy9Vx8M=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "529d3b935d68bdf9120fe4d7f8eded7b56271697",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

127
flake.nix Normal file
View File

@@ -0,0 +1,127 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/24.11";
utils.url = "github:numtide/flake-utils";
crane.url = "github:ipetkov/crane";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
nixpkgs,
utils,
crane,
fenix,
...
}:
utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
# A useful helper for folding a list of `prevSet -> newSet` functions
# into an attribute set.
composeAttrOverrides = defaultAttrs: overrides: builtins.foldl'
(acc: f: acc // (f acc)) defaultAttrs overrides;
cargoTarget = pkgs.stdenv.hostPlatform.rust.rustcTargetSpec;
cargoTargetEnvVar = builtins.replaceStrings ["-"] ["_"]
(pkgs.lib.toUpper cargoTarget);
cargoTOML = builtins.fromTOML (builtins.readFile ./Cargo.toml);
packageVersion = cargoTOML.workspace.package.version;
rustVersion = cargoTOML.workspace.package."rust-version";
rustStable = fenix.packages.${system}.stable.withComponents [
"cargo" "rustc" "rust-src"
];
rustNightly = fenix.packages.${system}.latest;
craneLib = (crane.mkLib pkgs).overrideToolchain rustStable;
nativeBuildInputs = [
pkgs.pkg-config
pkgs.libgit2
pkgs.perl
];
withClang = prev: {
buildInputs = prev.buildInputs or [] ++ [
pkgs.clang
];
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
};
withMaxPerf = prev: {
cargoBuildCommand = "cargo build --profile=maxperf";
cargoExtraArgs = prev.cargoExtraArgs or "" + " --features=jemalloc,asm-keccak";
RUSTFLAGS = prev.RUSTFLAGS or [] ++ [
"-Ctarget-cpu=native"
];
};
withMold = prev: {
buildInputs = prev.buildInputs or [] ++ [
pkgs.mold
];
"CARGO_TARGET_${cargoTargetEnvVar}_LINKER" = "${pkgs.llvmPackages.clangUseLLVM}/bin/clang";
RUSTFLAGS = prev.RUSTFLAGS or [] ++ [
"-Clink-arg=-fuse-ld=${pkgs.mold}/bin/mold"
];
};
withOp = prev: {
cargoExtraArgs = prev.cargoExtraArgs or "" + " -p op-reth --bin=op-reth";
};
mkReth = overrides: craneLib.buildPackage (composeAttrOverrides {
pname = "reth";
version = packageVersion;
src = ./.;
inherit nativeBuildInputs;
doCheck = false;
} overrides);
in
{
packages = rec {
reth = mkReth ([
withClang
withMaxPerf
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
withMold
]);
op-reth = mkReth ([
withClang
withMaxPerf
withOp
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
withMold
]);
default = reth;
};
devShell = let
overrides = [
withClang
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
withMold
];
in craneLib.devShell (composeAttrOverrides {
packages = nativeBuildInputs ++ [
rustNightly.rust-analyzer
rustNightly.clippy
rustNightly.rustfmt
];
} overrides);
}
);
}