From e15c41fdfea171ee44f50da914813d0ba76a0a59 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Tue, 12 Dec 2023 22:51:54 +0100 Subject: [PATCH] misc: flake refactoring --- flake-modules/checks.nix | 31 +++++ flake-modules/default.nix | 26 ++++ flake-modules/dev.nix | 29 +++++ flake-modules/legacy-packages.nix | 18 +++ flake-modules/lib.nix | 20 ++++ flake-modules/modules.nix | 47 ++++++++ flake-modules/overlays.nix | 21 ++++ flake-modules/packages.nix | 28 +++++ flake-modules/templates.nix | 8 ++ flake-modules/wrappers.nix | 35 ++++++ flake.nix | 189 +++--------------------------- 11 files changed, 282 insertions(+), 170 deletions(-) create mode 100644 flake-modules/checks.nix create mode 100644 flake-modules/default.nix create mode 100644 flake-modules/dev.nix create mode 100644 flake-modules/legacy-packages.nix create mode 100644 flake-modules/lib.nix create mode 100644 flake-modules/modules.nix create mode 100644 flake-modules/overlays.nix create mode 100644 flake-modules/packages.nix create mode 100644 flake-modules/templates.nix create mode 100644 flake-modules/wrappers.nix diff --git a/flake-modules/checks.nix b/flake-modules/checks.nix new file mode 100644 index 0000000000..da2930ae94 --- /dev/null +++ b/flake-modules/checks.nix @@ -0,0 +1,31 @@ +{ + perSystem = { + pkgs, + makeNixvimWithModuleUnfree, + makeNixvimWithModule, + ... + }: { + checks = { + tests = import ../tests { + inherit pkgs; + inherit (pkgs) lib; + makeNixvim = configuration: + makeNixvimWithModuleUnfree { + module = { + config = configuration; + }; + }; + }; + + extra-args-tests = import ../tests/extra-args.nix { + inherit pkgs; + inherit makeNixvimWithModule; + }; + + lib-tests = import ../tests/lib-tests.nix { + inherit pkgs; + inherit (pkgs) lib; + }; + }; + }; +} diff --git a/flake-modules/default.nix b/flake-modules/default.nix new file mode 100644 index 0000000000..2e8997c3cd --- /dev/null +++ b/flake-modules/default.nix @@ -0,0 +1,26 @@ +{inputs, ...}: { + imports = [ + ./checks.nix + ./dev.nix + ./lib.nix + ./legacy-packages.nix + ./modules.nix + ./overlays.nix + ./packages.nix + ./templates.nix + ./wrappers.nix + ]; + + perSystem = { + pkgs, + system, + ... + }: { + _module.args = { + pkgsUnfree = import inputs.nixpkgs { + inherit system; + config.allowUnfree = true; + }; + }; + }; +} diff --git a/flake-modules/dev.nix b/flake-modules/dev.nix new file mode 100644 index 0000000000..2f1eb72144 --- /dev/null +++ b/flake-modules/dev.nix @@ -0,0 +1,29 @@ +{inputs, ...}: { + imports = [ + inputs.pre-commit-hooks.flakeModule + ]; + + perSystem = { + pkgs, + config, + ... + }: { + devShells.default = pkgs.mkShellNoCC { + shellHook = config.pre-commit.installationScript; + }; + + formatter = pkgs.alejandra; + + pre-commit = { + settings.hooks = { + alejandra.enable = true; + statix = { + enable = true; + excludes = [ + "plugins/lsp/language-servers/rust-analyzer-config.nix" + ]; + }; + }; + }; + }; +} diff --git a/flake-modules/legacy-packages.nix b/flake-modules/legacy-packages.nix new file mode 100644 index 0000000000..dca631c3b3 --- /dev/null +++ b/flake-modules/legacy-packages.nix @@ -0,0 +1,18 @@ +{ + perSystem = { + pkgs, + config, + makeNixvimWithModule, + ... + }: { + legacyPackages = rec { + inherit makeNixvimWithModule; + makeNixvim = configuration: + makeNixvimWithModule { + module = { + config = configuration; + }; + }; + }; + }; +} diff --git a/flake-modules/lib.nix b/flake-modules/lib.nix new file mode 100644 index 0000000000..49c72e3ff8 --- /dev/null +++ b/flake-modules/lib.nix @@ -0,0 +1,20 @@ +{ + config, + lib, + withSystem, + ... +}: { + flake.lib = lib.genAttrs config.systems ( + lib.flip withSystem ( + { + pkgs, + config, + ... + }: + import ../lib { + inherit pkgs lib; + inherit (config.legacyPackages) makeNixvim; + } + ) + ); +} diff --git a/flake-modules/modules.nix b/flake-modules/modules.nix new file mode 100644 index 0000000000..4bc394215d --- /dev/null +++ b/flake-modules/modules.nix @@ -0,0 +1,47 @@ +{ + modules, + inputs, + ... +}: { + _module.args = let + nixvimModules = with builtins; + map + (f: ../modules + "/${f}") + ( + attrNames (readDir ../modules) + ); + in { + modules = pkgs: let + nixpkgsMaintainersList = pkgs.path + "/nixos/modules/misc/meta.nix"; + + nixvimExtraArgsModule = rec { + _file = ./flake.nix; + key = _file; + config = { + _module.args = { + pkgs = pkgs.lib.mkForce pkgs; + inherit (pkgs) lib; + helpers = import ../lib/helpers.nix {inherit (pkgs) lib;}; + # TODO: Not sure why the modules need to access the whole flake inputs... + inherit inputs; + }; + }; + }; + in + nixvimModules + ++ [ + nixpkgsMaintainersList + nixvimExtraArgsModule + ]; + }; + + perSystem = { + pkgs, + config, + ... + }: { + _module.args = { + modules = modules pkgs; + }; + }; +} diff --git a/flake-modules/overlays.nix b/flake-modules/overlays.nix new file mode 100644 index 0000000000..17ca24696b --- /dev/null +++ b/flake-modules/overlays.nix @@ -0,0 +1,21 @@ +{inputs, ...}: { + imports = [ + inputs.flake-parts.flakeModules.easyOverlay + ]; + perSystem = { + config, + pkgs, + final, + ... + }: { + overlayAttrs = { + nixvim = { + inherit + (config.legacyPackages) + makeNixvim + makeNixvimWithModule + ; + }; + }; + }; +} diff --git a/flake-modules/packages.nix b/flake-modules/packages.nix new file mode 100644 index 0000000000..fc2f0073fb --- /dev/null +++ b/flake-modules/packages.nix @@ -0,0 +1,28 @@ +{ + perSystem = { + pkgs, + pkgsUnfree, + config, + modules, + ... + }: { + packages = let + docs = { + docs = pkgsUnfree.callPackage (import ../docs) { + inherit modules; + }; + }; + + man-docs = import ../man-docs { + pkgs = pkgsUnfree; + inherit modules; + }; + + # TODO: deprecate (unused) + helpers = import ../helpers pkgs; + in + docs + // man-docs + // helpers; + }; +} diff --git a/flake-modules/templates.nix b/flake-modules/templates.nix new file mode 100644 index 0000000000..07e79d4773 --- /dev/null +++ b/flake-modules/templates.nix @@ -0,0 +1,8 @@ +{ + flake.templates = { + default = { + path = ../templates/simple; + description = "A simple nix flake template for getting started with nixvim"; + }; + }; +} diff --git a/flake-modules/wrappers.nix b/flake-modules/wrappers.nix new file mode 100644 index 0000000000..cb333c82f8 --- /dev/null +++ b/flake-modules/wrappers.nix @@ -0,0 +1,35 @@ +{ + modules, + self, + ... +}: let + wrapperArgs = { + inherit modules; + inherit self; + }; +in { + perSystem = { + pkgs, + pkgsUnfree, + config, + ... + }: { + _module.args = { + makeNixvimWithModule = + import ../wrappers/standalone.nix + pkgs + wrapperArgs; + + makeNixvimWithModuleUnfree = + import ../wrappers/standalone.nix + pkgsUnfree + wrapperArgs; + }; + }; + + flake = { + nixosModules.nixvim = import ./nixos.nix wrapperArgs; + homeManagerModules.nixvim = import ./wrappers/hm.nix wrapperArgs; + nixDarwinModules.nixvim = import ./wrappers/darwin.nix wrapperArgs; + }; +} diff --git a/flake.nix b/flake.nix index 7b5fcdb037..c1f90cf71c 100644 --- a/flake.nix +++ b/flake.nix @@ -3,181 +3,30 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - flake-parts.url = "github:hercules-ci/flake-parts"; + + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; pre-commit-hooks = { url = "github:cachix/pre-commit-hooks.nix"; inputs.nixpkgs.follows = "nixpkgs"; + inputs.nixpkgs-stable.follows = "nixpkgs"; }; }; - outputs = { - self, - nixpkgs, - flake-parts, - pre-commit-hooks, - ... - } @ inputs: - with nixpkgs.lib; - with builtins; let - # TODO: Support nesting - nixvimModules = map (f: ./modules + "/${f}") (attrNames (builtins.readDir ./modules)); - - modules = pkgs: - nixvimModules - ++ [ - rec { - _file = ./flake.nix; - key = _file; - config = { - _module.args = { - pkgs = mkForce pkgs; - inherit (pkgs) lib; - helpers = import ./plugins/helpers.nix {inherit (pkgs) lib;}; - inherit inputs; - }; - }; - } - - (pkgs.path + "/nixos/modules/misc/meta.nix") - # ./plugins/default.nix - ]; - - wrapperArgs = { - inherit modules; - inherit self; - }; - in - flake-parts.lib.mkFlake {inherit inputs;} ({ - withSystem, - config, - lib, - ... - }: { - systems = [ - "x86_64-linux" - "aarch64-linux" - "x86_64-darwin" - "aarch64-darwin" - ]; - - perSystem = { - pkgs, - system, - ... - }: let - pkgs-unfree = import nixpkgs { - inherit system; - config.allowUnfree = true; - }; - in { - checks = { - tests = import ./tests { - inherit pkgs; - inherit (pkgs) lib; - # Some nixvim supported plugins require the use of unfree packages. - # As we test as many things as possible, we need to allow unfree sources by generating - # a separate `makeNixvim` module (with pkgs-unfree). - makeNixvim = let - makeNixvimWithModuleUnfree = import ./wrappers/standalone.nix pkgs-unfree wrapperArgs; - in - configuration: - makeNixvimWithModuleUnfree { - module = { - config = configuration; - }; - }; - }; - lib-tests = import ./tests/lib-tests.nix { - inherit (pkgs) pkgs lib; - }; - extra-args-tests = import ./tests/extra-args.nix { - inherit pkgs; - inherit (self.legacyPackages.${system}) makeNixvimWithModule; - }; - pre-commit-check = pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - alejandra = { - enable = true; - excludes = ["plugins/_sources"]; - }; - statix.enable = true; - }; - settings.statix.ignore = ["plugins/lsp/language-servers/rust-analyzer-config.nix"]; - }; - }; - devShells = { - default = pkgs.mkShell { - inherit (self.checks.${system}.pre-commit-check) shellHook; - }; - }; - packages = - { - docs = pkgs-unfree.callPackage (import ./docs) { - modules = modules pkgs; - }; - } - // (import ./helpers pkgs) - // (import ./man-docs { - pkgs = pkgs-unfree; - modules = modules pkgs; - }); - - legacyPackages = rec { - makeNixvimWithModule = import ./wrappers/standalone.nix pkgs wrapperArgs; - makeNixvim = configuration: - makeNixvimWithModule { - module = { - config = configuration; - }; - }; - }; - - formatter = pkgs.alejandra; - }; - - flake = { - nixosModules.nixvim = import ./wrappers/nixos.nix wrapperArgs; - homeManagerModules.nixvim = import ./wrappers/hm.nix wrapperArgs; - nixDarwinModules.nixvim = import ./wrappers/darwin.nix wrapperArgs; - rawModules.nixvim = nixvimModules; - - overlays.default = final: prev: { - nixvim = rec { - makeNixvimWithModule = import ./wrappers/standalone.nix prev wrapperArgs; - makeNixvim = configuration: - makeNixvimWithModule { - module = { - config = configuration; - }; - }; - }; - }; - - lib = lib.genAttrs config.systems ( - lib.flip withSystem ( - { - pkgs, - system, - ... - }: - import ./lib { - inherit pkgs; - inherit (pkgs) lib; - inherit (self.legacyPackages."${system}") makeNixvim; - } - ) - ); - - templates = let - simple = { - path = ./templates/simple; - description = "A simple nix flake template for getting started with nixvim"; - }; - in { - default = simple; - }; - }; - }); + outputs = inputs: + inputs.flake-parts.lib.mkFlake {inherit inputs;} { + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + + imports = [ + ./flake-modules + ]; + }; }