pict-rs 0.3.2 on OpenBSD 7.2: Install
Tags: pictrs, rust, actix, minio
Published: 2023-01-15 13:12:13

## Summary

This post is a part of [Lemmy 0.16.7 on OpenBSD 7.2](https://obsd.solutions/en/blog/2023/01/15/lemmy-0167-on-openbsd-72/index.html) about [Lemmy](https://join-lemmy.org/).

### Premise

The server is [OpenBSD](https://www.openbsd.org/) and [Rust (rustlang)](https://www.rust-lang.org/) is installed. Also, there is `_lemmy` user whose home directory is `/var/lemmy`.

### Environment

- OS: OpenBSD [7.2](https://www.openbsd.org/72.html)
- Object Storage: [MinIO](https://min.io/) - [AWS](https://aws.amazon.com/) [S3](https://aws.amazon.com/s3/)-compatible (Optional)
- Apps
  - [pict-rs](https://git.asonix.dog/asonix/pict-rs/): 0.3.2
    - based on Rust [1.63](https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html) + Actix Web [4](https://github.com/actix/actix-web/wiki/v4-Announcement) + Actix 0.13

## Tutorial

### pict-rs server

Lemmy uses pict-rs to store image. It is a simple image hosting service which is open source and written in Rust, too.

#### Prepare for the dependencies

You have to install [`protobuf`](https://github.com/protocolbuffers/protobuf), "Protocol Buffers - Google's data interchange format"

Therefore switch back to your own user if you are acting as `_lemmy` to install it:

```console
$ exit

$ doas pkg_add protobuf
```

The output was:

```console
quirks-6.42 signed on 2023-01-13T18:22:41Z
protobuf-3.21.6: ok
```

#### Build pict-rs server

Switch to `_lemmy`:

```console
$ doas su - _lemmy
```

Get the source of the stable version (v0.4 didn't work in my case):

```console
$ git clone https://git.asonix.dog/asonix/pict-rs.git --branch v0.3.2 --single-branch
```

The output was:

```console
Cloning into 'pict-rs'...
remote: Enumerating objects: 2707, done.
remote: Counting objects: 100% (2707/2707), done.
remote: Compressing objects: 100% (1932/1932), done.
remote: Total 2707 (delta 1806), reused 1077 (delta 687), pack-reused 0
Receiving objects: 100% (2707/2707), 2.21 MiB | 114.00 KiB/s, done.
Resolving deltas: 100% (1806/1806), done.
Note: switching to '4dff8de9851041911b8243a35bf9f6df4d612268'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
(...)
```

Go inside:

```console
$ cd pict-rs
```

Then run to build:

```console
$ cargo build --release
```

The output was:

```console
    Updating git repository `https://github.com/asonix/rust-s3`
   (...)
   Compiling rustls v0.20.6
   (...)
   Compiling actix-web v4.1.0
   (...)
   Compiling pict-rs v0.3.2 (/var/lemmy/pict-rs)
    Finished release [optimized] target(s) in 20m 35s
```

Now you can run pict-rs server with `-p` option to specify data directory:

```console
$ cargo run --release -- -p data
```

The output was:

```console
    Finished release [optimized] target(s) in 0.24s
     Running `target/release/pict-rs -p data`
2023-01-14T12:51:58.377271Z  INFO restructure{store=FileStore { path_gen: "generator", root_dir: "data" }}: pict_rs::store::file_store::restructure: new
2023-01-14T12:51:58.377478Z  INFO restructure{store=FileStore { path_gen: "generator", root_dir: "data" }}: pict_rs::store::file_store::restructure: close time.busy=11.3µs time.idle=197µs
2023-01-14T12:51:58.377627Z  INFO actix_server::builder: Starting 1 workers
2023-01-14T12:51:58.377736Z  INFO actix_server::server: Actix runtime found; starting in Actix runtime
```

Additionally, you can specify address to listen:

```console
$ cargo run --release -- -a 127.0.0.1:8080 -p data
```

Additionally, you can modify its configuration:

```console
$ nvim pict-rs.toml
```

For example:

```diff
- api_key = 'API_KEY'

- image_format = 'jpeg'

- opentelemetry_url = 'http://localhost:4317/'
+ opentelemetry_url = 'http://127.0.0.1:4317/'
```

##### Integrate pict-rs with MinIO (Optional)

pict-rs stores image files in their local directory by default.

Well, there is a way to introduce MinIO bucket usage by editting `pict-rc.toml` as below:

```diff
- type = "file_store"
+ type = "s3_store"
+ 
+ region = "https://(minio-fqdn)"
+ bucket_name = "(...)"
+ access_key = "(...)"
+ secret_key = "(...)"
```

and, of course, creating MinIO bucket and service account, with bucket policy like:

```json
{
 "Version": "2012-10-17",
 "Statement": [
  {
   "Effect": "Allow",
   "Action": [
    "admin:*"
   ]
  },
  {
   "Effect": "Allow",
   "Action": [
    "s3:*"
   ],
   "Resource": [
    "arn:aws:s3:::{bucket-name}",
    "arn:aws:s3:::{bucket-name}/*"
   ]
  }
 ]
}
```

## Conclusion

Well done :) Go on to the next step.

<center>[Return](https://obsd.solutions/en/blog/2023/01/15/lemmy-0167-on-openbsd-72/index.html)</center>