Supported way to set NIX_PATH

This is likely to be me once again missing it in the docs (and if so, I apologize), but:

What is the supported way to set NIX_PATH to a custom location? I like to use a clone of the nixpkgs repo rather than a channel. Is there something in a nix.conf or config.nix that will do this?

So far I’m just modifying my /etc/bashrc to clobber the already-set NIX_PATH to point to /nix.

I am on MacOS if that is important.

1 Like

There are several methods. All depending on the scope that you want.

  • modifying bashrc is certainly the global way to do this since you are not using NixOS. Otherwise you can set the nix path in the configuration.
  • you can apply it locally with nix-shell

For the nix-shell If you want to fix nixpkgs to a specific commit

let

  commitRev = "4df3426f5a5e78cef4835897a43abd9e2a092b74"; # 18.03 on 2018-08-22

  nixpkgs = builtins.fetchTarball {
    url = "https://github.com/NixOS/nixpkgs-channels/archive/${commitRev}.tar.gz";
    sha256 = "05k5mssiqxffxi45mss9wjns6k76i248rpasa48akdcriry1mp63";
  };

  pkgs = import nixpkgs { config = {}; };

in

pkgs.mkShell {

  buildInputs = [ pkgs.nixops ];

  shellHook = ''
    export NIX_PATH="nixpkgs=${nixpkgs}:."
  '';

}

Otherwise you can use a local directory that you have on your machine

let
  pkgs = import <nixpkgs> { config = {}};
in
  pkgs.mkShell { 
     shellHook = ''
       export NIX_PATH="nixpkgs=<pathtonixpkgsdirectory>"
     '';
  }

I think the way you’re doing it (NIX_PATH environment variable) is already the supported best practice.

Alternatively, if you don’t want to have to set anything in your env, most commands take a -I flag to set the nix path:

nix build -I nixpkgs=/path/to/nixpkgs <args>

Thanks for the info, guys! It sounds like what I’m doing is about as correct as it gets. I just wanted to make sure there was no other source of truth that any of the tools might have been consulting that would lead to some sort of conflict/inconsistency.

@costrouc, I didn’t know about pkgs.mkShell, thanks! That looks like an interesting (although, undocumented?) feature. With nix-shell so far, I’ve just been using regular old derivations, which seems to work ok for me. mkShell sounds like it’s designed for this though… Can you tell me how it is different?

@qoli I am somewhat new to nix so take what I say with a grain of salt (been using for 2 months). Using pkgs.mkShell is the correct way to instantiate a nix-shell. If you look at the definition https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/mkshell/default.nix#L31-L45 you will see that it is a normal mkDerivation that will not allow building ( see nobuildPhase ). I will admit that it is not documented well enough and I too originally used mkDerivation. mkShell has two useful variables buildInputs: specify all packages that should be made available in environment, shellHook: run arbitrary bash script before entering environment. As an example

let
  pkgs = import <nixpkgs> { config = {}};
in
  pkgs.mkShell { 
     buildInputs = [ pkgs.hello ];
     shellHook = ''
       export NIX_PATH="nixpkgs=<pathtonixpkgsdirectory>"
       export LD_LIBRARY_PATH="${pkgs.hello}/lib"
       source .env
     '';
  }

nix-shell is really powerful. Using nix-shell you can create a well defined environment that should be the same for everyone.

For the record, there is a smallish documentation segment about mkShell intended to be added to the Nixpkgs manual, but it looks like something broke the integration of .md files into the documentation, or it never fully worked.

It’s not necessary to include nixops here, but you can follow that pattern to make pkgs available inside your shell.

You can also set it in nixos globally via configuration.nix: https://github.com/Mic92/dotfiles/blob/master/nixos/configuration.nix#L72

You can also set it in nixos globally via configuration.nix

Yeah! I use that on my NixOS machines. I guess I was asking if there was a direct equivalent outside of NixOS (it sounds like there isn’t).

Still, hacking on bashrc seems to work just fine :slight_smile:

I put

source ~/.nix-profile/etc/profile.d/nix.sh

in my .bashrc file, and it solved this problem…

Please let me know if I did the right thing or if I did a bad thing… Thanks!

2 months later, still have this (ubuntu 16.04) and no problems so far.