Getting error trying to pass an argument/attribute to Home-Manager

Hello everyone!

I started using NixOS on my desktop PC a month ago and I love its declarative approach. It is beautiful and elegant. A big thank you to all who are involved in creating this amazing OS! I am not a developer and use my computer for light tasks like creating text documents and spreadsheets, browsing the web, listening to music etc. Learning Nix without any kind of background in software development isn’t easy but I am willing to invest my spare time and overcome the obstacles step by step as they occur. I’ve already found many useful resources scattered across the web that, so far, helped me answer most of the questions that arose during my Nix journey. And I have to admit, it’s also a lot of fun learning so many new and awesome things about it.

Unfortunately, here’s an issue I wasn’t able to figure out on my own yet. (I already tried to search this forum but was unable to find an answer. I’m sorry if I missed something.) Soon after my initial setup I enabled Flakes and added Home-Manager as a system module. Now I would like to pass some attributes/arguments to Home-Manager but when rebuilding the system I always get the error “attribute missing”. To be more specific: I’d like to define my username globally in flake.nix and pass it to configuration.nix and home.nix as something like ${username} . In configuration.nix my setup works as expected but in home.nix it doesn’t.

Here are the relevant bits of my configuration. That’s my flake.nix:

{
outputs = { self, nixpkgs, nixpkgs-unstable, home-manager, ... }:
  let
    /* ---- SYSTEM SETTINGS ---- */
      system = "x86_64-linux";
      hostname = "NixOS-UM350";
    
    /* ---- USER SETTINGS ---- */
      username = "myName";
      desktop = "gnome"; # choose "gnome" or "kde"
    
    /* ---- PKGS ---- */
      pkgs = import nixpkgs {
        inherit system;
        config = { allowUnfree = true; };
      };
      pkgs-unstable = import nixpkgs-unstable {
        inherit system;
        config = { allowUnfree = true; };
      };
  in {

    homeConfigurations = {
      ${username} = home-manager.lib.homeManagerConfiguration {
        extraSpecialArgs = { inherit username hostname desktop pkgs pkgs-unstable; };
        modules = [ ./home.nix ];
      };
    };

    nixosConfigurations = {
      "${hostname}" = nixpkgs.lib.nixosSystem {
        inherit system;
        specialArgs = { inherit username hostname desktop pkgs pkgs-unstable; };
        modules = [
          ./configuration.nix
          
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.${username} = import ./home.nix;
          }
        ];
      };
    };
  };

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

And here’s what my home.nix looks like:

{ config, pkgs, pkgs-unstable, username, ... }:
{
  /* ---- Home-Manager ---- */
  programs.home-manager.enable = true;

  /* ---- Username ---- */
  home.username = ${username};
  home.homeDirectory = "/home/${username}";

  /* ---- User-level Packages ---- */
  home.packages = with pkgs; [
    signal-desktop
    qalculate-gtk
    geogebra6
    texlive.combined.scheme-small
    setzer
    element-desktop
    gimp
    darktable
  ];
  
  home.stateVersion = "23.05";
}

Now, if I try to sudo nixos-rebuild switch --flake . then I get the following error:

building the system configuration...
error: attribute 'username' missing

       at /nix/store/3s69yxbbl116zwga3i6cy7prplywq0bn-source/lib/modules.nix:512:28:

          511|         builtins.addErrorContext (context name)
          512|           (args.${name} or config._module.args.${name})
             |                            ^
          513|       ) (lib.functionArgs f);
(use '--show-trace' to show detailed location information)

As far as I understand it, using
extraSpecialArgs = { inherit username; };
in the homeConfigurations section of flake.nix should pass it to Home-Manager. But I still get the error. So, I am at a loss here.

I’d much appreciate any help that points me in the right direction. Thank you in advance and sorry for the wall of text!

1 Like

Hi, Welcome to Nix community

as my name says i’m newbie so it’s possible that my solution is wrong

You are using home-manager both as standalone and nix module but only one of them is enough. in standalone you are passing extraSpecialArgs and it should be OK but what nixos-rebuild switch use is nix module one. you can change it like this

 modules = [
          ./configuration.nix
          
          home-manager.nixosModules.home-manager
          {
            #edit this line and add what you want to pass to home.nix
            extraSpecialArgs = { inherit username; }; 
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.${username} = import ./home.nix;
          }
        ];

and because you have home manager as nix module you can remove the standalone part (below code)

    homeConfigurations = {
      ${username} = home-manager.lib.homeManagerConfiguration {
        extraSpecialArgs = { inherit username hostname desktop pkgs pkgs-unstable; };
        modules = [ ./home.nix ];
      };
    };
3 Likes

YES! You are totally right. Without realizing it, I mixed the standalone and module variant in my configuration. Thanks a lot for pointing this out!

I had to tweak your suggestion slightly. I just mention it, so that I’ll be able to find it if I need to look it up again in the future. After getting an error about an unknown option, I checked the relevant section of the Home Manager manual and the correct line should read

home-manager.extraSpecialArgs = { inherit username; };

Now, it works exactly as expected. Thank you very much for your help, it’s much appreciated!

1 Like