I have a configuration.nix that I wrote, funnily enough I am now having a hard time understanding why it works.
So I am importing several files in nix
imports =
[
# Include the results of the hardware scan.
./hardware-configuration.nix
./conf/nvidia.nix
# use firewall with defaults
(import ./utils/firewall.nix ({
config = config;
pkgs = pkgs;
enable_localsend = true;
}))
];
With a firewall.nix here
{ config, pkgs, enable_localsend ? false, ... }:
{
networking = {
nftables = {
enable = true;
};
firewall = {
enable = true;
allowPing = true;
package = pkgs.nftables;
allowedTCPPorts = [ 80 443 22 8000 8080 ] ++ (if enable_localsend then [ 53317 ] else [ ]);
allowedUDPPorts = [ ] ++ (if enable_localsend then [ 53317 ] else [ ]);
};
};
}
Why am I able to specify the first couple as just a path? While the firewalls.nix needs to be imported?
Because imports
should be defined as a list of NixOS modules, and a NixOS module can take one of three forms.
- An attribute set with all the option definitions you want to set.
- A function that takes some automatic arguments (like
pkgs
and lib
) and returns an attrset like #1.
- A nix file that returns either #1 or #2.
So the ones that you used a file path for are #3, but the one where you used import
is actually #1. You just manually imported a file that contained a function and manually called that function with some arguments, which returns an attrset that you used as a module a la #1.
1 Like
Given that #2 exists, can I configure imports to be a path like 3 but pass the args? without importing?
A module can set _module.args.foo = "bar";
to make it so that other modules can take foo
as one of these automatic arguments, but this is finicky and it’s easy to accidentally make infinite recursion. It’s usually much better to just create your own custom options and set their values. See the manual’s section on the module system: NixOS Manual
1 Like
Or if you want to understand how modules work in a step-by-step tutorial: Module system — nix.dev documentation
And there’s an entire collection of in-depth videos about the module system by @infinisil: https://m.youtube.com/results?search_query=the+nix+hour+module+system