Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala Native and Nix - a match made in heaven??

Scala Native and Nix - a match made in heaven??

Scala on the JVM has many conveniences that we sometimes take for granted - the portability of classfiles can make you oblivious to the complexity of the bare metal... but you can quickly disillusion yourself by trying Scala Native!

Using native libraries, cross-compiling to different architectures, distribution... just the beginning of problems that we have to solve anew.

Thankfully, there are decent solutions to some of these problems, and I'm going to try and convince you that Nix solves all of them - in this talk, I'll go over my experience building a Scala Native app interacting with native libraries (e.g. `hidapi`), and how Nix helped me with these struggles.

Jakub Kozłowski

December 11, 2024
Tweet

More Decks by Jakub Kozłowski

Other Decks in Programming

Transcript

  1. Jakub Kozłowski | WSUG | 11.12.2024 Scala Native & Nix

    A match made in heaven?? https://linktr.ee/kubukoz
  2. I once made a REPL for managing my Spotify playlists

    • f: fast forward • m: move to "good" • d: remove from "pending" • (and some more)
  3. 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
  4. 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
  5. 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
  6. 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...
  7. 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
  8. Scala Native Alternative target for Scala • Supports all Scala

    language features • Compiles using LLVM • Runs without a JVM • Relatively easy to set up nowadays
  9. Let's use hidapi on Native! • Get headers • Generate

    bindings • Get hidapi binary • Compile & link • Run
  10. Get hidapi binary • Use a package manager like brew,

    vcpkg... • Or compile from source
  11. Scala Native The downsides... • Portability :( • Native dependencies

    :( • Linking times (getting better) • Scala library availability :/
  12. 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
  13. Nix

  14. What is Nix? • A package manager, build system, deployment

    tool... • Purely functional, immutable packages... • Built and installed in isolation...
  15. 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/
  16. Scala Native build with Nix ...with native dependencies 1. SN

    con fi g (linux has hidapi-hidraw & hidapi-libusb variants)
  17. 2. Bindgen con fi g Headers provided by Nix sn-bindgen

    binary provided by Nix Scala Native build with Nix ...with native dependencies
  18. 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
  19. 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 • ...
  20. Is this for me? • I don't know, consider alternatives

    too • Find your level of Nix (dis)comfort (nix builds? shells? home-manager? NixOS?)