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

docs(tutorial): add rust tutorial including openssl example #1155

Merged
merged 11 commits into from
Apr 15, 2024
5 changes: 3 additions & 2 deletions docs/tutorials/ros2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ The audience for this tutorial is developers who are familiar with ROS 2 and how
The crux of this tutorial is to show you only need pixi!
- On Windows, it's advised to enable Developer mode. Go to Settings -> Update & Security -> For developers -> Developer mode.

If you're new to pixi, you can check out the [basic usage](../basic_usage.md) guide.
This will teach you the basics of pixi project within 3 minutes.
!!! note ""
If you're new to pixi, you can check out the [basic usage](../basic_usage.md) guide.
This will teach you the basics of pixi project within 3 minutes.

## Create a pixi project.

Expand Down
152 changes: 152 additions & 0 deletions docs/tutorials/rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Tutorial: Develop a Rust package using `pixi`

In this tutorial, we will show you how to develop a Rust package using `pixi`.
The tutorial is written to be executed from top to bottom, missing steps might result in errors.

The audience for this tutorial is developers who are familiar with Rust and `cargo` and how are interested to try pixi for their development workflow.
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved

!!! note ""
If you're new to pixi, you can check out the [basic usage](../basic_usage.md) guide.
This will teach you the basics of pixi project within 3 minutes.

## Prerequisites

- You need to have `pixi` installed. If you haven't installed it yet, you can follow the instructions in the [installation guide](../index.md).
The crux of this tutorial is to show you only need pixi!

## Create a pixi project.

```shell
pixi init my_rust_project
cd my_rust_project
```

It should have created a directory structure like this:

```shell
my_rust_project
├── .gitattributes
├── .gitignore
└── pixi.toml
```

The `pixi.toml` file is the manifest file for your project. It should look like this:

```toml title="pixi.toml"
[project]
name = "my_rust_project"
version = "0.1.0"
description = "Add a short description here"
authors = ["User Name <user.name@email.url>"]
channels = ["conda-forge"]
platforms = ["linux-64"]
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a comment that platforms will differ depends on your machine ( to not create confusion )


[tasks]

[dependencies]
```

## Add Rust dependencies

To use a pixi project you don't need any dependencies on your system, all the dependencies you need should be added through pixi, so other users can use your project without any issues.
```shell
pixi add rust
```

This will add the `rust` package to your `pixi.toml` file.
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a comment from where it will add

Copy link
Contributor

Choose a reason for hiding this comment

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

also maybe we can add something like:

This will add the rust package to your pixi.toml file under [dependencies]


## Add a `cargo` project
As you now have rust installed, you can create a `cargo` project in your `pixi` project.
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
```shell
pixi run cargo init
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add here maybe as a note which will briefly explain how pixi run works so it would be clear for a novice user why we write
pixi run cargo init

```

Now we can build a `cargo` project using `pixi`.
```shell
pixi run cargo build
```
To simplify the build process, you can add a `build` task to your `pixi.toml` file using the following command:
```shell
pixi task add build "cargo build"
```
Which creates this field in the `pixi.toml` file:
```toml title="pixi.toml"
[tasks]
build = "cargo build"
```

And now you can build your project using:
```shell
pixi run build
```

You can also run your project using:
```shell
pixi run cargo run
```
Which you can simplify with a task again.
```shell
pixi task add start "cargo run"
```

So you should get the following output:
```shell
pixi run start
Hello, world!
```

Congratulations you have a Rust project running on your machine with pixi!
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved

## Next steps, why is this useful when there is `rustup`?
Cargo is not a binary package manager, but a source package manager.
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
This means that you need to have the Rust compiler installed on your system to use it.
And possibly other dependencies that are not included in the `cargo` package manager.
For example, you might need to install `openssl` or `libssl-dev` on your system to build a package.
This is the case for `pixi` as well, but `pixi` will install these dependencies in your project folder, so you don't have to worry about them.

Add the following dependencies to your cargo project:
```shell
pixi run cargo add git2
```

If your system is not preconfigured to build C and have the `libssl-dev` package installed you will not be able to build the project:
```shell
pixi run build
...
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
Could not find directory of OpenSSL installation, and this `-sys` crate cannot
proceed without this knowledge. If OpenSSL is installed and this crate had
trouble finding it, you can set the `OPENSSL_DIR` environment variable for the
compilation process.

Make sure you also have the development packages of openssl installed.
For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

If you're in a situation where you think the directory *should* be found
automatically, please open a bug at https://github.com/sfackler/rust-openssl
and include information about your system as well as this message.

$HOST = x86_64-unknown-linux-gnu
$TARGET = x86_64-unknown-linux-gnu
openssl-sys = 0.9.102


It looks like you're compiling on Linux and also targeting Linux. Currently this
requires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config`
could not be found. If you have OpenSSL installed you can likely fix this by
installing `pkg-config`.
...
```
You can fix this with pixi:
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can write something like this:
You can fix this by adding necessary dependencies for building git2

```shell
pixi add openssl pkg-config compilers
```

Now you can build your project again:
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
```shell
pixi run build
...
Compiling git2 v0.18.3
Compiling my_rust_project v0.1.0 (/my_rust_project)
Finished dev [unoptimized + debuginfo] target(s) in 7.44s
Running `target/debug/my_rust_project`
```
Copy link
Contributor

@nichmor nichmor Apr 12, 2024

Choose a reason for hiding this comment

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

Maybe we can write a small outro? like conclusion or some small push for user to discover other commands

something like:

As a small exercise you can add clippy as dependency
add cargo fmt as a task.

Also another crazy idea and you can totally ignore it:

maybe we can add a call to action for our users to share completion of this tutorial on twitter / linkedin?

something like:

You can share a completion of this tutorial within our amazing community! Post it on linkedin or twitter with #pixi #prefix.dev and share your thoughts!

1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ nav:
- RStudio: ide_integration/r_studio.md
- Tutorials:
- ROS 2: tutorials/ros2.md
- Rust: tutorials/rust.md
- Examples:
- C++/Cmake: examples/cpp-sdl.md
- OpenCV: examples/opencv.md
Expand Down
Loading