Using nix-shell with flakes over channels

hi there,
i’ve been using flakes instead of channels, and was wondering if i could use nix commands (among which nix-shell) in such a way as well, but so far haven’t quite figured this out yet:

$ nix-shell -p nodePackages.npm
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist, ignoring
error:
       … <borked>

         at «none»:0: (source not available)

       … while calling the 'import' builtin

         at «string»:1:18:

            1| {...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (npm) ]; } ""
             |                  ^

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

       at «none»:0: (source not available)

now, going the well-trodden path, it works, as expected:

$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --update
$ echo $NIX_PATH
/home/kiara/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
$ ls /home/kiara/.nix-defexpr/channels/nixpkgs
CONTRIBUTING.md  COPYING  default.nix  doc  flake.nix  lib  maintainers  nixos  pkgs  README.md  svn-revision
$ nix-shell -p nodePackages.npm

to prevent these from going out of sync, if my flake.nix references a nixpkgs rev, could i have nix commands use that instead?

You can do this: Do flakes also set the system channel? - #16 by peterhoeg

There’s some indirection there to avoid problems in edge cases, but the gist is just to add the flake input to NIX_PATH under its intended channel name.

1 Like