Should I need to set nixos-config after switching to flakes, or is this a bug?

Hi, I wanted to use nh, but it only supports flake based systems, so I switched to using flakes by putting this in /etc/nixos/flake.nix

{
  description = "A simple NixOS flake";

  inputs = {
    # NixOS official package source, using the nixos-24.05 branch here
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
  };

  outputs = { self, nixpkgs, ... }@inputs: {
    # nixosConfigurations.hostname
    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        # Import the previous configuration.nix we used,
        # so the old configuration file still takes effect
        ./configuration.nix
      ];
    };
  };
}

It seemed to work, but after I eventually rebooted, it would fail like this when I try to rebuild:

$  sudo nixos-rebuild switch --impure
building the system configuration...
error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'nixos-system-nixos-add_uv'
         whose name attribute is located at /nix/store/l2v78vdd33hvyyy33w9zih2ss60k2yms-source/pkgs/stdenv/generic/make-derivation.nix:333:7

       … while evaluating attribute 'buildCommand' of derivation 'nixos-system-nixos-add_uv'

         at /nix/store/l2v78vdd33hvyyy33w9zih2ss60k2yms-source/nixos/modules/system/activation/top-level.nix:53:5:

           52|     passAsFile = [ "extraDependencies" ];
           53|     buildCommand = systemBuilder;
             |     ^
           54|

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

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

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

But it works if I specify nixos-config: sudo nixos-rebuild switch -I nixos-config=/etc/nixos/configuration.nix --impure
Checking with set I see that NIX_PATH used to be this:

NIX_PATH=nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels

Now it is this:

NIX_PATH=/home/alister/.nix-defexpr/channels:nixpkgs=flake:nixpkgs:/nix/var/nix/profiles/per-user/root/channels

If I set it manually like this I can even rebuild using nh:

NIX_PATH=/home/alister/.nix-defexpr/channels:nixpkgs=flake:nixpkgs:/nix/var/nix/profiles/per-user/root/channels:nixos-config=/etc/nixos/configuration.nix

I presume that nh truly is building from the flake, because if I move configuration.nix somewhere else and update the flake, then this still gives me a working system:

sudo touch /etc/nixos/configuration.nix
sudo nixos-rebuild switch -I nixos-config=/etc/nixos/configuration.nix --impure

So it seems like there is a check somewhere for nixos-config, even though it is actually not otherwise needed for a flakes build.
Is this a bug?
If not, can anybody advise on whether it all looks and behaves as you would expect?

  • Does my NIX_PATH look right? Several things about it look wrong to me, but I don’t understand what would be causing them to be wrong.
  • Should I be adding something to my configuration to set nixos-config? Obviously I can resolve my problem by doing that, but I’d prefer not to blindly implement a workaround if something is broken. I haven’t seen this mentioned in the tutorials where I’ve seen this flake.nix, or by other people using it on their systems, and my understanding is that if flakes are enabled nixos-rebuild is supposed to look for /etc/nixos/flake.nix by default, and if it does not exist, look for /etc/nixos/configuration.nix. It doesn’t make sense to me that I would need to set nixos-config to /etc/nixos/configuration.nix if I am using /etc/nixos/flake.nix, but not if I am using /etc/nixos/configuration.nix directly…
  • If so, what would be the recommended thing to add, and where?
  • Or should I be looking for some other problem something else?

Thanks.

Should I need to set nixos-config after switching to flakes

Nope. And NIX_PATH is irrelevant too for flake-based commands. Just make sure you’re not using --impure… which is unfortunately what you did above

Also drop this line:

OK, thanks.

So to be able to remove --impure I have to:

  • change an inclusion of /etc/nixos/hardware-configuration.nix to ./hardware-configuration.nix (don’t know why I had an absolute path in the first place)
  • stop using system.copySystemConfiguration (ok, it wasn’t useful as it didn’t work as I expected anyway)
  • fix causes of
error: in pure evaluation mode, 'fetchTarball' requires a 'sha256' argument

(i.e. change how I’m using nur and how I’m installing nix-alien)

  • fix other causes of:
error: access to absolute path ...  is forbidden in pure eval mode (use '--impure' to override)

(stop using callPackage to install something while waiting and hoping that a pull request on nixpkgs might one day be accepted).

Setting NIX_PATH and continuing to use --impure is certainly the easy way to go :slight_smile:

You should fix those things. Or don’t use flakes at that point, you’re not really using flakes anyway since you’re setting nixos-config which is ignoring the flake.nix.

Unnecessary with flakes as the flake is copied to the store before eval.

You don’t need NUR with flakes, NUR is a pre-flakes way to expose some common projects.
Most of said projects are also flakes that you can use directly if needed.

No? You just have to use a valid path. The error tells you as much.

1 Like

NH 4.0 supports a regular configuration: nh os switch -f '<nixpkgs/nixos> -- -I nixos-config=/etc/nixos/configuration.nix, though I haven’t set up defaults to make this more ergonomic to use out of the box.

Btw, please use the github issues or ping me on discourse for issues with nh, because I discovered this post by accident.

You don’t need NUR with flakes, NUR is a pre-flakes way to expose some common projects.

Thanks, that’s great news. I saw you’d closed a couple of tickets about using NH without flakes, saying it wasn’t supported, so I thought that was the end of the matter. BTW it actually does already work fine with an impure system and NIX_PATH set, but it will be cleaner to get rid of the flake.

Most of said projects are also flakes that you can use directly if needed.

Well, not the ones I’m using, it seems.

No? You just have to use a valid path. The error tells you as much.

I seemed to just get the error message about not using an absolute path, even when I used a relative one, which didn’t seem very helpful.

Still don’t need NUR.

Well I can’t help if you haven’t provided the code and its context :slight_smile: