Skip to content

Commit a744dd8

Browse files
manverujonringer1015bit
authored
Add Nix Flake (#936)
* flakes * add nomad entrypoint * add mantis-entrypoint to hydra jobs * remove rocksdb lock before start * entrypoint needs procps * another attempt to fix the broken shutdown issue * another attempt to make sure entrypoint is killable * shorten the time between config updates when mantis is unstable * Update mantis sha256 * Update mantis sha256 again * Remove old mantis packages Co-authored-by: Jonathan Ringer <[email protected]> Co-authored-by: Jonathan Ringer <[email protected]> Co-authored-by: Petra Bierleutgeb <[email protected]>
1 parent 4f5d57f commit a744dd8

File tree

8 files changed

+329
-96
lines changed

8 files changed

+329
-96
lines changed

flake.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
description = "Mantis flake";
3+
4+
inputs.flake-utils.url = "github:numtide/flake-utils";
5+
inputs.nixpkgs.url = "github:nixos/nixpkgs?rev=a98302aa9b9628915878a6ea9776c40a0bb02950";
6+
inputs.sbt-derivation.url = "github:zaninime/sbt-derivation";
7+
8+
outputs = { self, nixpkgs, flake-utils, sbt-derivation }: #, libsonic, libsonic-jnr }:
9+
let
10+
overlay = import ./nix/overlay.nix self.rev;
11+
pkgsForSystem = system: (import nixpkgs) { inherit system; overlays = [
12+
#libsonic.overlay
13+
#libsonic-jnr.overlay
14+
sbt-derivation.overlay
15+
overlay
16+
];};
17+
18+
mkHydraUtils = mkPkgs: let
19+
# nothing in lib should really depend on the system
20+
libPkgs = mkPkgs "x86_64-linux";
21+
# [attrset] -> attrset
22+
recursiveMerge = libPkgs.lib.foldr libPkgs.lib.recursiveUpdate {};
23+
mkHydraJobsForSystem = attrs: system:
24+
recursiveMerge (map (n: { "${n}"."${system}" = (mkPkgs system)."${n}"; }) attrs);
25+
in {
26+
collectHydraSets = jobSets: { hydraJobs = recursiveMerge jobSets; };
27+
mkHydraSet = attrs: systems: recursiveMerge (map (mkHydraJobsForSystem attrs) systems);
28+
};
29+
30+
hydraUtils = mkHydraUtils pkgsForSystem;
31+
inherit (hydraUtils) collectHydraSets mkHydraSet;
32+
33+
in flake-utils.lib.eachDefaultSystem (system: rec {
34+
pkgs = pkgsForSystem system;
35+
legacyPackages = pkgs;
36+
37+
defaultPackage = pkgs.mantis;
38+
devShell = pkgs.mkShell {
39+
nativeBuildInputs = with pkgs; [ solc sbt ];
40+
};
41+
apps.mantis = flake-utils.lib.mkApp { drv = pkgs.mantis; };
42+
defaultApp = apps.mantis;
43+
}) // ( collectHydraSets [
44+
(mkHydraSet [ "mantis" ] [ "x86_64-linux" "x86_64-darwin" ])
45+
(mkHydraSet [ "mantis-entrypoint" ] [ "x86_64-linux" ])
46+
]);
47+
}

nix/entrypoint.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
3+
set -exuo pipefail
4+
5+
cleanup() {
6+
# kill all processes whose parent is this process
7+
pkill -P $$
8+
}
9+
10+
for sig in INT QUIT HUP TERM; do
11+
trap "
12+
cleanup
13+
trap - $sig EXIT
14+
kill -s $sig "'"$$"' "$sig"
15+
done
16+
trap cleanup EXIT
17+
18+
19+
mkdir -p /tmp
20+
mkdir -p "$NOMAD_TASK_DIR/mantis"
21+
cd "$NOMAD_TASK_DIR"
22+
name="java"
23+
24+
if [ -n "${DAG_NAME:-}" ]; then
25+
if [ -f "ethash/$DAG_NAME" ]; then
26+
echo "found existing DAG"
27+
sha256sum "ethash/$DAG_NAME"
28+
else
29+
mkdir -p ethash
30+
aws \
31+
--endpoint-url "$MONITORING_ADDR" \
32+
s3 cp \
33+
"s3://$DAG_BUCKET/$DAG_NAME" \
34+
"ethash/$DAG_NAME" \
35+
|| echo "Unable to download DAG, skipping."
36+
fi
37+
fi
38+
39+
if [ -d "$STORAGE_DIR" ]; then
40+
echo "$STORAGE_DIR found, not restoring from backup..."
41+
else
42+
echo "$STORAGE_DIR not found, restoring backup..."
43+
restic restore latest \
44+
--tag "$NAMESPACE" \
45+
--target / \
46+
|| echo "couldn't restore backup, continue startup procedure..."
47+
mkdir -p "$NOMAD_TASK_DIR/mantis"
48+
rm -rf "$NOMAD_TASK_DIR/mantis/{keystore,node.key}"
49+
rm -rf "$NOMAD_TASK_DIR/mantis/logs"
50+
fi
51+
52+
until [ "$(grep -c enode mantis.conf)" -ge "$REQUIRED_PEER_COUNT" ]; do
53+
sleep 1
54+
done
55+
56+
ulimit -c unlimited
57+
cp mantis.conf running.conf
58+
59+
(
60+
while true; do
61+
set +x
62+
while diff -u running.conf mantis.conf > /dev/stderr; do
63+
sleep 900
64+
done
65+
set -x
66+
67+
if ! diff -u running.conf mantis.conf > /dev/stderr; then
68+
echo "Found updated config file, restarting Mantis"
69+
pkill "$name" || true
70+
fi
71+
done
72+
) &
73+
74+
starts=0
75+
while true; do
76+
starts="$((starts+1))"
77+
echo "Start Number $starts" > /dev/stderr
78+
cp mantis.conf running.conf
79+
cat running.conf > /dev/stderr
80+
rm -f "$NOMAD_TASK_DIR/mantis/rocksdb/LOCK"
81+
mantis "-Duser.home=$NOMAD_TASK_DIR" "$@" || true
82+
sleep 10
83+
done

nix/mantis.nix

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{ src
2+
, lib
3+
, coreutils
4+
, stdenv
5+
, nix-gitignore
6+
, solc
7+
, makeWrapper
8+
, runtimeShell
9+
, jre
10+
, sbt
11+
, gawk
12+
, gnused
13+
, protobuf
14+
, substituteAll
15+
, writeShellScriptBin
16+
}:
17+
18+
let
19+
version = let
20+
buildSbt = builtins.readFile ../build.sbt;
21+
captures = builtins.match ''.*version := "([^"]+)".*'' buildSbt;
22+
in builtins.elemAt captures 0;
23+
24+
PATH = lib.makeBinPath [ jre solc coreutils gawk gnused ];
25+
LD_LIBRARY_PATH = ''''; #lib.makeLibraryPath [ libsonic ];
26+
27+
# filter out mentions of protobridge, which is unable to execute
28+
protoc-wrapper = writeShellScriptBin "protoc" ''
29+
set -e
30+
31+
for f in "$@"; do
32+
echo ''${f##*=}
33+
done | grep protocbridge | xargs sed -i "1s|.*|#!${runtimeShell}|"
34+
35+
exec ${protobuf}/bin/protoc "$@"
36+
'';
37+
38+
39+
in sbt.mkDerivation rec {
40+
pname = "mantis";
41+
inherit src version;
42+
43+
nativeBuildInputs = [ solc protobuf makeWrapper ];
44+
45+
preConfigure = ''
46+
HOME=$TMPDIR
47+
PROTOC_CACHE=.nix/protoc-cache
48+
'';
49+
50+
# used by sbt-derivation to modify vendor derivation
51+
overrideDepsAttrs = oldAttrs: {
52+
inherit preConfigure;
53+
};
54+
PROTOCBRIDGE_SHELL = runtimeShell;
55+
56+
patches = [
57+
(substituteAll {
58+
src = ./protoc.patch;
59+
protobuf = protoc-wrapper;
60+
})
61+
];
62+
63+
#patchPhase = lib.optionalString (libsonic != null) ''
64+
# rm -rf src/main/resources
65+
# cp -r ${libsonic}/lib src/main/resources
66+
#'';
67+
68+
# This sha represents the change dependencies of mantis.
69+
# Update this sha whenever you change the dependencies
70+
depsSha256 = "0n7vv4k73cxjwg40qggr7gnkkg7vn8a179sf0wxnz3absj1700jj";
71+
72+
# this is the command used to to create the fixed-output-derivation
73+
depsWarmupCommand = "PROTOC_CACHE=.nix/protoc-cache; HOME=$TMPDIR; PATH=${PATH}:$PATH; sbt clean; sbt compile --debug";
74+
75+
installPhase = ''
76+
sbt stage
77+
mkdir -p $out/
78+
cp -r target/universal/stage/* $out/
79+
mkdir -p $out/share/mantis
80+
mv $out/{LICENSE,RELEASE,mantis_config.txt} $_
81+
82+
# wrap executable so that java is available at runtime
83+
for p in $(find $out/bin/* -executable); do
84+
wrapProgram "$p" \
85+
--prefix PATH : ${PATH} \
86+
${lib.optionalString (!stdenv.isDarwin)
87+
"--prefix LD_LIBRARY_PATH : ${LD_LIBRARY_PATH}"
88+
}
89+
done
90+
'';
91+
92+
93+
}

nix/overlay.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
rev: final: prev: {
2+
jre = prev.jdk8.jre;
3+
4+
mantis = final.callPackage ./mantis.nix {
5+
src = builtins.fetchGit {
6+
url = "https://github.com/input-output-hk/mantis";
7+
ref = "flake";
8+
rev = rev;
9+
submodules = true;
10+
};
11+
};
12+
13+
writeBashChecked = final.writers.makeScriptWriter {
14+
interpreter = "${final.bashInteractive}/bin/bash";
15+
check = final.writers.writeBash "shellcheck-check" ''
16+
${final.shellcheck}/bin/shellcheck -x "$1"
17+
'';
18+
};
19+
20+
writeBashBinChecked = name: final.writeBashChecked "/bin/${name}";
21+
22+
mantis-entrypoint-script = final.writeBashBinChecked "mantis-entrypoint" ''
23+
export PATH=${
24+
final.lib.makeBinPath
25+
(with final; [ coreutils restic gnugrep awscli diffutils mantis procps ])
26+
}
27+
28+
${builtins.readFile ./entrypoint.sh}
29+
'';
30+
31+
mantis-entrypoint = final.symlinkJoin {
32+
name = "mantis";
33+
paths = with final; [ mantis mantis-entrypoint-script ];
34+
};
35+
}

nix/pkgs/default.nix

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)