Skip to content

zaninime/sbt-derivation

Repository files navigation

sbt-derivation

A Nix library for building sbt-based, Scala projects with Nix.

Tests GitHub license PRs Welcome


Note: you are browsing the version 2 of this project. If you've been using this library already in your projects, please follow the migration guide below.

Features

  • Sane defaults: start by only specifying the options required by Nix (4 in total)
  • More advanced options for specific use-cases
  • Compatible with sbt > 1.5.1 up until latest
  • Tested by GitHub actions against a matrix of sbt versions and Scala 2.12, 2.13, 3.1
  • Follows similar conventions as used in Nixpkgs
  • Reduced footprint: depends on sbt, find, sed and a couple of utils to make the JARs reproducible
    • Depending on the archival strategy, the number of tools might increase, e.g. tar is necessary for making archives

How it works

The mechanism used by sbt-derivation is the same as the one used in other language ecosystems in Nixpkgs, e.g. Go and Rust. Internally, two derivations are created: one for the dependencies and one for the actual build.

The dependencies derivation is a fixed-output derivation, it has a hash that needs to be changed each time the dependencies are updated or changed in any way. It's also far from trivial to calculate this hash programmatically, strictly from the project files. The proposed solution is trust on first use (aka let a build fail the first time and use the hash that Nix prints).

The actual build derivation copies the dependencies in the workspace before running the build step. The provided sbt is already configured to point to those.

Installation

There are three ways of installing this library in your project.


If your project uses Flakes

Simply add an input to your Flake pointing to this repository.

{
  description = "My Scala project";

  # you probably have this one already
  inputs.nixpkgs.url = "github:NixOS/nixpkgs";

  # add this line
  inputs.sbt.url = "github:zaninime/sbt-derivation";
  # recommended for first style of usage documented below, but not necessary
  inputs.sbt.inputs.nixpkgs.follows = "nixpkgs";

  outputs = {
    self,
    nixpkgs,
    sbt,
  }: {
    # first style of usage
    packages.x86_64-linux.my-first-scala-package = sbt.mkSbtDerivation.x86_64-linux {
      pname = "my-scala-package";
      # ...see below for all parameters
    };

    # second style of usage
    packages.x86_64-linux.my-second-scala-package = sbt.lib.mkSbtDerivation {
      # pass your pkgs here
      pkgs = nixpkgs.legacyPackages.x86_64-linux;

      # ...and the rest of the arguments
      pname = "my-scala-package";
    };
  };
}

If your project uses Niv (or equivalent)

Add the dependency by using:

$ niv add zaninime/sbt-derivation

Import then the overlay and pass it to your copy of nixpkgs.