mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2025-08-16 07:37:51 +02:00
feat: nix module's default parameters & specialArgs
This commit is contained in:
@ -125,7 +125,7 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
#
|
||||
# A Nix Module can be an attribute set, or a function that
|
||||
# returns an attribute set. By default, if a Nix Module is a
|
||||
# function, this function can only have the following parameters:
|
||||
# function, this function have the following default parameters:
|
||||
#
|
||||
# lib: the nixpkgs function library, which provides many
|
||||
# useful functions for operating Nix expressions:
|
||||
@ -143,11 +143,12 @@ Note that the copied template cannot be used directly. You need to modify it to
|
||||
# this parameter is rarely used,
|
||||
# you can ignore it for now.
|
||||
#
|
||||
# Only these parameters can be passed by default.
|
||||
# If you need to pass other parameters,
|
||||
# The default parameters mentioned above are automatically generated by Nixpkgs.
|
||||
# However, if you need to pass other non-default parameters to the submodules,
|
||||
# you'll have to manually configure these parameters using `specialArgs`.
|
||||
# you must use `specialArgs` by uncomment the following line:
|
||||
#
|
||||
# specialArgs = {...} # pass custom arguments into all sub module.
|
||||
# specialArgs = {...}; # pass custom arguments into all sub module.
|
||||
modules = [
|
||||
# Import the configuration.nix here, so that the
|
||||
# old configuration file can still take effect.
|
||||
@ -164,6 +165,14 @@ We defined a NixOS system called `nixos-test` with a configuration file at `./co
|
||||
|
||||
To apply the configuration to a system with hostname `nixos-test`, run `sudo nixos-rebuild switch --flake /etc/nixos#nixos-test`. No changes will be made to the system because we imported the old configuration file in `/etc/nixos/flake.nix`, so the actual state we declared remains unchanged.
|
||||
|
||||
The comments in the above code are already quite detailed, but let's emphasize a few points here:
|
||||
|
||||
1. Default parameters like `lib`, `pkgs`, `config`, and others are automatically generated by Nixpkgs and can be automatically injected into submodules without the need for additional declarations here.
|
||||
|
||||
2. In `specialArgs = {...};`, the content of the attribute set is omitted here. Its contents are automatically injected into submodules through name matching.
|
||||
|
||||
1. A common usage, for instance, is to directly write `specialArgs = inputs;`, enabling all data sources from the `inputs` attribute set to be used in the submodules.
|
||||
|
||||
## Managing System Packages with Flakes
|
||||
|
||||
After the switch, we can manage the system using Flakes. One common requirement is installing packages. We have previously seen how to install packages using `environment.systemPackages` from the official `nixpkgs` repository.
|
||||
|
Reference in New Issue
Block a user