1# From an end-user configuration file (`configuration.nix'), build a NixOS
2# configuration object (`config') from which we can retrieve option
3# values.
4
5# !!! Please think twice before adding to this argument list!
6# Ideally eval-config.nix would be an extremely thin wrapper
7# around lib.evalModules, so that modular systems that have nixos configs
8# as subcomponents (e.g. the container feature, or nixops if network
9# expressions are ever made modular at the top level) can just use
10# types.submodule instead of using eval-config.nix
11evalConfigArgs@
12{ # !!! system can be set modularly, would be nice to remove,
13 # however, removing or changing this default is too much
14 # of a breaking change. To set it modularly, pass `null`.
15 system ? builtins.currentSystem
16, # !!! is this argument needed any more? The pkgs argument can
17 # be set modularly anyway.
18 pkgs ? null
19, # !!! what do we gain by making this configurable?
20 # we can add modules that are included in specialisations, regardless
21 # of inheritParentConfig.
22 baseModules ? import ../modules/module-list.nix
23, # !!! See comment about args in lib/modules.nix
24 extraArgs ? {}
25, # !!! See comment about args in lib/modules.nix
26 specialArgs ? {}
27, modules
28, modulesLocation ? (builtins.unsafeGetAttrPos "modules" evalConfigArgs).file or null
29, # !!! See comment about check in lib/modules.nix
30 check ? true
31, prefix ? []
32, lib ? import ../../lib
33, extraModules ? let e = builtins.getEnv "NIXOS_EXTRA_MODULE_PATH";
34 in if e == "" then [] else [(import e)]
35}:
36
37let pkgs_ = pkgs;
38in
39
40let
41 inherit (lib) optional;
42
43 evalModulesMinimal = (import ./default.nix {
44 inherit lib;
45 # Implicit use of feature is noted in implementation.
46 featureFlags.minimalModules = { };
47 }).evalModules;
48
49 pkgsModule = rec {
50 _file = ./eval-config.nix;
51 key = _file;
52 config = lib.mkMerge (
53 (optional (system != null) {
54 # Explicit `nixpkgs.system` or `nixpkgs.localSystem` should override
55 # this. Since the latter defaults to the former, the former should
56 # default to the argument. That way this new default could propagate all
57 # they way through, but has the last priority behind everything else.
58 nixpkgs.system = lib.mkDefault system;
59 })
60 ++
61 (optional (pkgs_ != null) {
62 _module.args.pkgs = lib.mkForce pkgs_;
63 })
64 );
65 };
66
67 withWarnings = x:
68 lib.warnIf (evalConfigArgs?extraArgs) "The extraArgs argument to eval-config.nix is deprecated. Please set config._module.args instead."
69 lib.warnIf (evalConfigArgs?check) "The check argument to eval-config.nix is deprecated. Please set config._module.check instead."
70 x;
71
72 legacyModules =
73 lib.optional (evalConfigArgs?extraArgs) {
74 config = {
75 _module.args = extraArgs;
76 };
77 }
78 ++ lib.optional (evalConfigArgs?check) {
79 config = {
80 _module.check = lib.mkDefault check;
81 };
82 };
83
84 allUserModules =
85 let
86 # Add the invoking file (or specified modulesLocation) as error message location
87 # for modules that don't have their own locations; presumably inline modules.
88 locatedModules =
89 if modulesLocation == null then
90 modules
91 else
92 map (lib.setDefaultModuleLocation modulesLocation) modules;
93 in
94 locatedModules ++ legacyModules;
95
96 noUserModules = evalModulesMinimal ({
97 inherit prefix specialArgs;
98 modules = baseModules ++ extraModules ++ [ pkgsModule modulesModule ];
99 });
100
101 # Extra arguments that are useful for constructing a similar configuration.
102 modulesModule = {
103 config = {
104 _module.args = {
105 inherit noUserModules baseModules extraModules modules;
106 };
107 };
108 };
109
110 nixosWithUserModules = noUserModules.extendModules { modules = allUserModules; };
111
112in
113withWarnings nixosWithUserModules // {
114 inherit extraArgs;
115 inherit (nixosWithUserModules._module.args) pkgs;
116}