Skip to content

Commit

Permalink
anchor-client with new()
Browse files Browse the repository at this point in the history
This commit introduces an anchor-bridge-client crate with a partially
working new() call. Still needs proper handling of seeds for some of
the accounts.

Change-Id: I73bfc4b7e23c45643776ba8f13946f1f8c7c65ed
  • Loading branch information
Stan Drozd committed May 18, 2021
1 parent 360d0ad commit e0b193b
Show file tree
Hide file tree
Showing 12 changed files with 4,114 additions and 443 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval "$(lorri direnv)"
10 changes: 10 additions & 0 deletions nix/rust.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ sources ? import ./sources.nix }:

let
pkgs =
import sources.nixpkgs {overlays = [ (import sources.nixpkgs-mozilla) ]; };
channel = "nightly";
date = "2020-11-19";
targets = [];
chan = pkgs.rustChannelOfTargets channel date targets;
in chan
62 changes: 62 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"cargo2nix": {
"branch": "master",
"description": "Build Rust crates with Nix",
"homepage": "",
"owner": "cargo2nix",
"repo": "cargo2nix",
"rev": "c491edfb1b2721c8202afe4051ce7b44adfb668d",
"sha256": "0aby21f72314qfmkqcdv7wpw6cywn7isr7gsh8gz992cn2szjpg4",
"type": "tarball",
"url": "https://github.com/cargo2nix/cargo2nix/archive/c491edfb1b2721c8202afe4051ce7b44adfb668d.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "af958e8057f345ee1aca714c1247ef3ba1c15f5e",
"sha256": "1qjavxabbrsh73yck5dcq8jggvh3r2jkbr6b5nlz5d9yrqm9255n",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/af958e8057f345ee1aca714c1247ef3ba1c15f5e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "release-20.09",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a1643da0480d7716e76c79654b2591dfacbf2931",
"sha256": "1bvivv5dj6qvc5kyf2f0s84s5hqh8ry0i5ly9c9qsw4s9svl9yb1",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/a1643da0480d7716e76c79654b2591dfacbf2931.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-mozilla": {
"branch": "master",
"description": "mozilla related nixpkgs (extends nixos/nixpkgs repo)",
"homepage": "",
"owner": "mozilla",
"repo": "nixpkgs-mozilla",
"rev": "8c007b60731c07dd7a052cce508de3bb1ae849b4",
"sha256": "1zybp62zz0h077zm2zmqs2wcg3whg6jqaah9hcl1gv4x8af4zhs6",
"type": "tarball",
"url": "https://github.com/mozilla/nixpkgs-mozilla/archive/8c007b60731c07dd7a052cce508de3bb1ae849b4.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"rust-overlay": {
"branch": "master",
"description": "Pure and reproducible nix overlay for binary distributed rust toolchains",
"homepage": "",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "a3873f83ed5fe58c4c6af09700d27b54aa19b986",
"sha256": "07hll4yalziyzd68gg518kmxs6y31bb2y3ilp1hw4lpbj1jyj4k7",
"type": "tarball",
"url": "https://github.com/oxalica/rust-overlay/archive/a3873f83ed5fe58c4c6af09700d27b54aa19b986.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
174 changes: 174 additions & 0 deletions nix/sources.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# This file has been generated by Niv.

let

#
# The fetchers. fetch_<type> fetches specs of type <type>.
#

fetch_file = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };

fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };

fetch_git = name: spec:
let
ref =
if spec ? ref then spec.ref else
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
in
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };

fetch_local = spec: spec.path;

fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';

fetch_builtin-url = name: throw
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=file -a builtin=true'';

#
# Various helpers
#

# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
sanitizeName = name:
(
concatMapStrings (s: if builtins.isList s then "-" else s)
(
builtins.split "[^[:alnum:]+._?=-]+"
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
)
);

# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> {}
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';

# The actual fetching function.
fetch = pkgs: name: spec:

if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";

# If the environment variable NIV_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
replace = name: drv:
let
saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";

# Ports of functions for older nix versions

# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);

# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);

# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));

# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";

# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else {};

# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchTarball attrs;

# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchurl attrs;

# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
) config.sources;

# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
, sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;

# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
inherit pkgs;
};

in
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }
17 changes: 17 additions & 0 deletions nix/xargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ rust ? import ./rust.nix }: (
self: super: {
xargo = self.rustPlatform.buildRustPackage rec {
pname = "xargo";
src = self.fetchFromGitHub {
owner = "japaric";
repo = pname;
};
cargoSha256 = "unset";
meta = with self.lib; {
description = "The rust cross-compilation tool";
homepage = "https://github.com/japaric/xargo";
maintainers = maintainers.tailhook;
};
};
}
)
100 changes: 100 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{ sources ? import nix/sources.nix # Please specify inputs obtained from `sources` as separate params like below
, cargo2nix ? sources.cargo2nix
, nixpkgs ? sources.nixpkgs
, rust-olay ? import sources.rust-overlay
}:
let
tilt-olay = self: super: {
tilt184 = self.tilt.overrideAttrs (
oldAttrs: {
version = "0.18.4";
src = self.fetchFromGitHub {
owner = "tilt-dev";
repo = oldAttrs.pname;
rev = "v0.18.4";
sha256 = "sha256-xqBgbsrVSAOqtfHbEF07i6XIdiBXMYoR7H4Kc4xK7x0=";
};
buildFlagsArray = [ "-ldflags=-X main.version=0.18.4" ];
}
);
};
scripts-olay = self: super: {
whcluster = self.writeShellScriptBin "whcluster" ''
set -e
default_minikube_args="--cpus=10 --memory=10gb --disk-size=200gb"
export MINIKUBE_ARGS=''${MINIKUBE_ARGS:-$default_minikube_args}
${self.minikube}/bin/minikube start $MINIKUBE_ARGS
${self.minikube}/bin/minikube ssh 'echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p'
${self.whkube}/bin/whkube
'';
whkube = self.writeShellScriptBin "whkube" ''${self.kubectl}/bin/kubectl config set-context --current --namespace=wormhole'';
whtilt = self.writeShellScriptBin "whtilt" ''
echo "Starting Tilt with ''${1:=5} guardians"
${self.tilt184}/bin/tilt up --update-mode exec -- --num=$1
'';
whinotify = self.writeShellScriptBin "whinotify" ''
${self.minikube}/bin/minikube ssh 'echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p'
'';
# stan-work config
whsw = self.writeShellScriptBin "whsw" ''
${pkgs.mutagen}/bin/mutagen sync terminate whsw-sync || true
${pkgs.mutagen}/bin/mutagen sync create -n whsw-sync . stan-work:~/wormhole
${pkgs.mutagen}/bin/mutagen sync flush whsw-sync
export MINIKUBE_ARGS='--cpus=30 --memory=110g --disk-size=1000gb'
ssh stan-work \
". ~/.zprofile && \
cd wormhole && \
nix-shell --command ' MINIKUBE_ARGS=\"$MINIKUBE_ARGS\" whcluster && \
killall tilt || true'"
ssh -L 10350:127.0.0.1:10350 stan-work \
"cd wormhole && \
source ~/.zprofile && \
nix-shell shell.nix --command 'whtilt $WH_GUARDIAN_COUNT'"
'';
};
cargo2nix-olay = import "${cargo2nix}/overlay";
pkgs = import nixpkgs {
overlays = [
# cargo2nix-olay
rust-olay
tilt-olay
scripts-olay
];
};
cargo2nix-drv = import cargo2nix {
inherit nixpkgs;
};
in
pkgs.mkShell {
nativeBuildInputs = (
with pkgs; [
go
gopls
hidapi
libudev
openssl
pkgconfig
protobuf
whcluster
whinotify
whkube
whtilt
whsw
# (
# rust-bin.stable."1.51.0".default.override {
# extensions = [
# "rust-src"
# "rust-analysis"
# ];
# }
# )
# Provided on Fedora:
kubectl
minikube
tilt184
# xargo
]
);
DOCKER_BUILDKIT = 1;
PROTOC = "${pkgs.protobuf}/bin/protoc";
}
Loading

0 comments on commit e0b193b

Please sign in to comment.