Trying to add unstable release Error:Flake - dose not provide attribute

As a new user to nixos(stable), i am trying to build my system slowly step by step(with the help of playlist). At a point i wanted to try nh (nix helper) which is unstable. So i tried to follow as per one of the video which says how to make use of both stable and unstable releases together.

while adding unstable release to the flakes.nix, i faced some variable error and somehow rectified those. But later landed on the flake does not provide attribute error.

Here goes my flake.nix

{
  description = "Initial Flake";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-23.11";
    nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";

    home-manager.url = "github:nix-community/home-manager/release-23.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { self, nixpkgs, nixpkgs-unstable, home-manager, ... }:
    let
      systemSettings = {
        system = "x86_64-linux";
        hostname = "nixos";
      };
      userSettings = {
        username = "mainuser";
        dotfilesDir = "~/.dotfiles";
      };

      system = systemSettings.system;
      lib = nixpkgs.lib;
      pkgs = nixpkgs.legacyPackages.${systemSettings.system};
      pkgs-unstable = nixpkgs-unstable.legacyPackages.${systemSettings.system};

    in {
    nixosConfigurations = {
      system = lib.nixosSystem {
        system = systemSettings.system;
        modules = [ ./configuration.nix ];
        specialArgs = {
          inherit systemSettings;
          inherit userSettings;
          inherit pkgs-unstable;
        };
      };
    };
    homeConfigurations = {
      userSettings.username = home-manager.lib.homeManagerConfiguration {
        inherit pkgs;
        modules = [ ./home.nix ];
        extraSpecialArgs = {
          inherit systemSettings;
          inherit userSettings;
          inherit pkgs-unstable;
        };
      };
    };
  };
}

before adding unstable releases to the flakes, my nixosConfigurations looked like this

nixosConfigurations = {
      systemSettings.hostname = lib.nixosSystem {
        inherit system
        ...

but this was throwing error at the lib variable, so i blindly copied what his repo had, i.e, i had to add system variable as his, removed inherit system and add like so

system = systemSettings.system;
in {
nixosConfigurations = {
      system = lib.nixosSystem {
        system = systemSettings.system;
        ...

and continue with nix flake update. No error till here. when i try to sudo nixos-rebuild switch --flake . im getting

error: flake 'path:/home/mainuser/.dotfiles' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."nixos".config.system.build.nixos-rebuild' or 'nixosConfigurations."nixos".config.system.build.nixos-rebuild'

From the error and tries by reverting, i understood, i have introduced specialArgs and extraSpecialArgs along with the nixosConfigurations in a wrong way. Can anyone please help me in fixing this.

my nix flake show . output is

path:/home/mainuser/.dotfiles?lastModified=1715199342&narHash=sha256-nl4HQrlgYowfVtLR%2BqvQjq2bsWIpKOP93sKPob1Wr/o%3D
├───homeConfigurations: unknown
└───nixosConfigurations
    └───system: NixOS configuration

Just for the context, this is my configuration.nix (i have removed unnecessary parts of the config and included which ever part i have changed w.r.t adding arguments, unstable releases and its packages.

{ config, pkgs, pkgs-unstable, systemSettings, userSettings, ... }:

{
  imports =
    [ ./hardware-configuration.nix ];

  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # Session Variables  <===== introduced this for the sake of nh 
  environment.sessionVariables = {
    FLAKE = "/home/${userSettings.username}/.dotfiles";
  };

  networking.hostName = systemSettings.hostname; <===== earlier i was using username directly

  # Flakes
  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.${userSettings.username} = {
    isNormalUser = true;
    description = userSettings.username;
    extraGroups = [ "networkmanager" "wheel" ];
    packages = with pkgs; [
      firefox
      kate
    ];
  };

  nixpkgs.config.allowUnfree = true;

  environment.systemPackages =
  (with pkgs; [
    # list of unstable packages
    vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
    wget
    git
    neofetch
    libreoffice
    unzip
    unrar
    curl
    gittyup
    jre_minimal
    # nix helpers
    nix-output-monitor
    nvd
    # Media
    vlc
    gimp
    gaphor
  ])
  ++
  (with pkgs-unstable; [
    # list of unstable packages
    # nix helpers
    nh
  ]);


  fonts.packages = with pkgs; [
    nerdfonts
  ];
  system.stateVersion = "23.05"; # Did you read the comment?
}

nixos-rebuild will try to build the NixOS configuration attribute named nixos by default, yours is system. Either replace system with nixos or add --flake .#system when calling nixos-rebuild.

It works :face_holding_back_tears:. Earlier i kept my nixosConfigurations as

nixosConfigurations = {
      systemSettings.hostname = lib.nixosSystem {
        inherit system
        ...

and i was using the command sudo nixos-rebuild --flake .

So changing this line to system = lib.nixosSystem, from now on i should be using the rebuild cmd as sudo nixos-rebuild --flake . #system , right ?

  nixosConfigurations = {
-  systemSettings.hostname = lib.nixosSystem {
+  ${systemSettings.hostname} = lib.nixosSystem {

This should fix your original issue, then you can use nixos-rebuild without --flake .#system.

1 Like

Okay, now i understand what system actually meant. thanks for the explanation @FedericoSchonborn

@FedericoSchonborn: Im facing the same issue in the home-manager now.
earlier i used to give the command home-manager switch --flake . and now i tried to give home-manager switch --flake .#system

it throws

Home Manager not found at /nix/store/9613fxrf433y10fz18ccixj4zpmdibzq-source.
error: flake 'path:/home/arvindh/.dotfiles' does not provide attribute 'packages.x86_64-linux.homeConfigurations."sy
stem".activationPackage', 'legacyPackages.x86_64-linux.homeConfigurations."system".activationPackage' or 'homeConfig
urations."system".activationPackage'

This is my home.nix

{ config, pkgs, systemSettings, userSettings,... }:
{
    imports = [
    ./sh/sh.nix
  ];

 
#   home.username = "mainuser";
  home.username = userSettings.username;
#   home.homeDirectory = "/home/mainuser";
  home.homeDirectory = "/home/${userSettings.username}";

  home.stateVersion = "23.05"; # Please read the comment before changing.

  home.packages = with pkgs; [
    hello
    emacs
    tmux
    htop
  ];

  home.file = {
  };
  
  home.file.".p10k.zsh" = {
    source = ./p10k/.p10k.zsh;
    executable = true;
  };

  home.sessionVariables = {
    # EDITOR = "emacs";
  };

  programs.home-manager.enable = true;
}

What am i missing now ?