Skip to content

Commit

Permalink
chore(nix): add scarb package manager to development tools
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron committed Jan 6, 2025
1 parent 922d7a8 commit d914fc2
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
13 changes: 12 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
overlays = [
(import rust-overlay)
(final: prev: {
scarb = final.callPackage (./. + "/tools/scarb.nix") {};
})
];

pkgs = import nixpkgs {
inherit system overlays;
};

rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;

in
{
# Export the scarb package
packages.scarb = pkgs.scarb;
packages.default = pkgs.scarb;

devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
Expand All @@ -24,6 +34,7 @@
nodePackages.prettier
taplo-cli
yq
scarb
];

buildInputs = with pkgs; [
Expand Down
107 changes: 107 additions & 0 deletions tools/scarb.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{ pkgs }:

let
# Define versions and their hashes
versions = {
"2.8.2" = {
hashes = {
"aarch64-apple-darwin" = "59d041fca5404b0c60e0e1ff22e9d1929b118c9914ba80c012cac7c372168e2c";
"x86_64-apple-darwin" = "209979076e65ba3e0d53506cab826516a647f44c0d77fc4be221fea5bcb6607e";
"aarch64-unknown-linux-gnu" = "c4e0066b00af0e644585f4c27ecea55abed732679b7e8654876303bae982e2f6";
"x86_64-unknown-linux-gnu" = "2033173c4b8e72fd4bf1779baca6d5348d4f0b53327a14742cb2f65c2f0e892c";
"aarch64-unknown-linux-musl" = "35bb6f035691fe71faf5836b8e90d05671f4407a14758e31658fd5c7e97d2a08";
"x86_64-unknown-linux-musl" = "b8c99d5f917880f4416e443abfaeab9eb2af7b8d34d34a84e5c913b0e8149d22";
"x86_64-pc-windows-msvc" = "ef9161f4305c4ac3262810fb0e888835a00204b98dd185ec07d2cc4e3e9c0330";
};
};
};

# System mapping
systemMap = {
"aarch64-darwin" = "aarch64-apple-darwin";
"x86_64-darwin" = "x86_64-apple-darwin";
"aarch64-linux" = "aarch64-unknown-linux-gnu";
"x86_64-linux" = "x86_64-unknown-linux-gnu";
"aarch64-linux-musl" = "aarch64-unknown-linux-musl";
"x86_64-linux-musl" = "x86_64-unknown-linux-musl";
"x86_64-windows" = "x86_64-pc-windows-msvc";
};

# Get current system platform
currentSystem = pkgs.stdenv.hostPlatform.system;
platform = systemMap.${currentSystem} or (throw "Unsupported system: ${currentSystem}");

# Get extension based on platform
extension = if platform == "x86_64-pc-windows-msvc" then "zip" else "tar.gz";

# Get version data
version = "2.8.2";
versionData = versions.${version} or (throw "Version ${version} not found");
sha256 = versionData.hashes.${platform} or (throw "Hash not found for ${platform} in version ${version}");

in
pkgs.stdenv.mkDerivation {
pname = "scarb";
inherit version;

src = pkgs.fetchurl {
url = "https://github.com/software-mansion/scarb/releases/download/v${version}/scarb-v${version}-${platform}.${extension}";
inherit sha256;
};

# Add meta information
meta = with pkgs.lib; {
description = "Scarb package manager for Cairo/Starknet development";
homepage = "https://github.com/software-mansion/scarb";
license = licenses.mit;
platforms = builtins.attrNames systemMap;
maintainers = with maintainers; [ ];
};

# Installation phase
installPhase = ''
mkdir -p $out/{bin,doc}
${if extension == "zip" then ''
unzip $src
cd scarb-v${version}-${platform}
'' else ''
tar xf $src
cd scarb-v${version}-${platform}
''}
# Install binaries
mv bin/* $out/bin/
# Install documentation
mv doc/* $out/doc/
# Make all binaries executable
chmod +x $out/bin/*
# Verify installation of all required binaries
required_bins=(
scarb
scarb-cairo-language-server
scarb-cairo-run
scarb-cairo-test
scarb-doc
scarb-snforge-test-collector
)
for bin in "''${required_bins[@]}"; do
if [ ! -x "$out/bin/$bin" ]; then
echo "Error: Required binary $bin not found or not executable"
exit 1
fi
done
'';

# Add required build inputs
nativeBuildInputs = with pkgs; [
gnutar
gzip
] ++ pkgs.lib.optionals (extension == "zip") [
unzip
];
}

0 comments on commit d914fc2

Please sign in to comment.