Hi all,
I am new to NixOS and as everyone I have to manage my configuration.nix file.
I have a question on how to write on it and arrange it. Is it possible, and is it advised, to put some settings in a separated file (I don’t know, for example packages for a specific user) and add it to the “main” configuration file, in the same way it is sometimes done for the bash_aliases added to bash_rc ?
Hey!
If I understand correctly, you are asking about modularizing your nixos config, splitting it into multiple files.
It is possible.
First, the way you probably want to do it:
There exists a special imports
field for all modules in the nixpkgs module system. All file paths you add there will be merged into the configuration
# module_a.nix
{ pkgs, lib, ...}: {
environment.systemPackages = with pkgs; [ hello ];
}
# configuration.nix
{ pkgs, lib, ...} : {
imports = [./module-a.nix];
environment.systemPackages = with pkgs; [ world ];
# in the final config, environment.systemPackages will be [hello world]
}
Second, the nix language itself provides an import keyword that can bind the nix expression from one file to a variable.
Example
# a.nix
## a function that returns 2
{}: 2
# b.nix
let a = import ./a.nix; in (a {}) +2
### this file will evaluate to 2+2=4
Thanks a lot !
It is very clear and it seems to be what I was searching for.
There is just a small typo :
instead of
imports = [./module_a.nix];
Anyway, thanks again !