Set your NIX_PATH to your System Flake’s Nixpkgs for a More Predictable Nix-Shell
I came across an interesting thread on the NixOS subreddit today that helped me fix a problem that I didn’t even know I had with my NixOS system configuration.
Every now and then, I’ll try to quickly do nix-shell -p somepackage
, and
it will fail, because somepackage
couldn’t be found.
I go and check https://search.nixos.org and it’s there. Huh. Weird.
So I just go and add it to my
environment.systemPackages
to install it, and remove it if it turns out I don’t really need it.
This alternative step works because my system configuration flake is pointing
to a different version of nixpkgs than the nix-shell
command.
So what’s the solution? Let’s make nix-shell
use the same version of nixpkgs
as the system configuration flake!
{
inputs,
...
}: {
nix = {
registry = {
nixpkgs = {
flake = inputs.nixpkgs;
};
};
nixPath = [
"nixpkgs=${inputs.nixpkgs.outPath}"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
];
};
}
Of course, if you also have a nixpkgs-unstable
input in your flake and would
rather that nix-shell -p
always pulled in packages from there, you can
reference nixpkgs-unstable
here as well without forcing your whole system
configuration to track the changes on the nixpkgs
master branch.
If you have any questions you can reach out to me on Twitter and Mastodon.
If you’re interested in what I read to come up with solutions like this one, you can subscribe to my Software Development RSS feed.
If you’d like to watch me writing code while explaining what I’m doing, you can also subscribe to my YouTube channel.
On 11/14/2023 I was impacted by large scale layoffs at my previous employer. I am currently looking for work. I am an experienced SRE with a strong passion for developer enablement, as well as an accomplished open-source developer with a portfolio of popular Rust and Go projects on GitHub. Please reach out if you are hiring for a role that you think I’d be a good fit for.
If you found this content valuable or if you are a happy user of
komorebi
, please consider sponsoring me
on GitHub or tipping me on
Ko-fi to help me through this uncertain period.