From 1c8ac213b04efdf368a0370a1392a9ab8fe4b370 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Sun, 16 Apr 2023 16:31:17 -0700 Subject: [PATCH] Split up builtinModule into multiple modules --- builtin-modules/default-formatters.nix | 7 +++ builtin-modules/editorconfig.nix | 13 ++++ builtin-modules/pre-commit-hook.nix | 10 +++ default.nix | 87 ++++++++++---------------- 4 files changed, 64 insertions(+), 53 deletions(-) create mode 100644 builtin-modules/default-formatters.nix create mode 100644 builtin-modules/editorconfig.nix create mode 100644 builtin-modules/pre-commit-hook.nix diff --git a/builtin-modules/default-formatters.nix b/builtin-modules/default-formatters.nix new file mode 100644 index 0000000..8c80bd3 --- /dev/null +++ b/builtin-modules/default-formatters.nix @@ -0,0 +1,7 @@ +_: { + devTools = pkgs: with pkgs; [ nixpkgs-fmt nodePackages.prettier ]; + formatters = { + "*.nix" = "nixpkgs-fmt"; + "*.md | *.json | *.yml" = "prettier --write"; + }; +} diff --git a/builtin-modules/editorconfig.nix b/builtin-modules/editorconfig.nix new file mode 100644 index 0000000..f6b7df6 --- /dev/null +++ b/builtin-modules/editorconfig.nix @@ -0,0 +1,13 @@ +{ src, lib }: +let + inherit (lib) getExe optionalAttrs optionalString pathExists; +in +{ + checks = optionalAttrs (pathExists (src + /.editorconfig)) { + # By default, high false-positive flags are disabled. + editorconfig = { editorconfig-checker }: + "${getExe editorconfig-checker}" + + optionalString (!pathExists (src + /.ecrc)) + " -disable-indent-size -disable-max-line-length"; + }; +} diff --git a/builtin-modules/pre-commit-hook.nix b/builtin-modules/pre-commit-hook.nix new file mode 100644 index 0000000..0cd21b6 --- /dev/null +++ b/builtin-modules/pre-commit-hook.nix @@ -0,0 +1,10 @@ +_: { + shellHook = { lib, flakelite }: '' + if [ -f flake.nix ] && [ -d .git/hooks ] && + [ ! -f .git/hooks/pre-commit ]; then + echo Installing git pre-commit hook... + cp ${lib.getExe flakelite.inputs'.flakelite.packages.pre-commit + } .git/hooks + fi + ''; +} diff --git a/default.nix b/default.nix index b43b23a..5dfbe36 100644 --- a/default.nix +++ b/default.nix @@ -5,12 +5,12 @@ localInputs: let inherit (builtins) intersectAttrs isPath readDir; - inherit (localInputs.nixpkgs.lib) attrNames attrVals callPackageWith - composeManyExtensions concat concatStringsSep filter filterAttrs foldAttrs - foldl functionArgs genAttrs hasSuffix isFunction isList isString listToAttrs - mapAttrs mapAttrsToList mapAttrs' mergeAttrs nameValuePair optional - optionalAttrs optionalString parseDrvName pathExists pipe recursiveUpdate - removePrefix removeSuffix zipAttrsWith; + inherit (localInputs.nixpkgs.lib) attrNames attrVals attrValues + callPackageWith composeManyExtensions concat concatStringsSep filter + filterAttrs foldAttrs foldl functionArgs genAttrs hasSuffix isFunction + isList isString listToAttrs mapAttrs mapAttrsToList mapAttrs' mergeAttrs + nameValuePair optional optionalAttrs parseDrvName pathExists pipe + recursiveUpdate removePrefix removeSuffix zipAttrsWith; /* Attributes in flakelite's lib output. */ @@ -23,60 +23,39 @@ let /* Module which is always included as first module. */ - builtinModule = { src, inputs, root }: { + baseModule = { inputs, root, args }: { # Ensures nixpkgs and flakelite are available for modules. inputs = { flakelite = localInputs.self; inherit (localInputs) nixpkgs; }; - withOverlays = params: [ - (final: prev: { - # Allows access to flakelite lib functions from package sets. - # Also adds pkgs-specific additional args. - flakelite = params // { - # Inputs with system auto-selected. - # i.e. inputs.self.packages.${system} -> inputs'.self.packages - inputs' = mapAttrs - (_: mapAttrs - (_: v: v.${prev.system} or { })) - inputs; - # Default package meta attribute generated from root module attrs. - meta = { - platforms = root.systems; - } // optionalAttrs (root ? description) { - inherit (root) description; - } // optionalAttrs (root ? license) { - license = - if isList root.license - then attrVals root.license final.lib.licenses - else final.lib.licenses.${root.license}; - }; + withOverlay = final: prev: { + # Allows access to flakelite lib functions from package sets. + # Also adds pkgs-specific additional args. + flakelite = args // { + # Inputs with system auto-selected. + # i.e. inputs.self.packages.${system} -> inputs'.self.packages + inputs' = mapAttrs + (_: mapAttrs + (_: v: v.${prev.system} or { })) + inputs; + # Default package meta attribute generated from root module attrs. + meta = { + platforms = root.systems; + } // optionalAttrs (root ? description) { + inherit (root) description; + } // optionalAttrs (root ? license) { + license = + if isList root.license + then attrVals root.license final.lib.licenses + else final.lib.licenses.${root.license}; }; - }) - ]; - checks = { pkgs, lib, ... }: - # Enable editorconfig support if detected. - # By default, high false-positive flags are disabled. - (optionalAttrs (pathExists (src + /.editorconfig)) { - editorconfig = "${lib.getExe pkgs.editorconfig-checker}" - + optionalString (!pathExists (src + /.ecrc)) - " -disable-indent-size -disable-max-line-length"; - }); - shellHook = { lib, flakelite }: '' - if [ -f flake.nix ] && [ -d .git/hooks ] && - [ ! -f .git/hooks/pre-commit ]; then - echo Installing git pre-commit hook... - cp ${lib.getExe flakelite.inputs'.flakelite.packages.pre-commit - } .git/hooks - fi - ''; - devTools = pkgs: with pkgs; [ nixpkgs-fmt nodePackages.prettier ]; - formatters = { - "*.nix" = "nixpkgs-fmt"; - "*.md | *.json | *.yml" = "prettier --write"; + }; }; }; + builtinModules = attrValues (importDir ./builtin-modules); + /* Import each nix file in a directory as attrs. Attr name is file name with extension stripped. To allow use in an importable directory, default.nix is skipped. To provide a file that will result in a "default" attr, name the @@ -376,10 +355,12 @@ let raw = root; }; + modules = [ baseModule ] ++ builtinModules ++ root'.modules; + # Merge result of all the modules. merged = foldl mergeModules moduleAttrDefaults - ((map (m: normalizeModule (applyNonSysArgs m)) - ([ builtinModule ] ++ root'.modules)) ++ [ root' ]); + ((map (m: normalizeModule (applyNonSysArgs m)) modules) + ++ [ root' ]); # Returns package set for a system. pkgsFor = system: import merged.inputs.nixpkgs {