I have an error when doing sudo nixos-rebuild switch:
error:
… while calling the 'import' builtin
at «string»:1:6:
1| with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild
| ^
… while realising the context of a path
… while calling the 'findFile' builtin
at «string»:1:13:
1| with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild
| ^
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
building Nix...
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
error: file 'nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix' was not found in the Nix search path (add it using $NIX_PATH or -I)
/tmp/nixos-rebuild.nCiLsS/nix
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
building the system configuration...
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
If i do sudo su - and then nixos-rebuild switch everything i OK, i also have a small script on fish:
#!/run/current-system/sw/bin/fish
set pkgs $argv
sed -i "22a\ $pkgs" /etc/nixos/configuration.nix
nixos-rebuild switch
and if i execute it with sudo it is working fine, i was trying to change
#!/run/current-system/sw/bin/fish
to
#!/bin/sh
and it’s outputs same errors.
What am i doing wrong?
This script isn’t in configuration.nix. I was too lazy to search more efficient way to install packages way that i want, sed is inserting new line with packages that i want to install after
You should really prefer .enable options or module specifc options in general if they’re available, adding packages to the environment.systemPackages is a last resort if all you need is the binary and there’s no additonal config needed.
Oh I see. If I were you, I would be quite tempted to pull that into a separate file to limit the blast radius of sed. It would also make it a lot easier to structurally edit it (i.e maintain the list in sorted order, only add packages if they aren’t already in the list).
Also generally agree with @waffle8946 above, try to keep that list empty
In any case, the first response I think is correct. If you want to run it with sh then you can’t use $argv that’s a fish specific thing.
I will admit that the env | prefix was me editing something and not paying attention (doesn’t change the output but still stupid), but the single quotes are important. sudo sh -c 'echo $NIX_PATH', otherwise you are just giving me the $NIX_PATH from your user env, which we know is correct.
oleg@nixos~ $ cat /etc/profile
# /etc/profile: DO NOT EDIT -- this file has been generated automatically.
# This file is read for login shells.
# Only execute this file once per shell.
if [ -n "$__ETC_PROFILE_SOURCED" ]; then return; fi
__ETC_PROFILE_SOURCED=1
# Prevent this file from being sourced by interactive non-login child shells.
export __ETC_PROFILE_DONE=1
if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
. /nix/store/9f3hpc2y4dfx1x6a1nhvmlrj1d242hqp-set-environment
fi
# Read system-wide modifications.
if test -f /etc/profile.local; then
. /etc/profile.local
fi
if [ -n "${BASH_VERSION:-}" ]; then
. /etc/bashrc
fi
The slightly weird setting is actually that your user’s env for some reason has a $NIX_PATH - the defaults are normally used and correct.
Where did you configure it to do that? This might clue us in to how you use your system; you probably have some misunderstanding somewhere.
Anyway, the underlying issue if you get that result with no $NIX_PATH is that you have no nixpkgs channel set.
If you look closely you’ll notice that your NIX_PATH is aliasing nixpkgs to nixos, so that would explain why it works for your user, you likely just need to rename your channel.
and truth be told I’d never looked at what the implementation of that is, which is… environment.sessionVariables. So I have a fully populated NIX_PATH even under root, and thought this was normal.
This is looking for something called nixpkgs, you only have something called nixos.
That line is part of the NixOS module system init code, so you need to provide a nixpkgs channel to use NixOS.
You can just re-add your channel but under the name nixpkgs with the sudo nix-channel --add command (and then delete the old nixos channel, I guess). sudo is important because it needs to be in the root user’s channels.
root@nixos~ # nixos-rebuild switch
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error:
… while calling the 'import' builtin
at «string»:1:6:
1| with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild
| ^
… while realising the context of a path
… while calling the 'findFile' builtin
at «string»:1:13:
1| with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild
| ^
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
building Nix...
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error: file 'nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix' was not found in the Nix search path (add it using $NIX_PATH or -I)
/tmp/nixos-rebuild.cQW7jA/nix
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
building the system configuration...
unpacking 'https://nixos.org/channels/nixos-25.05' into the Git cache...
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' cannot be downloaded, ignoring
warning: Nix search path entry 'https://nixos.org/channels/nixos-25.05' does not exist, ignoring
error: file 'nixpkgs/nixos' was not found in the Nix search path (add it using $NIX_PATH or -I)
root@nixos~ # nix-channel --list
home-manager https://github.com/nix-community/home-manager/archive/master.tar.gz
nixos https://nixos.org/channels/nixos-25.05
nixpkgs https://nixos.org/channels/nixos-25.05
Having a root nixos channel is the default under NixOS. And usually there is a nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos entry in the users (and roots) nix path.
So the way to fix this, is to:
ensure that the users nix path is set correctly
not using doas, as it is known to mess up the environment on NixOS, due to not adhering to the main way NixOS transports environment variables… There is even a ticket in doas because of that, but I can not find it