Search code examples
confignixnixos

How to write a new .nix config file for packages?


I want to have my list of installed packages in a separate .nix file from configuration.nix. I followed the manual and came up with this:

{ config, pkgs }:

{
  environment.systemPackages = [
    pkgs.firefox
  ];
}

Trying to compile it gives me an error:

[root@nixos:/dev/disk]# nixos-rebuild build
building Nix...
error: anonymous function at /etc/nixos/packages.nix:1:1 called with unexpected argument 'options', at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:170:8
(use '--show-trace' to show detailed location information)
building the system configuration...
error: anonymous function at /etc/nixos/packages.nix:1:1 called with unexpected argument 'options', at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:170:8
(use '--show-trace' to show detailed location information)

Solution

  • From the error, it seems like you're missing ... in the arguments list, so this should work:

    { config, pkgs, ... }:
    
    {
      environment.systemPackages = [
        pkgs.firefox
      ];
    }
    

    (An attempt at a more detailed explanation follows:)

    Your packages.nix is a function and config and pkgs are some of its parameters. There are more though, trying it out quickly I got config, pkgs, lib, options, modulesPath as the full argument list.

    Your /etc/nixos/configuration.nix is simply one of the many NixOS modules, and it is evaluated using the machinery of NixOS modules in lib.evalModules (defined in <nixpkgs/lib/modules.nix>). It is a bit special (but only a bit), and it is not evaluated directly but through <nixpkgs/nixos/lib/eval-config.nix>, which is the entrypoint to evaluating the entire NixOS configuration (also used in NixOps and when creating installation media for NixOS). You can read more about NixOS modules in the manual in the chapter "Writing NixOS Modules", although that's really not necessary for just using NixOS. It might be good to scroll through it before delving into the source code of NixOS modules though, something I learned too late.