Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dev shell environment #1534

Merged
merged 5 commits into from
Aug 13, 2024
Merged

Add dev shell environment #1534

merged 5 commits into from
Aug 13, 2024

Conversation

Ifropc
Copy link
Contributor

@Ifropc Ifropc commented Aug 8, 2024

What

This PR adds a devshell environment using nix

Why

Currently, in order to develop stellar-cli locally you need to install some dependencies, and sometimes you also need to modify your .<shell>rc file to make your environment easy to use for development.
However, this makes switching between different branches/versions or switching between development/production builds more complicated.
This change adds a configuration for a development shell, which automatically does the following:

  1. Installs all necessary dependencies (only accessible in the shell) -> rust, make, libudev, etc. without affecting your global environment. Dependencies are pinned into the lockfile. We can always update rust version to latest stable or change it to nightly with re-generating lockfile/changing nix config. Dependencies are installed once and cached. Can clean them out of your system with nix collect-garbage.
  2. Setup stellar alias, configure development key and environment (set STELLAR_NETWORK and STELLAR_ACCOUNT variables)
  3. Add up-to-date autocomplete in the shell

Developer simply needs to install nix and run nix develop

This should work both on any Linux distro, Mac (Darwin builds are available for all packages in nixpkgs) and Windows WSL

Known limitations

nix is required to be installed in the system, with v2.20+
Currently bash is used as a default shell. It's possible to use other shells with nix develop -c zsh

@Ifropc
Copy link
Contributor Author

Ifropc commented Aug 8, 2024

Examples:

# No rust toolchain is installed in my system, and I have `stellar` installed in ~/.cargo/bin/stellar
ifro@nixos:~/projects/stellar-cli/ > which cargo
cargo not found
ifro@nixos:~/projects/stellar-cli/ > stellar --version
stellar 21.2.0 (v20.0.0-248-g96e819c949089a6744621164bb406fc411de676e-dirty)
soroban-env 21.2.0 (8809852dcf8489f99407a5ceac12625ee3d14693)
soroban-env interface version 90194313216
stellar-xdr 21.2.0 (9bea881f2057e412fdbb98875841626bf77b4b88)
xdr curr (70180d5e8d9caee9e8645ed8a38c36a8cf403cd9)
ifro@nixos:~/projects/stellar-cli/ > nix develop
Using nix (Nix) 2.20.8
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
     Running `target/debug/stellar keys add dev_key`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
     Running `target/debug/stellar completion --shell bash`

# Have rust toolchain in devshell, and `stellar` points to the freshly built binary 
[ifro@nixos:~/projects/stellar-cli]$ which cargo
/nix/store/i223gfb0a86hxywdsqxmyy291z511056-rust-default-1.80.0/bin/cargo

[ifro@nixos:~/projects/stellar-cli]$ stellar --version
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
     Running `target/debug/stellar --version`
stellar 21.3.0 (v20.0.0-257-g0e615b6c61d03ab294fd6ccab42b1b08f317a437)
soroban-env 21.2.0 (8809852dcf8489f99407a5ceac12625ee3d14693)
soroban-env interface version 90194313216
stellar-xdr 21.2.0 (9bea881f2057e412fdbb98875841626bf77b4b88)
xdr curr (70180d5e8d9caee9e8645ed8a38c36a8cf403cd9)

@Ifropc
Copy link
Contributor Author

Ifropc commented Aug 8, 2024

Also no documentation on it at the moment, open to suggestions on where to add it (README.md or CONTRIBUTING.md)

@leighmcculloch
Copy link
Member

Documentation should be in CONTRIBUTING.md I think.

Copy link
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if many folks deving on the cli use nixos, but sounds like you do @Ifropc. Can you remove the secret key and setup assumptions so that the flake is only the tool setup and doesn't make other assumptions? Otherwise lgtm.

flake.nix Outdated Show resolved Hide resolved
@Ifropc
Copy link
Contributor Author

Ifropc commented Aug 9, 2024

I added secret key just cause I thought it'd be a good idea to easily test things locally (so shared dev secret key is fine) but I now I think it's better if i remove exporting the variable when key is not defined instead
Also you don't need to use nixos for that! Nix is just a package manager and environment configuration tool so you can install and use it in any Linux distro or without impacting your native package manager (as all installed are done against your dev shell)

@leighmcculloch
Copy link
Member

I added secret key just cause I thought it'd be a good idea to easily test things locally (so shared dev secret key is fine) but I now I think it's better if i remove exporting the variable when key is not defined instead

It's very easy for devs to create a key, it's a single command, stellar keys generate me.

flake.nix Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
Co-authored-by: Leigh McCulloch <[email protected]>
@Ifropc Ifropc enabled auto-merge (squash) August 13, 2024 17:05
@Ifropc Ifropc merged commit dafc95a into main Aug 13, 2024
25 checks passed
@Ifropc Ifropc deleted the flake branch August 13, 2024 17:09
@TomMD
Copy link
Contributor

TomMD commented Oct 7, 2024

Before I open an issue perhaps asking here makes sense: why the libudev0 dependency? That means it can't be used on mac/darwin.

@Ifropc
Copy link
Contributor Author

Ifropc commented Oct 8, 2024

Hey @TomMD answered in #1661 thanks for raising this up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants