Skip to content

Commit 9c6947a

Browse files
committed
feat(nix): Add Solana nix package
1 parent 3cf4f6c commit 9c6947a

File tree

8 files changed

+204
-9
lines changed

8 files changed

+204
-9
lines changed

.envrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ fi
66

77
dotenv_if_exists
88
nix_direnv_watch_file shell.nix
9+
nix_direnv_watch_file flake.nix
10+
nix_direnv_watch_file overlay.nix
911
use flake

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}:
1414
flake-utils.lib.simpleFlake {
1515
inherit self nixpkgs;
16-
name = "nix-blockchain-development";
16+
name = "meta-craft-labs";
1717
shell = ./shell.nix;
18+
overlay = ./overlay.nix;
1819
};
1920
}

overlay.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
finalNixpkgs: prevNixpkgs: let
2+
solana-artefacts = prevNixpkgs.solana-testnet.overrideAttrs (finalSolanaPkg: prevSolanaPkg: {
3+
pname = "solana-artefacts";
4+
buildAndTestSubdir = null;
5+
nativeBuildInputs = prevSolanaPkg.nativeBuildInputs ++ [finalNixpkgs.llvmPackages_13.clang];
6+
buildInputs = prevSolanaPkg.buildInputs ++ [finalNixpkgs.openssl];
7+
LIBCLANG_PATH = "${finalNixpkgs.llvmPackages_13.libclang.lib}/lib";
8+
CARGO_FEATURE_VENDORED = "0";
9+
OPENSSL_NO_VENDOR = "1";
10+
postInstall = ''
11+
mkdir -p $out/bin/sdk/
12+
cp -r ./sdk/bpf $out/bin/sdk/
13+
'';
14+
patches = prevSolanaPkg.patches ++ [./packages/cargo-build-bpf/patches/main.rs.diff];
15+
});
16+
17+
solana-bpf-tools = prevNixpkgs.callPackage ./packages/solana-bpf-tools {};
18+
in {
19+
metacraft-labs = {
20+
solana = prevNixpkgs.stdenv.mkDerivation rec {
21+
name = "solana-${version}";
22+
version = "1.23.1";
23+
24+
phases = ["installPhase"];
25+
26+
installPhase = ''
27+
mkdir -p $out
28+
cp -rf ${solana-artefacts}/* $out
29+
chmod 0755 -R $out;
30+
31+
mkdir -p $out/bin/sdk/bpf
32+
cp -rf ${solana-bpf-tools}/* $out/bin/sdk/bpf/
33+
chmod 0755 -R $out;
34+
'';
35+
36+
meta = with prevNixpkgs.lib; {
37+
homepage = "https://github.com/solana-labs/solana";
38+
platforms = platforms.linux;
39+
};
40+
};
41+
};
42+
}

packages/cargo-build-bpf/patches/Cargo.lock.diff

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/sdk/cargo-build-bpf/Cargo.toml b/sdk/cargo-build-bpf/Cargo.toml
2+
index 888b59a101..86968aef4f 100644
3+
--- a/sdk/cargo-build-bpf/Cargo.toml
4+
+++ b/sdk/cargo-build-bpf/Cargo.toml
5+
@@ -14,7 +14,6 @@ bzip2 = "0.4.3"
6+
cargo_metadata = "0.14.2"
7+
clap = { version = "3.1.5", features = ["cargo", "env"] }
8+
regex = "1.5.4"
9+
-solana-download-utils = { path = "../../download-utils", version = "=1.10.11" }
10+
solana-sdk = { path = "..", version = "=1.10.11" }
11+
tar = "0.4.38"
12+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
diff --git a/sdk/cargo-build-bpf/src/main.rs b/sdk/cargo-build-bpf/src/main.rs
2+
index a602f6715f..a2451f3ce1 100644
3+
--- a/sdk/cargo-build-bpf/src/main.rs
4+
+++ b/sdk/cargo-build-bpf/src/main.rs
5+
@@ -2,7 +2,6 @@ use {
6+
bzip2::bufread::BzDecoder,
7+
clap::{crate_description, crate_name, crate_version, Arg},
8+
regex::Regex,
9+
- solana_download_utils::download_file,
10+
solana_sdk::signature::{write_keypair_file, Keypair},
11+
std::{
12+
collections::{HashMap, HashSet},
13+
@@ -113,81 +112,12 @@ where
14+
15+
// Check whether a package is installed and install it if missing.
16+
fn install_if_missing(
17+
- config: &Config,
18+
- package: &str,
19+
- url: &str,
20+
- download_file_name: &str,
21+
- target_path: &Path,
22+
+ _config: &Config,
23+
+ _package: &str,
24+
+ _url: &str,
25+
+ _download_file_name: &str,
26+
+ _target_path: &Path,
27+
) -> Result<(), String> {
28+
- // Check whether the target path is an empty directory. This can
29+
- // happen if package download failed on previous run of
30+
- // cargo-build-bpf. Remove the target_path directory in this
31+
- // case.
32+
- if target_path.is_dir()
33+
- && target_path
34+
- .read_dir()
35+
- .map_err(|err| err.to_string())?
36+
- .next()
37+
- .is_none()
38+
- {
39+
- fs::remove_dir(&target_path).map_err(|err| err.to_string())?;
40+
- }
41+
-
42+
- // Check whether the package is already in ~/.cache/solana.
43+
- // Download it and place in the proper location if not found.
44+
- if !target_path.is_dir()
45+
- && !target_path
46+
- .symlink_metadata()
47+
- .map(|metadata| metadata.file_type().is_symlink())
48+
- .unwrap_or(false)
49+
- {
50+
- if target_path.exists() {
51+
- fs::remove_file(&target_path).map_err(|err| err.to_string())?;
52+
- }
53+
- fs::create_dir_all(&target_path).map_err(|err| err.to_string())?;
54+
- let mut url = String::from(url);
55+
- url.push('/');
56+
- url.push_str(config.bpf_tools_version);
57+
- url.push('/');
58+
- url.push_str(download_file_name);
59+
- let download_file_path = target_path.join(download_file_name);
60+
- if download_file_path.exists() {
61+
- fs::remove_file(&download_file_path).map_err(|err| err.to_string())?;
62+
- }
63+
- download_file(url.as_str(), &download_file_path, true, &mut None)?;
64+
- let zip = File::open(&download_file_path).map_err(|err| err.to_string())?;
65+
- let tar = BzDecoder::new(BufReader::new(zip));
66+
- let mut archive = Archive::new(tar);
67+
- archive
68+
- .unpack(&target_path)
69+
- .map_err(|err| err.to_string())?;
70+
- fs::remove_file(download_file_path).map_err(|err| err.to_string())?;
71+
- }
72+
- // Make a symbolic link source_path -> target_path in the
73+
- // sdk/bpf/dependencies directory if no valid link found.
74+
- let source_base = config.bpf_sdk.join("dependencies");
75+
- if !source_base.exists() {
76+
- fs::create_dir_all(&source_base).map_err(|err| err.to_string())?;
77+
- }
78+
- let source_path = source_base.join(package);
79+
- // Check whether the correct symbolic link exists.
80+
- let invalid_link = if let Ok(link_target) = source_path.read_link() {
81+
- if link_target.ne(target_path) {
82+
- fs::remove_file(&source_path).map_err(|err| err.to_string())?;
83+
- true
84+
- } else {
85+
- false
86+
- }
87+
- } else {
88+
- true
89+
- };
90+
- if invalid_link {
91+
- #[cfg(unix)]
92+
- std::os::unix::fs::symlink(target_path, source_path).map_err(|err| err.to_string())?;
93+
- #[cfg(windows)]
94+
- std::os::windows::fs::symlink_dir(target_path, source_path)
95+
- .map_err(|err| err.to_string())?;
96+
- }
97+
Ok(())
98+
}
99+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{pkgs}:
2+
with pkgs;
3+
stdenv.mkDerivation rec {
4+
name = "solana-bpf-tools-${version}";
5+
version = "1.23";
6+
src = fetchzip {
7+
url = "https://github.com/solana-labs/bpf-tools/releases/download/v${version}/solana-bpf-tools-linux.tar.bz2";
8+
sha256 = "sha256-4aWBOAOcGviwJ7znGaHbB1ngNzdXqlfDX8gbZtdV1aA=";
9+
stripRoot = false;
10+
};
11+
12+
nativeBuildInputs = [autoPatchelfHook];
13+
buildInputs = with pkgs; [
14+
zlib
15+
stdenv.cc.cc
16+
openssl
17+
];
18+
19+
installPhase = ''
20+
mkdir -p $out/dependencies/bpf-tools;
21+
cp -r $src/llvm $out/dependencies/bpf-tools/;
22+
cp -r $src/rust $out/dependencies/bpf-tools/;
23+
chmod 0755 -R $out;
24+
mkdir -p $out/bin/sdk/bpf/
25+
cp -r $out/dependencies $out/bin/sdk/bpf/
26+
'';
27+
28+
meta = with lib; {
29+
homepage = "https://github.com/solana-labs/bpf-tools/releases";
30+
platforms = platforms.linux;
31+
};
32+
}

shell.nix

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{pkgs}:
2-
with pkgs; let
3-
nodejs = nodejs-16_x;
4-
corepack = callPackage ./nix/corepack-shims {inherit nodejs;};
5-
in
2+
with pkgs;
63
mkShell {
74
buildInputs = [
85
# For priting the direnv banner
@@ -15,13 +12,11 @@ in
1512
# and tailed log files
1613
tmux
1714
tmuxinator
18-
19-
# Node.js dev environment for unit tests
20-
nodejs
21-
corepack
15+
metacraft-labs.solana
2216
];
2317

2418
shellHook = ''
2519
figlet "nix-blockchain-development"
20+
echo "${metacraft-labs.solana}"
2621
'';
2722
}

0 commit comments

Comments
 (0)