Skip to content

Commit

Permalink
add packages to Nix flake config, move to repo root
Browse files Browse the repository at this point in the history
This way we can actually build and run a node using just:
```sh
nix run 'github:status-im/nimbus-eth2?submodules=1'
```
The `?submodules=1` part should eventually not be necessary.
For more details see:
NixOS/nix#4423

Signed-off-by: Jakub Sokołowski <jakub@status.im>
  • Loading branch information
jakubgs committed Apr 18, 2024
1 parent 867995a commit eff9c52
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ jobs:
if: ${{ !cancelled() }} && github.event_name == 'pull_request'
run: |
excluded_files="config.yaml"
excluded_extensions="ans|cfg|json|json\\.template|md|png|service|ssz|txt"
excluded_extensions="ans|cfg|json|json\\.template|md|png|service|ssz|txt|lock|nix"
current_year=$(date +"%Y")
problematic_files=()
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ geth-*.zip
# generated during Nim compilation
*.nim.generated.nim

result
/dist
/benchmark_results
/.update.timestamp
Expand Down
85 changes: 85 additions & 0 deletions ci/Jenkinsfile.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* beacon_chain
* Copyright (c) 2019-2024 Status Research & Development GmbH
* Licensed and distributed under either of
* * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
* * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
* at your option. This file may not be copied, modified, or distributed except according to those terms.
*/
#!/usr/bin/env groovy
library 'status-jenkins-lib@nix/flake-build'

pipeline {
/* This way we run the same Jenkinsfile on different platforms. */
agent { label params.AGENT_LABEL }

parameters {
string(
name: 'AGENT_LABEL',
description: 'Label for targetted CI slave host: linux/macos',
defaultValue: params.AGENT_LABEL ?: getAgentLabel(),
)
choice(
name: 'VERBOSITY',
description: 'Value for the V make flag to increase log verbosity',
choices: [0, 1, 2]
)
}

options {
timestamps()
ansiColor('xterm')
/* This also includes wait time in the queue. */
timeout(time: 1, unit: 'HOURS')
/* Limit builds retained. */
buildDiscarder(logRotator(
numToKeepStr: '5',
daysToKeepStr: '30',
))
/* Abort old builds for non-main branches. */
disableConcurrentBuilds(
abortPrevious: !isMainBranch()
)
}

stages {
stage('Beacon Node') {
steps { script {
nix.flake('beacon_node')
} }
}

stage('Version check') {
steps { script {
sh 'result/bin/nimbus_beacon_node --version'
} }
}
}

post {
always {
cleanWs(
disableDeferredWipeout: true,
deleteDirs: true
)
}
}
}

def isMainBranch() {
return ['stable', 'testing', 'unstable'].contains(env.BRANCH_NAME)
}

/* This allows us to use one Jenkinsfile and run
* jobs on different platforms based on job name. */
def getAgentLabel() {
if (params.AGENT_LABEL) { return params.AGENT_LABEL }
/* We extract the name of the job from currentThread because
* before an agent is picket env is not available. */
def tokens = Thread.currentThread().getName().split('/')
def labels = []
/* Check if the job path contains any of the valid labels. */
['linux', 'macos', 'x86_64', 'aarch64', 'arm64'].each {
if (tokens.contains(it)) { labels.add(it) }
}
return labels.join(' && ')
}
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
description = "nimbus-eth2";

inputs.nixpkgs.url = github:NixOS/nixpkgs/master;

outputs = { self, nixpkgs }:
let
stableSystems = [
"x86_64-linux" "aarch64-linux" "armv7a-linux"
"x86_64-darwin" "aarch64-darwin"
"x86_64-windows"
];
forEach = nixpkgs.lib.genAttrs;
forAllSystems = forEach stableSystems;
pkgsFor = forEach stableSystems (
system: import nixpkgs { inherit system; }
);
in rec {
packages = forAllSystems (system: let
buildTarget = pkgsFor.${system}.callPackage ./nix/default.nix {
inherit stableSystems; src = self;
};
build = targets: buildTarget.override { inherit targets; };
in rec {
beacon_node = build ["nimbus_beacon_node"];
signing_node = build ["nimbus_signing_node"];
validator_client = build ["nimbus_validator_client"];
ncli = build ["ncli"];
ncli_db = build ["ncli_db"];

default = beacon_node;
});

devShells = forAllSystems (system: {
default = pkgsFor.${system}.callPackage ./nix/shell.nix { };
});
};
}
2 changes: 0 additions & 2 deletions installer/nix/.gitignore

This file was deleted.

61 changes: 0 additions & 61 deletions installer/nix/flake.lock

This file was deleted.

15 changes: 0 additions & 15 deletions installer/nix/flake.nix

This file was deleted.

29 changes: 29 additions & 0 deletions nix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Usage

## Shell

A development shell can be started using:
```sh
nix develop
```

## Building

To build a beacon node you can use:
```sh
nix build '.?submodules=1#beacon_node'
```
The `?submodules=1` part should eventually not be necessary.
For more details see:
https://github.com/NixOS/nix/issues/4423

It can be also done without even cloning the repo:
```sh
nix build 'github:status-im/nimbus-eth2?submodules=1'
```

## Running

```sh
nix run 'github:status-im/nimbus-eth2?submodules=1'
```
12 changes: 12 additions & 0 deletions nix/csources.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { } }:

let
tools = pkgs.callPackage ./tools.nix {};
sourceFile = ../vendor/nimbus-build-system/vendor/Nim/config/build_config.txt;
in pkgs.fetchFromGitHub {
owner = "nim-lang";
repo = "csources_v1";
rev = tools.findKeyValue "^nim_csourcesHash=([a-f0-9]+)$" sourceFile;
# WARNING: Requires manual updates when Nim compiler version changes.
hash = "sha256-gwBFuR7lzO4zttR/6rgdjXMRxVhwKeLqDwpmOwMyU7A=";
}
87 changes: 87 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
stdenv, darwin, lib, cmake, which, writeScriptBin, callPackage,
src ? ../.,
# Options: nimbus_light_client, nimbus_validator_client, nimbus_signing_node, all
targets ? ["nimbus_beacon_node"],
# Options: 0,1,2
verbosity ? 0,
# Perform 2-stage bootstrap instead of 3-stage to save time.
quickAndDirty ? true,
# These are the only platforms tested in CI and considered stable.
stableSystems ? [
"x86_64-linux" "aarch64-linux" "armv7a-linux"
"x86_64-darwin" "aarch64-darwin"
"x86_64-windows"
],
}:

let
nimble = callPackage ./nimble.nix {};
csources = callPackage ./csources.nix {};
revision = src.rev or "dirty";
in stdenv.mkDerivation rec {
pname = "nimbus-eth2";
version = "${callPackage ./version.nix {}}-${revision}";

inherit src;

# Fix for Nim compiler calling 'git rev-parse' and 'lsb_release'.
nativeBuildInputs = let
fakeGit = writeScriptBin "git" "echo ${version}";
fakeLsbRelease = writeScriptBin "lsb_release" "echo nix";
in
[ fakeGit fakeLsbRelease which cmake ]
++ lib.optionals stdenv.isDarwin [ darwin.cctools ];

enableParallelBuilding = true;

# Disable CPU optmizations that make binary not portable.
NIMFLAGS = "-d:disableMarchNative -d:git_revision_override=${revision}";
# Avoid Nim cache permission errors.
XDG_CACHE_HOME = "/tmp";

makeFlags = targets ++ [
"V=${builtins.toString verbosity}"
"QUICK_AND_DIRTY_COMPILER=${if quickAndDirty then "1" else "0"}"
"QUICK_AND_DIRTY_NIMBLE=${if quickAndDirty then "1" else "0"}"
];

# Generate the nimbus-build-system.paths file.
configurePhase = ''
patchShebangs scripts vendor/nimbus-build-system > /dev/null
make nimbus-build-system-paths
'';

# Avoid nimbus-build-system invoking `git clone` to build Nim.
preBuild = ''
pushd vendor/nimbus-build-system/vendor/Nim
mkdir dist
cp -r ${nimble} dist/nimble
cp -r ${csources} csources_v1
chmod 777 -R dist/nimble csources_v1
sed -i 's/isGitRepo(destDir)/false/' tools/deps.nim
popd
'';

installPhase = ''
mkdir -p $out/bin
rm -f build/generate_makefile
cp build/* $out/bin
'';

meta = with lib; {
homepage = "https://nimbus.guide/";
downloadPage = "https://github.com/status-im/nimbus-eth2/releases";
changelog = "https://github.com/status-im/nimbus-eth2/blob/stable/CHANGELOG.md";
description = "Nimbus is a lightweight client for the Ethereum consensus layer";
longDescription = ''
Nimbus is an extremely efficient consensus layer client implementation.
While it's optimised for embedded systems and resource-restricted devices --
including Raspberry Pis, its low resource usage also makes it an excellent choice
for any server or desktop (where it simply takes up fewer resources).
'';
license = with licenses; [asl20 mit];
mainProgram = "nimbus_beacon_node";
platforms = stableSystems;
};
}
12 changes: 12 additions & 0 deletions nix/nimble.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { } }:

let
tools = pkgs.callPackage ./tools.nix {};
sourceFile = ../vendor/nimbus-build-system/vendor/Nim/koch.nim;
in pkgs.fetchFromGitHub {
owner = "nim-lang";
repo = "nimble";
rev = tools.findKeyValue "^ +NimbleStableCommit = \"([a-f0-9]+)\".+" sourceFile;
# WARNING: Requires manual updates when Nim compiler version changes.
hash = "sha256-qJcDKnc+9iUvYrZCMUbBbws+Qqa9vmWyCRsvOUEmq8U=";
}
Loading

0 comments on commit eff9c52

Please sign in to comment.