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!