Does not provide attribute 'packages.x86_64-linux.nixosConfigurations."x1-2021"

I’m running into an issue with nixos-rebuild. When I run sudo nixos-rebuild --flake .#x1-2021 switch, I see the following error

error: flake 'git+file:///home/ben/workspace/areas/system-management/dotfiles' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."x1-2021".config.system.build.nixos-rebuild', 'legacyPackages.x86_64-linux.nixosConfigurations."x1-2021".config.system.build.nixos-rebuild' or 'nixosConfigurations."x1-2021".config.system.build.nixos-rebuild'

This is my flake.nix:

{
  description = "My NixOS configuration flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    nur.url = "github:nix-community/NUR";
    impermanence.url = "github:nix-community/impermanence";

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

    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = inputs@{ self, nixpkgs, nixpkgs-unstable, impermanence, home-manager, nur, flake-utils }:
    flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
    let
      pkgs = import nixpkgs {
        inherit system;
        config.allowUnfree = true;
      };
      pkgsUnstable = import nixpkgs-unstable {
        inherit system;
        config.allowUnfree = true;
      };

    in {
      nixosConfigurations = {
        x1-2021 = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [
            ./machines/x1-2021
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.ben = import ./home.nix {inherit pkgs; inherit pkgsUnstable; inherit impermanence; inherit nur;};
            }
          ];
        };
      };
    });
}

My computer’s hostname is x1-2021.

One thing I don’t understand is that when I run this command nix flake show . in the directory with my flake.nix, I see the following output

git+file:///home/ben/workspace/areas/system-management/dotfiles
└───nixosConfigurations
    └───x86_64-linux: NixOS configuration

This seems to suggest that x1-2021 is not a correct configuration name, though I have put it under nixosConfiguration in the flake.nix above.

Any help is appreciated!

I don’t think you wanna use flake-utils.lib.eachSystem for this, it’s adding an extra layer of structure for systems that doesn’t make sense for nixosConfigurations. Each NixOS configuration is already an individual machine with a particular system, so you don’t need to divide them up that way.

So, the reason I was trying to use eachSystem was because I’m seeing this error

error: attribute 'currentSystem' missing

       at /nix/store/mz6lqpbsp4h36kgahvwfrgdjzz042hpn-source/pkgs/top-level/impure.nix:17:43:

           16|   # (build, in GNU Autotools parlance) platform.
           17|   localSystem ? { system = args.system or builtins.currentSystem; }
             |                                           ^
           18|

Is there a better approach for fixing this? It works if I use the --impure flag, but I’d like to make it pure, if possible.

Note: if I remove eachSystem as suggested, my flake looks like this

{
  description = "My NixOS configuration flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    nur.url = "github:nix-community/NUR";
    impermanence.url = "github:nix-community/impermanence";

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

    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = inputs@{ self, nixpkgs, nixpkgs-unstable, impermanence, home-manager, nur, flake-utils }:
    let
      pkgs = import nixpkgs {
        config.allowUnfree = true;
      };
      pkgsUnstable = import nixpkgs-unstable {
        config.allowUnfree = true;
      };

    in {
      nixosConfigurations = {
        x1-2021 = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [
            ./machines/x1-2021
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.ben = import ./home.nix {inherit pkgs; inherit pkgsUnstable; inherit impermanence; inherit nur;};
            }
          ];
        };
      };
    };
}

I was able to find a solution. I just had to pass the system explicitly when importing nixpkgs, like this

      pkgs = import nixpkgs {
        config.allowUnfree = true;
        system = "x86_64-linux";
      };
1 Like

Ah damn, you figured it out while I was writing this. You may want to moved the system out and use inherit like I did here:

    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        config.allowUnfree = true;
      };
      pkgsUnstable = import nixpkgs-unstable {
        inherit system;
        config.allowUnfree = true;
      };

    in {
      nixosConfigurations = {
        x1-2021 = nixpkgs.lib.nixosSystem {
          inherit system;
          modules = [
            ./machines/x1-2021
            home-manager.nixosModules.home-manager

I know you’d normally (i.e. when writing a flake that others might also use as an input) should use nixpkgs.${system}.legacyPackages instead, but as yours is a system configuration that doesn’t matter and I don’t know how you’d set allowUnfree in that situation anyway.

1 Like

Ah BTW, you don’t have to write inherit separately for every attribute. You can shorten that line to this:

home-manager.users.ben = import ./home.nix { inherit pkgs pkgsUnstable impermanence nur; };
1 Like

You don’t need to set the system in the nixpkgs instance or the nixosSystem call I believe. You can set the nixpkgs.hostPlatform option in your NixOS config, and it should work: it seems to for me.

Similarly, you can configure options like allowUnfree in the NixOS config too, by setting options like nixpkgs.config.allowUnfree.

Yes, I found that now as well, see the nixpkgs.hostPlatform option docs. The nixpkgs.config docs explicitly show how to set allowUnfree as well.