Stream: beginners

Topic: roc nightly and nix flake


view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 10:19):

I am trying to import roc language in a small template to get started with roc for side project in the weekend,
but I am not able to make it working.

So my current error is the following one

➜  nix-roc git:(main) nix develop
error:
       … while calling the 'derivationStrict' builtin

         at //builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'nix-shell'
         whose name attribute is located at /nix/store/z5gdynjj3h25xvdqpxski061qcvrpdik-source/pkgs/stdenv/generic/make-derivation.nix:300:7

       … while evaluating attribute 'buildInputs' of derivation 'nix-shell'

         at /nix/store/z5gdynjj3h25xvdqpxski061qcvrpdik-source/pkgs/stdenv/generic/make-derivation.nix:347:7:

          346|       depsHostHost                = lib.elemAt (lib.elemAt dependencies 1) 0;
          347|       buildInputs                 = lib.elemAt (lib.elemAt dependencies 1) 1;
             |       ^
          348|       depsTargetTarget            = lib.elemAt (lib.elemAt dependencies 2) 0;

       error: Dependency is not of a valid type: element 2 of buildInputs for nix-shell

looks like that I am importing in the wrong way the roc-lang, but I do not know how to fix the problem (maybe because I am also new with nix)
This is my flake.nix https://github.com/vincenzopalazzo/nix-roc/blob/main/flake.nix

view this post on Zulip Anton (Sep 02 2023 at 10:24):

Hi @Vincenzo Palazzo (vincenzopalazzo),
You probably want to use the nightly binary release as described here, instead of trying to make a new flake. I'm happy to help with any further questions :)

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 10:25):

I see it and this procedure works, but there is no way to get it working with flake?

view this post on Zulip Anton (Sep 02 2023 at 10:31):

You can get it to work with the flake but we don't have a release of roc on nix yet, so the release will have to be built from source which will take a couple of minutes, these are the steps:

git clone https://github.com/roc-lang/roc.git
cd roc
nix develop
cargo build --release --bin roc

Once that is done you'll be able to run roc like this ./taret/release/roc examples/helloWorld.roc

view this post on Zulip Anton (Sep 02 2023 at 10:32):

May I ask why you would like to use the flake?

view this post on Zulip Anton (Sep 02 2023 at 10:32):

nix develop will use this file btw

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 10:45):

Mh still confused sorry! It is not possible fetch roc from the tar with flake? Like I did in here https://raw.githubusercontent.com/vincenzopalazzo/nix-roc/main/flake.nix

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 10:46):

Anton said:

May I ask why you would like to use the flake?

No particular reason, I want just set up a nix conf that I will not need to do the install step anymore :)

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 10:46):

So I can boostrap the roc conf plus my conf

view this post on Zulip Anton (Sep 02 2023 at 10:49):

Oh right, I understand now. Let me give that a try.

view this post on Zulip Anton (Sep 02 2023 at 10:57):

I'm going to be afk for lunch but I will continue after.

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 11:01):

Thanks! I am trying another solution too now

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 11:01):

Have a nice lunch

view this post on Zulip Anton (Sep 02 2023 at 12:51):

I thought starting with the default.nix file was easier so I did that one first:

let
  pkgs = import <nixpkgs> {};

  rocSrc = pkgs.fetchFromGitHub {
    owner = "roc-lang";
    repo = "roc";
    rev = "default-nix-env-var";
    sha256 = "sha256-nxGT2m0ftDslsgU8I2GZ72uPvtHEBE5wN9mJNu8X5Eo=";
  };

  roc = import rocSrc {};


in pkgs.mkShell {
  buildInputs = [
    pkgs.just
    roc
  ];
}

I am using a specific branch here because I had to make a small fix that will be merged later on.
You can activate this shell using the nix-shell command. This will still build roc from source though, if you share your OS and architecture I'll see if I can make it work with a nightly release. Once that works I'll try to convert the default.nix file to a flake.nix.

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 14:30):

I am using arch linux

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 14:30):

on a x86

view this post on Zulip Anton (Sep 02 2023 at 16:07):

Alright, this works:

let
  pkgs = import <nixpkgs> {};

  # Fetch and extract the nightly tar
  rocNightlyTar = pkgs.fetchurl {
    url = "https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz";
    sha256 = "sha256-3RDVU+sMFVsThviZAI76DKn07fb8j6k8qGlBjUtby8o=";
  };

  rocNightly = pkgs.stdenv.mkDerivation {
    name = "roc";
    src = rocNightlyTar;
    installPhase = ''
      mkdir -p $out
      cp -R * $out/
    '';
  };


in pkgs.mkShell {
  buildInputs = [
    pkgs.just
  ];

  # make the `roc` command work
  shellHook = ''
    export PATH=$PATH:${rocNightly}
  '';
}

Now I'm going to try converting that to a flake

view this post on Zulip Anton (Sep 02 2023 at 17:27):

This is the flake:

{
  description = "Devshell using the roc nightly.";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    # to easily make configs for multiple architectures
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    let
      supportedSystems = [ "x86_64-linux" ];
    in
      flake-utils.lib.eachSystem supportedSystems (system:
        let
            pkgs = import nixpkgs {
              inherit system;
            };

            rocNightlyTar = pkgs.fetchurl {
                url = "https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz";
                sha256 = "sha256-3RDVU+sMFVsThviZAI76DKn07fb8j6k8qGlBjUtby8o=";
            };

            rocNightly = pkgs.stdenv.mkDerivation {
                name = "roc";
                src = rocNightlyTar;
                installPhase = ''
                mkdir -p $out
                cp -R * $out/
                '';
            };
        in
            {
                devShell = pkgs.mkShell {
                    packages = [
                        pkgs.just
                        ];

                    shellHook = ''export PATH=$PATH:${rocNightly}'';
                };
            });
}

The repl does not work with the flake however because we do some dynamic library stuff for that. This seems hard to fix, so I recommend sticking with the earlier default.nix file.

Here is a bash script to update the sha in the default.nix for when the nightly is updated:

#!/usr/bin/env bash

# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail

# URL to fetch and get the sha256 hash from
URL="https://github.com/roc-lang/roc/releases/download/nightly/roc_nightly-linux_x86_64-latest.tar.gz"

# Fetch the sha256 hash using nix-prefetch-url
SHA256=$(nix-prefetch-url --type sha256 "$URL")

# Check if we successfully got the hash
if [ -z "$SHA256" ]; then
    echo "Failed to fetch SHA256."
    exit 1
fi

# Update the sha256 in the default.nix file
# This time, we are replacing the entire sha256 value with the new one, without the "sha256-" prefix.
sed -i "s|sha256-.*\"|${SHA256}\"|" default.nix

echo "Updated sha256 in default.nix to $SHA256."

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 18:30):

Thanks is is really magic hahah

view this post on Zulip Vincenzo Palazzo (vincenzopalazzo) (Sep 02 2023 at 18:30):

I will take a look

view this post on Zulip RKB (Sep 05 2023 at 02:05):

I got the same error as Vincenzo when trying to use Roc's flake as an input to my flake.
I wanted to keep the simple inputs style that works with nix flake update and I don't mind the build time, here's what works for me:

  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    roc.url = "github:roc-lang/roc";
  };

  outputs = { self, nixpkgs, flake-utils, roc }:
    let supportedSystems = with flake-utils.lib.system; [ x86_64-linux ];

    in flake-utils.lib.eachSystem supportedSystems (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        inherit (pkgs) lib stdenv callPackage;
      in {
        devShells.default = pkgs.mkShell {
          name = "devshell";
          buildInputs = [ roc.packages.${system}.default ];
        };
      });
}

The key thing is I couldn't just use buildInputs = [ roc ], I had to use nix flake show to find buildInputs = [ roc.packages.${system}.default ];.


Last updated: Jul 06 2025 at 12:14 UTC