ryan4yin / nixos-and-flakes-book

:hammer_and_wrench: :heart: Want to know NixOS & Flakes in detail? Looking for a beginner-friendly tutorial? Then you've come to the right place! 想要学习使用 NixOS 与 Flakes 吗?在寻找一份新手友好的教程?那你可来对地方了!
https://nixos-and-flakes.thiscute.world
Creative Commons Attribution Share Alike 4.0 International
2.06k stars 104 forks source link

Modules vs imports #79

Closed pcasaretto closed 9 months ago

pcasaretto commented 10 months ago

Hi there!

On this section (https://nixos-and-flakes.thiscute.world/nixos-with-flakes/modularize-the-configuration), there is a mention to imports, but the linked documentation points to modules. Looking at the example it seems modules is the right parameter name. However, I have seen imports in several other places

Can you ask you to enlighten me and explain these parameters?

ryan4yin commented 10 months ago
  1. modules is a parameter of the function nixpkgs.lib.nixosSystem
  2. imports is used in submodules to import other submodules.
pcasaretto commented 10 months ago

Where should the docs point to then? If modules is this https://github.com/NixOS/nixpkgs/blob/23.11/flake.nix#L24 It shouldn't point to https://nixos.org/manual/nixpkgs/unstable/#module-system-lib-evalModules, right?

Sorry if I'm being obtuse, I'm trying to wrap my head around this. Thanks again for the book. I've already learned a lot!

pcasaretto commented 10 months ago

Will modules = args.modules ++ [{ eventually find it's way into a lib.evalModules?

ryan4yin commented 9 months ago

Will modules = args.modules ++ [{ eventually find it's way into a lib.evalModules?

Yep, it's just part of the attrset passed into import ./nixos/lib/eval-config.nix

https://github.com/NixOS/nixpkgs/blob/057f9aecfb71c4437d2b27d3323df7f93c010b7e/flake.nix#L22-L35

        nixosSystem = args:
          import ./nixos/lib/eval-config.nix (
            args // {
              modules = args.modules ++ [{
                system.nixos.versionSuffix =
                  ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";
                system.nixos.revision = final.mkIf (self ? rev) self.rev;
              }];
            } // lib.optionalAttrs (! args?system) {
              # Allow system to be set modularly in nixpkgs.system.
              # We set it to null, to remove the "legacy" entrypoint's
              # non-hermetic default.
              system = null;
            }
          );
      });

And nixos/lib/eval-config.nix is a wrapper of lib.evalModules:

https://github.com/NixOS/nixpkgs/blob/4a9a73c62698fc0f61a2f30aa8f843e45f929002/nixos/lib/eval-config.nix#L6-L8

# Ideally eval-config.nix would be an extremely thin wrapper
# around lib.evalModules, so that modular systems that have nixos configs
# as subcomponents (e.g. the container feature, or nixops if network
# expressions are ever made modular at the top level) can just use
# types.submodule instead of using eval-config.nix
ryan4yin commented 9 months ago

Closing this issue due to inactivity. Feel free to reopen if you are still have questions.

pcasaretto commented 9 months ago

Still trying to wrap my head around it, but you've made it clear. Thank you!