Code Monkey home page Code Monkey logo

typelevel-nix's Introduction

typelevel-nix

In antiquity, installing sbt was sufficient to compile, test, and run most Scala projects. sbt would fetch all your JVM dependencies, and you didn't have other dependencies. There was a Scala CLR target, but it was abandoned.

Years passed, and Scala.js was born, and we were back to crossing platforms. We suddenly needed a Node installation to run our tests. More libraries are supporting Scala Native. Static site generators outside our JVM bubble have come and gone.

"Faugh," you say. "The JVM is good enough for me". Fine, but your libraries target Java 8 for broad appeal and your applications target Java 17 because it's 9 bigger, and you have to juggle multiple JVMs.

Wouldn't it be nice to have one-stop shopping for your development environment again?

nix develop to the rescue

Enter the Nix package manager. We can use nix develop to get a full, build enviroment for modern, cross-platform Scala.

For now, you'll need at least Nix 2.4 and to enable the Nix command.

The baseline

$ java -version
bash: java: command not found
$ sbt -version
bash: sbt: command not found
$ node --version
bash: node: command not found

All my apes gone.

Library shell

The one-liner nix develop github:typelevel/typelevel-nix#library gets us a full environment:

$ nix develop github:typelevel/typelevel-nix#library
๐Ÿ”จ Welcome to typelevel-lib-shell

[general commands]

  menu      - prints this menu
  sbt       - A build tool for Scala, Java and more
  scala-cli - Command-line tool to interact with the Scala language

[versions]

  Java - 8.0.292
  Node - 16.13.1

$ sbt -version
sbt version in this project: 1.6.1
sbt script version: 1.6.1
$ exit

Application shell

Alternatively, nix develop github:typelevel/typelevel-nix#application gives us the same, with a more contemporary JDK:

$ nix develop github:typelevel/typelevel-nix#application
๐Ÿ”จ Welcome to typelevel-app-shell

[general commands]

  menu      - prints this menu
  sbt       - A build tool for Scala, Java and more
  scala-cli - Command-line tool to interact with the Scala language

[versions]

  Java - 17.0.1

$ sbt -version
sbt version in this project: 1.6.1
sbt script version: 1.6.1
$ exit

Embed in your project

The typelevelShell module can be imported into your devshell.mkShell configuration:

{
  inputs = {
    typelevel-nix.url = "github:typelevel/typelevel-nix";
    nixpkgs.follows = "typelevel-nix/nixpkgs";
    flake-utils.follows = "typelevel-nix/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils, typelevel-nix }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ typelevel-nix.overlays.default ];
        };
      in
      {
        devShell = pkgs.devshell.mkShell {
          imports = [ typelevel-nix.typelevelShell ];
          name = "my-project-shell";
          typelevelShell = {
            jdk.package = pkgs.jdk8;
            sbtMicrosites = {
              enable = true;
              siteDir = ./site;
            };
          };
        };
      }
    );
}

Extra configuration in typelevelShell:

  • typelevelShell.jdk.package: the JDK package to use for sbt and the $JAVA_HOME environment. Defaults to pkgs.jdk17. If you're writing a library, you probably want pkgs.jdk8.
  • typelevelShell.native.enable: provide a clang environment for scala-native. Defaults to true in the library shell, and false elsewhere.
  • typelevelShell.native.libraries: a list of split-output libraries to include (dev output) and link (lib output). Defaults to [ pkgs.zlib ].
  • typelevelShell.nodejs.enable: provide NodeJS. Defaults to true in the library shell, and false elsewhere.
  • typelevelShell.nodejs.package: the package to use for NodeJS. Defaults to pkgs.nodejs.
  • typelevelShell.sbtMicrosites.enable: enables Jekyll support for sbt-microsites. Defaults to false.
  • typelevelShell.sbtMicrosites.siteDir: directory with your Gemfile, Gemfile.lock, and gemset.nix. Run bundix to create a gemset.nix, and every time you upgrade Jekyll.

Infrequently asked questions

Is this stable?

Not yet, but flake.lock makes it reproducible.

Can I use it with trusty old nix-shell?

Absolutely! The shell.nix provides a flakes-compatible shell that works with nix-shell. It selects the application shell by default, but you can be specific about it. E.g.

$ nix-shell --argstr shell library

To use it remotely, copy the content of the shell.nix in your project and point src to this repository instead. E.g.

{
  src = fetchTarball {
    url = "https://github.com/typelevel/typelevel-nix/archive/main.tar.gz";
    sha256 = "0000000000000000000000000000000000000000000000000000"; # replace hash
  };
};

Can I embed this in a project to share with my colleagues and collaborators?

Yes. You should be able to put a flake.nix so you can share with your colleagues or open source collaborators, and even provide a direnv so your $SHELL and $EDITOR do the right thing. Examples forthcoming.

Can I read .java-version from jEnv to get an appropriate JDK?

typelevelShell = {
  jdk.package = builtins.getAttr "jdk${pkgs.lib.fileContents ./.java-version}" pkgs;
}

Is there a binary cache?

Yes. We are publishing to typelevel.cachix.org for Linux and MacOS, though the heavy things are mostly in nixpkgs.

typelevel-nix's People

Contributors

amarrella avatar amesgen avatar armanbilge avatar dependabot[bot] avatar github-actions[bot] avatar gvolpe avatar hnaderi avatar kubukoz avatar rossabaker avatar rossabaker-flake-steward[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typelevel-nix's Issues

NIX_CC and scalapb, this is probably more of a nix flake question

Hi there, I'm trying to use this in a nixos/nix container and it's mostly working great except one thing. scalapb.

Scalapb has some checks to use $NIX_CC/nix-support/dynamic-linker if it's available and it's not available in nix develop Any ideas how to work around this?

coursier

Another newbie question.
In a new laptop using typelevel-nix I was able to build my code but to use metals I had to install coursier as an extra package
I'm not sure if it should be included here as it seems to be a one-time install. WDYT?

Basic questions

Hi

sorry to ask basic questions. I'm trying to learn nix and use it to setup my shell environment for some of my projects. Some use jvm 8 some 17, many scala.js thus node

I set a basic system following https://github.com/gvolpe/sbt-nix.g8 and I was a bit wondering how is this different and also trying to learn how to use typlevel-nix with nix-shell

Maybe this should better be in a Discussions section?

scala-cli doesn't compile for aarch64-darwin

Hi! Thanks again for the project!

I've tried running it on a M1 mac and it fails because scala-cli doesn't support aarch64-darwin yet (i tried compiling it from scratch to contribute directly upstream but no success so far).

Luckily x86 packages still work on the M1 (via rosetta) even if not as performantly (but this is fine for scala-cli i believe). Do you think it would be a good idea to add some logic to overlay x86_64 scala cli on aarch64 architectures? Or provide an example in the docs?

I've worked around it in my local project like this and it works, but there is probably a more robust way

let
pkgs-x86_64 = import nixpkgs {
    system = "x86_64-darwin";
};
scala-cli-overlay = final: prev: {
    scala-cli = pkgs-x86_64.scala-cli;
};
pkgs = import nixpkgs {
    inherit system;
    overlays = [ typelevel-nix.overlay scala-cli-overlay ];
};

Additional maintainer(s)

I like to have at least two maintainers on Typelevel projects. This one shouldn't be too burdensome. Volunteers?

Should we default to temurin?

http4s is now publishing with Temurin installed via Nix. Temurin is what sbt-typelevel uses by default. "jdk8" and "jdk17" are still Zulu in nixpkgs.

Basically, do we want to stick to Typelevel defaults or Nixpkgs defaults here where the twain meet?

jre used by scala-cli

{ package = pkgs.scala-cli; }

I am new to Nix and Scala, so please excuse me if I got this wrong.

It seems to me that the scala-cli is built without overriding the jre attribute, and thus it defaults to the one from nixpkgs, which in my case currently happens to be zulu19.30.11-ca-jdk-19.0.1. So by using this flake, I have both jdk-19 and jdk-17 installed. I couldn't think of why scala-cli cannot use jdk-17. If this isn't an oversight, I'd appreciate it if someone could help me understand why this flake needs two different jdks. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.