feat: nix module's default parameters & specialArgs

This commit is contained in:
Ryan Yin
2023-10-04 15:00:55 +08:00
parent d41370c05b
commit 7918e3d88e
2 changed files with 24 additions and 7 deletions

View File

@ -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.