Creating a simple overlay

I’m trying to create an overlay with one thing in it: a simple C program called hello-amy. Here’s the error message I’m getting:

$ nix-env -i hello-amy
error: attempt to call something which is not a function but a set, at /nix/store/ln25ib694xa2ydh2vxirj89pn50wbz6y-nixpkgs-19.03pre161900.61c3169a0e1/nixpkgs/lib/fixed-points.nix:56:67

Here’s how my overlay is set up.

$ tree ~/.config/nixpkgs/overlays
/home/amy/.config/nixpkgs/overlays
├── default.nix
└── hello-amy
    ├── default.nix
    ├── simple_builder.sh
    └── simple.c

1 directory, 4 files

Here’s the top-level default.nix in the overlay. I suspect the problem is in this file because this is the part I don’t really understand. I have looked at several examples of overlays, but they are all very different from each other and a bit complicated.

$ cat ~/.config/nixpkgs/overlays/default.nix 
self: super:

{
  hello-amy = super.callPackage ./hello-amy {};
}

And here are the contents of the hello-amy subdirectory. This stuff builds just fine using nix-build default.nix.

$ cat ~/.config/nixpkgs/overlays/hello-amy/default.nix 
with (import <nixpkgs> {});
derivation {
  name = "hello-amy";
  builder = "${bash}/bin/bash";
  args = [ ./simple_builder.sh ];
  inherit gcc coreutils;
  src = ./simple.c;
  system = builtins.currentSystem;
}
$ cat ~/.config/nixpkgs/overlays/hello-amy/simple.c
#include <stdio.h>
void main() {
  puts("Hello, Amy!");
}
$ cat ~/.config/nixpkgs/overlays/hello-amy/simple_builder.sh 
export PATH="$coreutils/bin:$gcc/bin"
mkdir $out
gcc -o $out/hello-amy $src
1 Like

The problem is with your default.nix file. You need to parameterise it by the inputs.

{ bash, gcc, coreutils }:
...

The error happens because callPackage expects the file in its first argument to be a function but your default.nix is not.

So I need to put that into ~/.config/nixpkgs/overlays/hello-amy/default.nix, right? (Not ~/.config/nixpkgs/overlays/default.nix). I assume I need to rewrite it a bit, because I got another error.

$ nix-env -i hello-amy
error: infinite recursion encountered, at undefined position
$ cat /home/amy/.config/nixpkgs/overlays/hello-amy/default.nix
with (import <nixpkgs> {});

{ bash, gcc, coreutils }:
derivation {
  name = "hello-amy";
  builder = "${bash}/bin/bash";
  args = [ ./simple_builder.sh ];
  inherit gcc coreutils;
  src = ./simple.c;
  system = builtins.currentSystem;
}

You also need to remove with (import <nixpkgs> {}); then I would expect it to work.

Unfortunately, I get the same error.

$ nix-env -i hello-amy
error: infinite recursion encountered, at undefined position
$ cat /home/amy/.config/nixpkgs/overlays/hello-amy/default.nix
{ bash, gcc, coreutils }:
derivation {
  name = "hello-amy";
  builder = "${bash}/bin/bash";
  args = [ ./simple_builder.sh ];
  inherit gcc coreutils;
  src = ./simple.c;
  system = builtins.currentSystem;
}

Bump, it seems something wrong when computing the fixed point of nixpkgs, what happened here?

@mhwombat I think you should have something like this

$ cat ~/.config/nixpkgs/overlays/hello-amy/default.nix

self: super: {
  hello-amy = with super; derivation {
    name = "hello-amy";
    builder = "${bash}/bin/bash";
    args = [ ./simple_builder.sh ];
    inherit gcc coreutils;
    src = ./simple.c;
    system = builtins.currentSystem;
  };
}

and the ~/.config/nixpkgs/overlays/default.nix is not needed. But I still don’t know why the infinite recursion. Any one familiar with the overlay mechanism here?

That fixed it, thank you.