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 ? [ ],
34}:
35
36let
37 inherit (lib) optional;
38
39 evalModulesMinimal =
40 (import ./default.nix {
41 inherit lib;
42 # Implicit use of feature is noted in implementation.
43 featureFlags.minimalModules = { };
44 }).evalModules;
45
46 pkgsModule = rec {
47 _file = ./eval-config.nix;
48 key = _file;
49 config = lib.mkMerge (
50 (optional (system != null) {
51 # Explicit `nixpkgs.system` or `nixpkgs.localSystem` should override
52 # this. Since the latter defaults to the former, the former should
53 # default to the argument. That way this new default could propagate all
54 # they way through, but has the last priority behind everything else.
55 nixpkgs.system = lib.mkDefault system;
56 })
57 ++ (optional (pkgs != null) {
58 # This should be default priority, so it conflicts with any user-defined pkgs.
59 nixpkgs.pkgs = pkgs;
60 })
61 );
62 };
63
64 withWarnings =
65 x:
66 lib.warnIf (evalConfigArgs ? extraArgs)
67 "The extraArgs argument to eval-config.nix is deprecated. Please set config._module.args instead."
68 lib.warnIf
69 (evalConfigArgs ? check)
70 "The check argument to eval-config.nix is deprecated. Please set config._module.check instead."
71 lib.warnIf
72 (specialArgs ? pkgs)
73 ''
74 You have set specialArgs.pkgs, which means that options like nixpkgs.config
75 and nixpkgs.overlays will be ignored. If you wish to reuse an already created
76 pkgs, which you know is configured correctly for this NixOS configuration,
77 please import the `nixosModules.readOnlyPkgs` module from the nixpkgs flake or
78 `(modulesPath + "/misc/nixpkgs/read-only.nix"), and set `{ nixpkgs.pkgs = <your pkgs>; }`.
79 This properly disables the ignored options to prevent future surprises.
80 ''
81 x;
82
83 legacyModules =
84 lib.optional (evalConfigArgs ? extraArgs) {
85 config = {
86 _module.args = extraArgs;
87 };
88 }
89 ++ lib.optional (evalConfigArgs ? check) {
90 config = {
91 _module.check = lib.mkDefault check;
92 };
93 };
94
95 allUserModules =
96 let
97 # Add the invoking file (or specified modulesLocation) as error message location
98 # for modules that don't have their own locations; presumably inline modules.
99 locatedModules =
100 if modulesLocation == null then
101 modules
102 else
103 map (lib.setDefaultModuleLocation modulesLocation) modules;
104 in
105 locatedModules ++ legacyModules;
106
107 noUserModules = evalModulesMinimal ({
108 inherit prefix specialArgs;
109 modules =
110 baseModules
111 ++ extraModules
112 ++ [
113 pkgsModule
114 modulesModule
115 ];
116 });
117
118 # Extra arguments that are useful for constructing a similar configuration.
119 modulesModule = {
120 config = {
121 _module.args = {
122 inherit
123 noUserModules
124 baseModules
125 extraModules
126 modules
127 ;
128 };
129 };
130 };
131
132 nixosWithUserModules = noUserModules.extendModules { modules = allUserModules; };
133
134 withExtraAttrs =
135 configuration:
136 configuration
137 // {
138 inherit extraArgs;
139 inherit (configuration._module.args) pkgs;
140 inherit lib;
141 extendModules = args: withExtraAttrs (configuration.extendModules args);
142 };
143in
144withWarnings (withExtraAttrs nixosWithUserModules)