scala-seed
scala-seed copied to clipboard
A Nix flake for getting started with Scala.
What is this?
This is an opinionated Nix flake for getting started with the Scala programming language. It creates a development subshell with the following Scala tools on the path:
- Ammonite
- Bloop
- Coursier
- GraalVM CE based on OpenJDK 21
- Mill
- sbt
- Scala CLI
- Scalafmt CLI
In fact it can create alternative subshells with these instead:
The first time you use this subshell these tools will be downloaded and cached. Once you exit the subshell they will no longer be on your path. The second run is instantaneous.
Prerequisites
Due to this issue bloop is not published for arm processors yet so you'll need to install Rosetta if you are on Apple silicon:
/usr/sbin/softwareupdate --install-rosetta --agree-to-license
Installation
-
Install the Nix package manager by selecting your OS in the official guide. Don't forget to reopen the terminal!
-
Enable the flakes feature:
mkdir -p ~/.config/nix echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.confIf the Nix installation is in multi-user mode, don’t forget to restart the
nix-daemonby running:sudo systemctl restart nix-daemon
Alternative unofficial installation
Taken from https://zero-to-nix.com/start/install
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
Usage
Use the default subshell (there is NO need to clone this repo) by running:
nix develop github:devinsideyou/scala-seed
For direnv/nix-direnv users put the following into your .envrc:
use flake github:devinsideyou/scala-seed
Pro tip: I will keep updating this flake so you might want to pin it to a specific commit:
nix develop github:devinsideyou/scala-seed/0c3b8c657b37eae320b073724d74390cf3162edf
use flake github:devinsideyou/scala-seed/0c3b8c657b37eae320b073724d74390cf3162edf
Alternative shells can be used as follows:
nix develop github:devinsideyou/scala-seed#java21 # the same as the default
nix develop github:devinsideyou/scala-seed#java17
nix develop github:devinsideyou/scala-seed#java11
nix develop github:devinsideyou/scala-seed#java8
Here is how you can see the metadata of the flake:
nix flake metadata github:devinsideyou/scala-seed
And here is how you can see everything the flake has to offer:
nix flake show github:devinsideyou/scala-seed --all-systems
Here is a useful incantation to pretty print a filtered list of what's on the path:
echo -e ${buildInputs// /\\n} | cut -d - -f 2- | sort
And here is another one that also shows the locations:
echo -e ${buildInputs// /\\n} | sort -t- -k2,2 -k3,3
And here is yet another one that shows everything Nix put on the path:
echo $PATH | sed 's/:/\n/g' | grep /nix/store | sort --unique -t- -k2,2 -k3,3
Just like any other subshell this one can be exited by typing exit or pressing Ctrl+D.
Scala first steps
Now that you have a working dev environment you can create your first Scala project like this:
cs launch giter8 -- devinsideyou/scala-seed # Scala 2
cs launch giter8 -- devinsideyou/scala3-seed # Scala 3
Now cd into your newly created project and launch sbt by typing sbt. The template you just used to create a project will display a couple of useful aliases for you to try. For instance r to run the program or t to run the tests. Type exit or press Ctrl+D when you are done to exit sbt. Don't forget that you are still inside of the Nix subshell so type exit or press Ctrl+D again to end up back in your regular shell.
Here is a Scala Crash Course and here is a Functional Programming Crash Course.
Ask questions on discord!
Welcome to Scala!
PS
Most Scala devs either use Intellij IDEA or the editors supported by Metals - a Scala language server.
This flake was tested in WSL 2 on Ubuntu-20.04 LTS, but it should work on Macs as well. I don't have a Mac, but I will set up CI eventually to test on them. Please report issues until then. Thank you!