Slide 1

Slide 1 text

Jakub Kozłowski | WSUG | 11.12.2024 Scala Native & Nix A match made in heaven?? https://linktr.ee/kubukoz

Slide 2

Slide 2 text

Story time

Slide 3

Slide 3 text

I once made a REPL for managing my Spotify playlists • f: fast forward • m: move to "good" • d: remove from "pending" • (and some more)

Slide 4

Slide 4 text

But it had problems. • had to keep the terminal tab open nearby • switching to/from it took time and I used it tens/hundreds of times throughout the day • the work fl ow in general could use improvement

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

I decided to get rid of the keyboard • Using the HID API, read a DualSense's output • Decode with scodec • Map to button presses and commands • Pipe to the Spotify REPL

Slide 7

Slide 7 text

HID on the JVM: hidapi + hid4java • hidapi: C library for HID access, abstracting away OS di ff erences • hid4java: JNA-based Java library wrapping hidapi

Slide 8

Slide 8 text

Problem solved? • I don't always have a free USB port • Feels odd to keep this running on my work computer all the time • (and there's overhead) • But I had one of these! Not quite...

Slide 9

Slide 9 text

JVM on RPi Zero 2 😒 • slow startup (3s to run hello world, 11s on the dualshock4s CLI) • poor performance • JNA / native library shenanigans at runtime • surely there's a better way

Slide 10

Slide 10 text

Onto Scala Native

Slide 11

Slide 11 text

Scala Native Alternative target for Scala • Supports all Scala language features • Compiles using LLVM • Runs without a JVM • Relatively easy to set up nowadays

Slide 12

Slide 12 text

Let's use hidapi on Native! • Get headers • Generate bindings • Get hidapi binary • Compile & link • Run

Slide 13

Slide 13 text

Headers

Slide 14

Slide 14 text

Bindings https://sn-bindgen.indoorvivants.com/ (this is easier in sbt)

Slide 15

Slide 15 text

Get hidapi binary • Use a package manager like brew, vcpkg... • Or compile from source

Slide 16

Slide 16 text

Compilation & linking This part gets tricky Where they are What libraries to link with

Slide 17

Slide 17 text

Running :shipit:

Slide 18

Slide 18 text

Running... on RPi

Slide 19

Slide 19 text

Scala Native The downsides... • Portability :( • Native dependencies :( • Linking times (getting better) • Scala library availability :/

Slide 20

Slide 20 text

Cross-compilation of native programs • generally possible • crazy di ff i cult • I will not get into it during this talk • ...but I did once manage to cross- compile to a 32-bit bare-metal console: https://youtu.be/paHZkg8Py1U https://clang.llvm.org/docs/CrossCompilation.html

Slide 21

Slide 21 text

What now? • you know exactly what comes now ❆

Slide 22

Slide 22 text

Nix

Slide 23

Slide 23 text

What is Nix? • A package manager, build system, deployment tool... • Purely functional, immutable packages... • Built and installed in isolation...

Slide 24

Slide 24 text

What does Nix buy us?

Slide 25

Slide 25 text

Nixpkgs The world's largest package repository (according to Repology)

Slide 26

Slide 26 text

Remote builds ... https://nixbuild.net

Slide 27

Slide 27 text

Transparent caching fi rst run A: 21.337s fi rst run B: 23.391s second run A: 0.226s Works remotely too! https://zero-to-nix.com/concepts/caching/ https://www.cachix.org/

Slide 28

Slide 28 text

nix copy Copy a Nix path and all its dependencies

Slide 29

Slide 29 text

Scala Native build with Nix ...with native dependencies 1. SN con fi g (linux has hidapi-hidraw & hidapi-libusb variants)

Slide 30

Slide 30 text

2. Bindgen con fi g Headers provided by Nix sn-bindgen binary provided by Nix Scala Native build with Nix ...with native dependencies

Slide 31

Slide 31 text

3. Nix environment Scala Native build with Nix ...with native dependencies fl ake.nix

Slide 32

Slide 32 text

4. Nix derivation Scala Native build with Nix ...with native dependencies Changes whenever you update sbt libs/plugins Makes hidapi appear on the link path How to build the app Putting the app in the special path at $out sbt-derivation / Nix shenanigans ;) from sbt-derivation

Slide 33

Slide 33 text

Bonus: development shell + + =

Slide 34

Slide 34 text

Caveats • Nix documentation is scattered around the Internet • It's much better today, and improving! https://nix.dev • Complexity, usability issues • Dated syntax • Disk usage can go high (use GC!) • Caches can sometimes take more time to download than a fresh build would • ...

Slide 35

Slide 35 text

Is this for me? • I don't know, consider alternatives too • Find your level of Nix (dis)comfort (nix builds? shells? home-manager? NixOS?)

Slide 36

Slide 36 text

How to get started? -https://zero-to-nix.com/start/ -https://nix.dev/ -https://nixos.org/guides/nix-pills/ -https://github.com/zaninime/sbt-derivation More about Scala + Nix: https://speakerdeck.com/kubukoz/nix-for-scala-folks

Slide 37

Slide 37 text

Links • Slides/contact/YouTube: https://linktr.ee/kubukoz • Sample repo: https://github.com/kubukoz/talks/tree/main/scala-native-and-nix • Nix/NixOS: https://nixos.org/ • Nixpkgs Search: https://search.nixos.org/packages https://linktr.ee/kubukoz