From 82f4031ce76e45e6c073fef0520572a030f19049 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 2 Oct 2019 01:46:49 +0200 Subject: [PATCH] Add source scanning --- default.nix | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ nix/project.nix | 7 ++----- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/default.nix b/default.nix index 3d0b06f..02dc253 100644 --- a/default.nix +++ b/default.nix @@ -8,8 +8,58 @@ let { modules ? [] }: lib.evalModules { check = true; modules = builtinModules ++ modules; + specialArgs = { inherit scanProjectModules; }; }; + scanProjectModules = scan "nix/project-module.nix"; + + # Like a PATH search, where filePath is like bin/somecommand + # + # scan : string -> sources -> listOf module + # + # Arguments + # + # filePath : string File to look for in the sources + # + # sources : attrsOf path Sources that may contain a file named ${filePath} + # or path or a path that has such a Nix expression + scan = filePath: + let + withAsserts = + assert builtins.typeOf filePath == "string"; + x: x; + + importify = + f: x: + if lib.types.path.check x + then builtins.addErrorContext + "while scanning for modules in ${toString x}" + (f (import x)) + else builtins.addErrorContext + "while scanning for modules" + (f x); + + scanAttrs = attrs: + lib.concatLists ( + lib.mapAttrsToList ( + key: value: + builtins.addErrorContext + "while scanning attribute ${key}" + (scanIfDirlike value) + ) + attrs + ); + + scanIfDirlike = value: + if lib.types.path.check value + then let + potential = value + "/${filePath}"; + in + if builtins.pathExists potential then [ potential ] else [] + else []; + in + withAsserts (importify scanAttrs); + builtinModules = [ ./modules/root.nix ./modules/nixpkgs.nix diff --git a/nix/project.nix b/nix/project.nix index 36d468b..369392d 100644 --- a/nix/project.nix +++ b/nix/project.nix @@ -1,9 +1,6 @@ +{ scanProjectModules, ... }: { - # imports = scan "nix/project-module.nix" ./sources.nix; - - imports = [ - ((import ./sources.nix).nix-pre-commit-hooks + "/nix/project-module.nix") - ]; + imports = scanProjectModules ./sources.nix; root = ../.; pinning.niv.enable = true;