Flakes quick start - how to use nixosModules

I want to try out flakes.

Flakes - NixOS Wiki is somehow quite different than https://gist.github.com/edolstra/40da6e3a4d4ee8fd019395365e0772e7#example-flake. The later is an initial draft that got changed a lot and now the correct function name is output not provides?

# Default module, for use in dependent flakes
nixosModule = { config }: { options = {}; config = {}; };
# Same idea as nixosModule but a list or attrset of them.
nixosModules = {};

Stupid question, let’s say I have a directory modules with a.nix, b.nix and c.nix and a default.nix that just has:

imports = [ ./a.nix ./b.nix ./c.nix ];

What do I have put to nixosModules to “export” those 3 different nix modules - I suppose I need to map import over the contents of (import modules/default.nix).imports or something? Or perhaps use builtins.readDir somehow?

1 Like

I haven’t tried flakes for NixOS modules yet, but I think you can simply write

nixosModule = { config }: { imports = [ ./default.nix ]; };

to load the modules a.nix, b.nix, and c.nix.

@fiksn see https://www.tweag.io/blog/2020-07-31-nixos-flakes/

Yeah, but I want to have one git repo with a few different modules one could choose from (not one module consisting of “submodules”). Now I did something like:

 outputs = { self, nixpkgs }:
    with nixpkgs;
      getNixFilesInDir = dir: builtins.filter (file: lib.hasSuffix ".nix" file && file != "default.nix") (builtins.attrNames (builtins.readDir dir));
      genKey = str: lib.replaceStrings [ ".nix" ] [ "" ] str;
      genValue = dir: str: { config }: { imports = [ "/${dir}${str}" ]; };
      moduleFrom = dir: str: { "${genKey str}" = genValue dir str; };
      modulesFromDir = dir: builtins.foldl' (x: y: x // (moduleFrom dir y)) { } (getNixFilesInDir dir);
      nixosModules = modulesFromDir ./modules;

Seems to work (altho I was hoping there was some easier way).