at 24.11-pre 4.2 kB view raw
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 lib.optional (e != "") (import e) 35}: 36 37let 38 inherit (lib) optional; 39 40 evalModulesMinimal = (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 ++ 58 (optional (pkgs != null) { 59 # This should be default priority, so it conflicts with any user-defined pkgs. 60 nixpkgs.pkgs = pkgs; 61 }) 62 ); 63 }; 64 65 withWarnings = x: 66 lib.warnIf (evalConfigArgs?extraArgs) "The extraArgs argument to eval-config.nix is deprecated. Please set config._module.args instead." 67 lib.warnIf (evalConfigArgs?check) "The check argument to eval-config.nix is deprecated. Please set config._module.check instead." 68 x; 69 70 legacyModules = 71 lib.optional (evalConfigArgs?extraArgs) { 72 config = { 73 _module.args = extraArgs; 74 }; 75 } 76 ++ lib.optional (evalConfigArgs?check) { 77 config = { 78 _module.check = lib.mkDefault check; 79 }; 80 }; 81 82 allUserModules = 83 let 84 # Add the invoking file (or specified modulesLocation) as error message location 85 # for modules that don't have their own locations; presumably inline modules. 86 locatedModules = 87 if modulesLocation == null then 88 modules 89 else 90 map (lib.setDefaultModuleLocation modulesLocation) modules; 91 in 92 locatedModules ++ legacyModules; 93 94 noUserModules = evalModulesMinimal ({ 95 inherit prefix specialArgs; 96 modules = baseModules ++ extraModules ++ [ pkgsModule modulesModule ]; 97 }); 98 99 # Extra arguments that are useful for constructing a similar configuration. 100 modulesModule = { 101 config = { 102 _module.args = { 103 inherit noUserModules baseModules extraModules modules; 104 }; 105 }; 106 }; 107 108 nixosWithUserModules = noUserModules.extendModules { modules = allUserModules; }; 109 110 withExtraAttrs = configuration: configuration // { 111 inherit extraArgs; 112 inherit (configuration._module.args) pkgs; 113 inherit lib; 114 extendModules = args: withExtraAttrs (configuration.extendModules args); 115 }; 116in 117withWarnings (withExtraAttrs nixosWithUserModules)