1

I'm trying to use Nix on Ubuntu 16.04.

After setup, I try to build the following expression:

let
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;
in rec {
  scalaEnv = stdenv.mkDerivation rec {
    name = "scala-env";
    shellHook = ''
    alias cls=clear
    '';
    CLANG_PATH = pkgs.clang + "/bin/clang";
    CLANGPP_PATH = pkgs.clang + "/bin/clang++";
    buildInputs = with pkgs; [
      stdenv
      sbt
      openjdk
      boehmgc
      libunwind
      re2
      clang
      zlib
      ammonite
    ];
  };
} 

But this ends in the error:

*** Downloading ‘https://cache.nixos.org/nar/022mrfa98hxccsn9znr9z9s7sh3kfc5wzvgfx45x5drcz9wq3wyv.nar.xz’ to ‘/nix/store/y1scdckyi7ij30771rl1pdq4s9gj683a-sbt-1.0.1’...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 50.0M  100 50.0M    0     0  12.5M      0  0:00:04  0:00:04 --:--:-- 10.7M

building path(s) ‘/nix/store/9xykkj5z6szrwamji3gshylxca092nv9-scala-env’
unpacking sources
variable $src or $srcs should point to the source
builder for ‘/nix/store/wg0kd6z5kik46xza5xsdqw4yf10ifksv-scala-env.drv’ failed with exit code 1
error: build of ‘/nix/store/wg0kd6z5kik46xza5xsdqw4yf10ifksv-scala-env.drv’ failed
The command '/bin/sh -c $nixenv && nix-build scala-default.nix -A scalaEnv' returned a non-zero code: 100

Note that /bin/sh -c $nixenv is just trying to set up the nix environment - I can post more details if that is helpful.

I've built a very similar expression in the past on another system - I'm not sure what could be going wrong on this system - how might one go about debugging this?

bbarker
  • 7,931
  • 5
  • 30
  • 44

2 Answers2

1

You should look at the error message:

variable $src or $srcs should point to the source

You could probably search for that error message in the nixpkgs repository to find exactly where it was generated.

What is happening is that you did not supply a builder shell script, so nixpkgs is just trying to guess how to build your project based on your other mkDerivation arguments. But it can't guess how to build your thing because you didn't give it some source files to compile. I suggest you write something like builder = ./builder.sh to add a builder script, and then in that file put the commands for building your software.

David Grayson
  • 71,301
  • 23
  • 136
  • 171
1

In Nix there are two derivation functions that you need to consider when building packages:

  1. derivation which is provided by Nix.
  2. stdenv.mkDerivation which is provided by Nix Packages Collection.

mkDerivation uses derivation under the hood, so when using mkDerivation it's important to understand how they both work. For your particular issue, the thing is that derivation has three required inputs:

There must be an attribute named system whose value must be a string specifying a Nix platform identifier, such as "i686-linux" or "powerpc-darwin"

There must be an attribute named name whose value must be a string. This is used as a symbolic name for the package by nix-env, and it is appended to the output paths of the derivation.

There must be an attribute named builder that identifies the program that is executed to perform the build. It can be either a derivation or a source (a local file reference, e.g., ./builder.sh).

Now, mkDerivation takes care of two of those required inputs by providing the system and builder attributes; which of course, you can override. However, because mkDerivation provides it's own builder, which is designed to build ./configure, make, make install-based packages, it has it's own set of requirements. Namely, that you provide a src attribute which points to your source code. Typically, fetchurl is used to provide the src attribute, but a Nix path (not a string) to the source should work too.

If the default builder provided by mkDerivation is not what you need, then you'll need to write your own. On the other hand, if it does provide what you need, then you just need to ensure you provide the inputs that it requires.

You can read more about derivation and mkDerivation here and here, respectively.

Emmanuel Rosa
  • 9,167
  • 2
  • 10
  • 18
  • Thanks, I read the related documentation, though I'm still not sure how to get it to locate my build script: I added a dummy script that returns 0: `builder = "./scala-build.sh";` but now get `./scala-build.sh: No such file or directory` when I run `nix-build ./scala-default.nix` when I have `scala-build.sh` in the same directory as `scala-default.nix` – bbarker Sep 07 '17 at 15:46
  • 1
    I guess this was sort of a different question so I added it here: https://stackoverflow.com/questions/46116114/where-should-the-builder-script-be-located-for-a-stand-alone-nix-expression Anyway, the good news is that I can seemingly get what I need so far with `nix-shell`. – bbarker Sep 08 '17 at 12:36