New to Nix & NixOS
When using a flake for my NixOS system’s configuration, is there any difference between using specialArgs
this way
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = { nixpkgs, ... } @ inputs : {
nixosConfigurations.hostname = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
specialArgs = inputs;
modules = [ ./configuration.nix ];
};
};
}
as opposed to this way?
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
outputs = { nixpkgs, ... } @ inputs : {
nixosConfigurations.hostname = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
specialArgs = { inherit inputs; };
modules = [ ./configuration.nix ];
};
};
}
In know the latter is some syntaxic sugar and is equivalent to the following
specialArgs = { inputs = inputs; };
I tried all three syntax, and from what I’ve seen, they all seem to work
I’ve read plenty of (official & non-official) documentations, blog posts, and watched countless videos and talks but I’m still a bit lost. If there’s a good documentation I’ve missed, I’m highly interested as well
Thanks
2 Likes
From what i can understand from the source , the difference should be that specialArgs=inputs;
would pass the individual members as module args while specialArgs={inherit inputs;};
passes inputs
itself.
2 Likes
That would be my understanding as well but they both seem to work
I need to do more testing to better understand this.
Thanks for the link to the source
I may have found a hint in @djacu ’s excellent Function Arguments - NixOS Modules Lessons
Function Arguments
When you define a module as a function like we did previously, there are certain arguments that are automatically provided.
You do not have to explicitely put them in the function signature as we have the ellipsis ...
to manage any arguments we do not use.
All modules are passed the following arguments:
lib
: The nixpkgs library.
config
: The results of all options after merging the values from all modules together.
options
: The options declared in all modules.
specialArgs
: An attribute set of extra arguments to be passed to the module functions.
All attributes of specialArgs
.
NOTE
The fact that all the attributes of specialArgs
are automatically provided means you don’t need to add specialArgs
to the module function signature if we want access to specialArgs.thing
.
We can just add thing
to the function signature and use it directly.
It looks like specialArgs
is always passed as an argument as well as all its attributes!
Which seems to be what the line @zimward pointed at do
({ inherit lib options config specialArgs; } // specialArgs)
Which means, pass lib
, options
, config
, specialArgs
and “merge” with all the attributes of specialArgs
(using the //
operator).
2 Likes