Trying to abstract with nix-shell

Abstracting with nix-shell seems very difficult. Here’s an example of what I mean.

In ~/.config/nixpkgs/overlays/env.nix I have:

self: super:

let
  candelaPkgs = import (fetchTarball {
    url = https://github.com/NixOS/nixpkgs-channels/archive/14a9ca27e69e33ac8ffb708de08883f8079f954a.tar.gz;
    sha256 = "1grsq8mcpl88v6kz8dp0vsybr0wzfg4pvhamj42dpd3vgr93l2ib";
  }) {};
in {
  env = {
    candela = super.mkShell {
      name = "candela";

      buildInputs = with candelaPkgs; [
        cairo
        nodejs-9_x
        nodePackages_8_x.node-gyp
        nodePackages.lerna
        pkgconfig
      ];
    };
  };
}

nix-shell -p env.candela gives me this error:

these derivations will be built:
  /nix/store/jnd4q1camnxqyf2gh05igqh4z3wk5kmn-candela.drv
building '/nix/store/jnd4q1camnxqyf2gh05igqh4z3wk5kmn-candela.drv'...
nobuildPhase

This derivation is not meant to be built, aborting

builder for '/nix/store/jnd4q1camnxqyf2gh05igqh4z3wk5kmn-candela.drv' failed with exit code 1
error: build of '/nix/store/jnd4q1camnxqyf2gh05igqh4z3wk5kmn-candela.drv' failed

But if I now create a file dummy.nix:

{ pkgs ? import <nixpkgs> {} }:

pkgs.env.candela

and I do nix-shell dummy.nix then I get a shell environment for Candela.

So when I say “abstracting seems difficult” what I mean is, creating the overlay is almost enough to get this effect of a “library” of shell environments, but I still have to do this artificial-seeming step of creating a barebones nix file with which to invoke nix-shell.

I could write a shell utility that creates that nix file in /tmp then runs nix-shell but it feels like I’m missing a native way to do it.

And I’ll just add: I realize my example here would be better off in a shell.nix file in a GitHub repo, but I’m just using it as an example of the pattern; there may be other shell environments I want to compose that are not project-specific, and I’m trying to learn the mechanics of how to make this work in a reusable or persistent way using the Nix tools.

Thanks!

roni

I think you want nix-shell '<nixpkgs>' -A env.candela :slight_smile:

With a hypothetical future nix shell command that has a UI consistent with the other new nix commands, nix shell nixpkgs.env.candela

2 Likes

I think you want nix-shell '<nixpkgs>' -A env.candela :slight_smile:

Excellent! Thanks for the tip, this works beautifully.

With a hypothetical future nix shell command that has a UI consistent with the other new nix commands, nix shell nixpkgs.env.candela

+1 for this calling sequence.

Just how hypothetical is this nix shell usage?

roni

I think nix run is what you looking for.

No, nix run is sort of like nix-shell -p, but doesn’t provide an environment for building software at all.