Exploring NixOS Configurations with nixos-option (with examples)
- Linux
- November 5, 2023
Introduction
NixOS is a Linux distribution built on the Nix package manager. One of the key features of NixOS is its declarative configuration model, which allows users to define their system configurations in a single file called configuration.nix
. The nixos-option
command allows users to inspect and explore the configuration options available in a NixOS system. In this article, we will explore the different use cases of the nixos-option
command with examples.
List all subkeys of a given option key
To list all subkeys of a given option key, we can use the following command:
nixos-option option_key
Motivation:
This use case is helpful when we want to explore the available configuration options for a particular aspect of the NixOS system. It provides an overview of the different subkeys and gives us an idea of the available customization options.
Explanation:
option_key
: The option key for which we want to list all subkeys.
Example:
Let’s say we want to explore the available options for the users.users
configuration key. We can use the following command:
nixos-option users.users
Output:
users.users = {
user = {
...
}
...
}
This output shows that the users.users
key has a subkey called user
. We can further explore the options under the user
subkey if needed.
List current boot kernel modules
To list the current boot kernel modules, we can use the following command:
nixos-option boot.kernelModules
Motivation:
This use case is useful when we want to check the kernel modules that are loaded during the boot process. It helps us identify which modules are already enabled and allows us to make modifications if required.
Explanation:
boot.kernelModules
: The configuration option for kernel modules.
Example:
Running the following nixos-option
command:
nixos-option boot.kernelModules
Output:
boot.kernelModules = []
This output indicates that no kernel modules are currently specified for loading during the boot process.
List authorized keys for a specific user
To list the authorized keys for a specific user, we can use the following command:
nixos-option users.users.username.openssh.authorizedKeys.keyFiles|keys
Motivation:
This use case is helpful when we want to view the authorized keys for a particular user. It allows us to verify the SSH keys that are authorized for a user’s SSH access.
Explanation:
users.users.username.openssh.authorizedKeys.keyFiles
: The configuration option for authorized keys of a specific user.keys
: A Nix expression function that returns the list of authorized key files.
Example:
Suppose we want to list the authorized keys for the user “john”. We can use the following command:
nixos-option users.users.john.openssh.authorizedKeys.keyFiles|keys
Output:
[ "/home/john/.ssh/authorized_keys" ]
This output indicates that the authorized keys for the user “john” are stored in the file “/home/john/.ssh/authorized_keys”.
List all remote builders
To list all remote builders, we can use the following command:
nixos-option nix.buildMachines
Motivation:
This use case is useful when we want to check the available remote builders in our NixOS system. Remote builders are used to distribute the build process across multiple machines, improving build performance and scalability.
Explanation:
nix.buildMachines
: The configuration option for remote builders in Nix.
Example:
Running the following nixos-option
command:
nixos-option nix.buildMachines
Output:
nix.buildMachines = []
This output indicates that no remote builders are currently specified in the Nix configuration.
List all subkeys of a given key on another NixOS configuration
To list all subkeys of a given key on another NixOS configuration, we can use the following command:
NIXOS_CONFIG=path_to_configuration.nix nixos-option option_key
Motivation:
This use case is helpful when we want to inspect the configuration options of a different NixOS system without modifying our current system. It allows us to explore the options available in another configuration file.
Explanation:
NIXOS_CONFIG=path_to_configuration.nix
: The environment variableNIXOS_CONFIG
specifies the path to the NixOS configuration file we want to inspect.option_key
: The option key for which we want to list all subkeys.
Example:
Suppose we have a different NixOS configuration file called another_configuration.nix
, and we want to explore its options for the networking.firewall
key. We can use the following command:
NIXOS_CONFIG=another_configuration.nix nixos-option networking.firewall
Output:
networking.firewall = {
...
}
This output shows that the networking.firewall
key has subkeys that can be further explored.
Show recursively all values of a user
To show recursively all values of a user, we can use the following command:
nixos-option -r users.users.user
Motivation:
This use case is helpful when we want to view all the configuration options and their values for a particular user. It gives us a comprehensive overview of the user’s configuration.
Explanation:
-r
: The flag to recursively show all values.users.users.user
: The user configuration key for which we want to view all values.
Example:
Suppose we want to show all values recursively for the user “john”. We can use the following command:
nixos-option -r users.users.john
Output:
users.users.john = {
uid = 1000;
gid = 100;
home = "/home/john";
...
}
This output displays all the configuration options and their respective values for the user “john”.
Conclusion
The nixos-option
command is a powerful tool for exploring and inspecting NixOS configurations. In this article, we covered several use cases of the command, including listing subkeys, checking kernel modules, viewing authorized keys, exploring remote builders, inspecting different configurations, and showing all values for a user. With these examples, users can gain a better understanding of the available options and customize their NixOS systems effectively.