Do I really need to touch all-packages.nix

I’m totally new to Nix and are now working my way through the various docs.

The manual talks about modifying all-packages.nix in order to register my derivation.

I manged to build my first derivation like this

$ nix-build default.nix
these derivations will be built:
building '/nix/store/mrvd5q77f6qxy58pj0y3xbc0fryyxgyc-myhello-1.0.0.drv'...

with very simple content

{ pkgs ? import <nixpkgs> {} }:

pkgs.stdenv.mkDerivation {

  pname = "myhello";
  version = "1.0.0";

  builder = ./;

and builder

source $stdenv/setup

mkdir -p $out/bin
cat << EOF >> $out/bin/hello
  echo "Hello World!"
chmod +x $out/bin/hello

This works as expected

$ ./result/bin/hello 
Hello World!

but nix cannot find this derivation when I query for it

$ nix-env -qa myhello
error: selector 'myhello' matches no derivations

I guess this is because I didn’t register myhello “globally”.

My goal is to build a few derivations that should (for now) only exist in my local nix installation.

How can I register this derivation so that it can be referenced and included in an env for example?

$ nix-env -i myhello
error: selector 'myhello' matches no derivations

nix-env looks for the nixpkgs in $NIX_PATH. It then looks for the myhello derivation there and I assume that it doesn’t exist. From what I understand you are placing a default.nix in some random directory. That won’t come up in nix-env, because it only looks in in the nixpkgs source directory.

What you can do is nix-env -f default.nix -i myhello, with a default.nix that lists all your packages. I recommend reading this post about setting up a private repo of nix packages: Sander van der Burg's blog: Managing private Nix packages outside the Nixpkgs tree

1 Like

Several alternate methods:

  1. you can modify all-packages.nix in a local version of nixpkgs and ensure your Nix points to that during use. (NIX_PATH env var).
  2. you can create an overlay: Overlays - NixOS Wiki
  3. you can create a new channel with only your attributes (that can depend on nixpkgs if needed)
  4. flakes: Flakes - NixOS Wiki
  5. use the attributes only locally in your project, not globally (see @Pacman99 's answer)

Great, thanks. That worked