From e03cd65e876e1562c90c1a4c6801dbc91cc0f463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Fri, 4 Oct 2024 23:23:30 +0200 Subject: [PATCH] feat: use flake-parts to clean up the flake.nix This is much more manageable! --- flake-modules/checks.nix | 26 ++++ flake-modules/default.nix | 25 ++++ flake-modules/devshells.nix | 13 ++ flake-modules/docs.nix | 16 +++ flake-modules/example-vm.nix | 46 ++++++++ flake-modules/nixos-modules.nix | 43 +++++++ flake-modules/nvim.nix | 24 ++++ flake-modules/overlays.nix | 26 ++++ flake-modules/scripts.nix | 14 +++ flake.lock | 12 +- flake.nix | 203 ++------------------------------ 11 files changed, 250 insertions(+), 198 deletions(-) create mode 100644 flake-modules/checks.nix create mode 100644 flake-modules/default.nix create mode 100644 flake-modules/devshells.nix create mode 100644 flake-modules/docs.nix create mode 100644 flake-modules/example-vm.nix create mode 100644 flake-modules/nixos-modules.nix create mode 100644 flake-modules/nvim.nix create mode 100644 flake-modules/overlays.nix create mode 100644 flake-modules/scripts.nix diff --git a/flake-modules/checks.nix b/flake-modules/checks.nix new file mode 100644 index 0000000..206d2e0 --- /dev/null +++ b/flake-modules/checks.nix @@ -0,0 +1,26 @@ +{ lib, ... }: +{ + perSystem = + { pkgs, self', ... }: + { + checks = + let + src = builtins.path { + path = ../.; + name = "configuration.nix"; + }; + runCmdInSrc = + name: cmd: + pkgs.runCommandNoCC name { } '' + cd ${src} + ${cmd} + mkdir $out + ''; + in + { + fmt = runCmdInSrc "fmt-src" "${lib.getExe self'.formatter} --check ."; + lint = runCmdInSrc "lint-src" "${lib.getExe pkgs.statix} check ."; + typos = runCmdInSrc "typos-src" "${lib.getExe pkgs.typos} ."; + }; + }; +} diff --git a/flake-modules/default.nix b/flake-modules/default.nix new file mode 100644 index 0000000..f028f92 --- /dev/null +++ b/flake-modules/default.nix @@ -0,0 +1,25 @@ +{ inputs, ... }: +let + overlays = builtins.attrValues inputs.self.overlays; +in +{ + imports = [ + ./checks.nix + ./devshells.nix + ./docs.nix + ./example-vm.nix + ./nixos-modules.nix + ./nvim.nix + ./overlays.nix + ./scripts.nix + ]; + + perSystem = + { system, pkgs, ... }: + { + _module.args.pkgs = import inputs.nixpkgs { inherit system overlays; }; + + # Nix files formatter (run `nix fmt`) + formatter = pkgs.nixfmt-rfc-style; + }; +} diff --git a/flake-modules/devshells.nix b/flake-modules/devshells.nix new file mode 100644 index 0000000..677ecdc --- /dev/null +++ b/flake-modules/devshells.nix @@ -0,0 +1,13 @@ +_: { + perSystem = + { pkgs, self', ... }: + { + devShells.default = pkgs.mkShellNoCC { + buildInputs = [ + pkgs.just + self'.packages.nvim + ]; + QEMU_OPTS_WL = "--enable-kvm -smp 4 -device virtio-gpu-rutabaga,gfxstream-vulkan=on,cross-domain=on,hostmem=2G,wsi=headless"; + }; + }; +} diff --git a/flake-modules/docs.nix b/flake-modules/docs.nix new file mode 100644 index 0000000..bcd9fbe --- /dev/null +++ b/flake-modules/docs.nix @@ -0,0 +1,16 @@ +{ lib, ... }: +{ + perSystem = + { pkgs, ... }: + { + packages = { + # Documentation + inherit (import ../docs { inherit pkgs lib; }) + docs + nixos-markdown + nvim-markdown + home-markdown + ; + }; + }; +} diff --git a/flake-modules/example-vm.nix b/flake-modules/example-vm.nix new file mode 100644 index 0000000..0e04439 --- /dev/null +++ b/flake-modules/example-vm.nix @@ -0,0 +1,46 @@ +{ inputs, lib, ... }: +{ + # Example vm configuration + flake.nixosConfigurations.vm = + let + system = "x86_64-linux"; + overlays = builtins.attrValues inputs.self.overlays; + config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "steam-original" ]; + pkgs = import inputs.nixpkgs { inherit system overlays config; }; + in + lib.nixosSystem { + inherit system pkgs; + modules = [ + inputs.self.nixosModules.vm # import vm module + { + time.timeZone = "Europe/Berlin"; + i18n.defaultLocale = "en_US.UTF-8"; + users.users.jdoe = { + password = "example"; + isNormalUser = true; + extraGroups = [ + "wheel" + "video" + "networkmanager" + ]; + }; + home-manager.users.jdoe = { + home = { + username = "jdoe"; + homeDirectory = "/home/jdoe"; + }; + jhome = { + enable = true; + gui.enable = true; + dev.rust.enable = true; + }; + }; + nix.registry.nixpkgs.flake = inputs.nixpkgs; + jconfig = { + enable = true; + gui.enable = true; + }; + } + ]; + }; +} diff --git a/flake-modules/nixos-modules.nix b/flake-modules/nixos-modules.nix new file mode 100644 index 0000000..f390328 --- /dev/null +++ b/flake-modules/nixos-modules.nix @@ -0,0 +1,43 @@ +{ inputs, lib, ... }: +{ + flake.nixosModules = + let + nvim-config.imports = [ + inputs.nixvim.homeManagerModules.nixvim + ../nvim + ]; + homeManagerModuleSandalone = import ../home { + inherit nvim-config; + inherit (inputs) stylix; + }; + homeManagerModuleNixOS = import ../home { inherit nvim-config; }; + nixosModule = { + imports = [ + (import ../system { inherit (inputs) stylix; }) + inputs.home-manager.nixosModules.home-manager + ] ++ lib.optional (inputs.lix-module != null) inputs.lix-module.nixosModules.default; + home-manager = { + useGlobalPkgs = true; + useUserPackages = true; + sharedModules = [ homeManagerModuleNixOS ]; + }; + # Pin nixpkgs + nix.registry.nixpkgs.flake = inputs.nixpkgs; + }; + + machines = [ "vm" ]; + mkMachine = hostname: { + imports = [ + nixosModule + (import (../machines + "/${hostname}")) + ]; + home-manager.sharedModules = [ { jhome.hostName = hostname; } ]; + }; + machineModules = lib.genAttrs machines mkMachine; + in + { + default = nixosModule; + inherit nixosModule homeManagerModuleNixOS homeManagerModuleSandalone; + } + // machineModules; +} diff --git a/flake-modules/nvim.nix b/flake-modules/nvim.nix new file mode 100644 index 0000000..95280ef --- /dev/null +++ b/flake-modules/nvim.nix @@ -0,0 +1,24 @@ +{ inputs, ... }: +let + standalone = ../nvim/standalone.nix; +in +{ + flake.overlays.nixvim = inputs.nixvim.overlays.default; + + perSystem = + { pkgs, system, ... }: + { + # Check standalone nvim build + checks.nvim = inputs.nixvim.lib.${system}.check.mkTestDerivationFromNixvimModule { + inherit pkgs; + module = ../nvim/standalone.nix; + }; + + # Nvim standalone module + packages.nvim = inputs.nixvim.legacyPackages.${system}.makeNixvimWithModule { + inherit pkgs; + module = standalone; + }; + + }; +} diff --git a/flake-modules/overlays.nix b/flake-modules/overlays.nix new file mode 100644 index 0000000..62e46dd --- /dev/null +++ b/flake-modules/overlays.nix @@ -0,0 +1,26 @@ +{ inputs, ... }: +{ + # Add unstable packages to overlay + flake.overlays.unstable = + final: prev: + let + unstablePkgs = inputs.unstable.legacyPackages.${prev.stdenv.hostPlatform.system}; + in + { + # Get unstable packages + unstable = unstablePkgs; + + # Update vim plugins with the versions from unstable + vimPlugins = prev.vimPlugins // unstablePkgs.vimPlugins; + + # Get specific packages from unstable + inherit (unstablePkgs) + gitoxide + jujutsu + neovim-unwrapped + ruff # nixpkgs stable version is improperly configured by nixvim + # wezterm + ; + }; + +} diff --git a/flake-modules/scripts.nix b/flake-modules/scripts.nix new file mode 100644 index 0000000..590f6a1 --- /dev/null +++ b/flake-modules/scripts.nix @@ -0,0 +1,14 @@ +let + scripts = import ../scripts; +in +{ + # Add scripts to overlay + flake.overlays.scripts = final: prev: scripts final; + + # Add scripts to packages + perSystem = + { pkgs, ... }: + { + packages = scripts pkgs; + }; +} diff --git a/flake.lock b/flake.lock index fce8ae4..d72406f 100644 --- a/flake.lock +++ b/flake.lock @@ -117,16 +117,15 @@ "flake-parts": { "inputs": { "nixpkgs-lib": [ - "nixvim", "nixpkgs" ] }, "locked": { - "lastModified": 1726153070, - "narHash": "sha256-HO4zgY0ekfwO5bX0QH/3kJ/h4KvUDFZg8YpkNwIbg1U=", + "lastModified": 1727826117, + "narHash": "sha256-K5ZLCyfO/Zj9mPFldf3iwS6oZStJcU4tSpiXTMYaaL0=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "bcef6817a8b2aa20a5a6dbb19b43e63c5bf8619a", + "rev": "3d04084d54bedc3d6b8b736c70ef449225c361b1", "type": "github" }, "original": { @@ -278,7 +277,9 @@ "inputs": { "devshell": [], "flake-compat": [], - "flake-parts": "flake-parts", + "flake-parts": [ + "flake-parts" + ], "git-hooks": [], "home-manager": [ "home-manager" @@ -306,6 +307,7 @@ }, "root": { "inputs": { + "flake-parts": "flake-parts", "home-manager": "home-manager", "lix-module": "lix-module", "nixpkgs": "nixpkgs", diff --git a/flake.nix b/flake.nix index e82ed27..31cb5e1 100644 --- a/flake.nix +++ b/flake.nix @@ -2,6 +2,7 @@ { # A helpful description of your flake description = "My NixOS configuration"; + # Flake inputs inputs = { nixpkgs.url = "nixpkgs/nixos-24.05"; @@ -33,6 +34,7 @@ inputs = { nixpkgs.follows = "unstable"; home-manager.follows = "home-manager"; + flake-parts.follows = "flake-parts"; # disable optional inputs flake-compat.follows = ""; nix-darwin.follows = ""; @@ -41,205 +43,20 @@ git-hooks.follows = ""; }; }; + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; # For deduplication systems.url = "github:nix-systems/default"; }; # Flake outputs that other flakes can use outputs = - { - self, - nixpkgs, - unstable, - stylix, - home-manager, - nixvim, - lix-module, - systems, - }: - let - inherit (nixpkgs) lib; - # Helpers for producing system-specific outputs - supportedSystems = import systems; - forEachSupportedSystem = - f: - nixpkgs.lib.genAttrs supportedSystems ( - system: - f { - inherit system; - pkgs = nixpkgs.legacyPackages.${system}; - } - ); - overlays = builtins.attrValues self.overlays; - scripts_pkgs = import ./scripts; - scripts = final: prev: scripts_pkgs final; - in - { - checks = forEachSupportedSystem ( - { pkgs, system }: - let - src = builtins.path { - path = ./.; - name = "configuration.nix"; - }; - runCmdInSrc = - name: cmd: - pkgs.runCommandNoCC name { } '' - cd ${src} - ${cmd} - mkdir $out - ''; - in - { - nvim = nixvim.lib.${system}.check.mkTestDerivationFromNixvimModule { - pkgs = import nixpkgs { inherit system overlays; }; - module = ./nvim/standalone.nix; - }; - fmt = runCmdInSrc "fmt-src" "${lib.getExe self.formatter.${system}} --check ."; - lint = runCmdInSrc "lint-src" "${lib.getExe pkgs.statix} check ."; - typos = runCmdInSrc "typos-src" "${lib.getExe pkgs.typos} ."; - } - ); + inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = import inputs.systems; - packages = forEachSupportedSystem ( - { pkgs, system }: - scripts_pkgs pkgs - // { - inherit (import ./docs { inherit pkgs lib; }) - docs - nixos-markdown - nvim-markdown - home-markdown - ; - # Nvim standalone module - nvim = nixvim.legacyPackages.${system}.makeNixvimWithModule { - pkgs = import nixpkgs { inherit system overlays; }; - module = ./nvim/standalone.nix; - }; - } - ); - - # Provide necessary overlays - overlays = { - inherit scripts; - nixvim = nixvim.overlays.default; - unstable = - final: prev: - let - unstablePkgs = unstable.legacyPackages.${prev.system}; - in - { - # Get unstable packages - unstable = unstablePkgs; - - # Update vim plugins with the versions from unstable - vimPlugins = prev.vimPlugins // unstablePkgs.vimPlugins; - # Get specific packages from unstable - inherit (unstablePkgs) - gitoxide - jujutsu - neovim-unwrapped - ruff # nixpkgs stable version is improperly configured by nixvim - # wezterm - ; - }; - }; - - # Nix files formatter (run `nix fmt`) - formatter = forEachSupportedSystem ({ pkgs, ... }: pkgs.nixfmt-rfc-style); - - # Example vm configuration - nixosConfigurations.vm = - let - system = "x86_64-linux"; - config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "steam-original" ]; - pkgs = import nixpkgs { inherit system overlays config; }; - in - lib.nixosSystem { - inherit system pkgs; - modules = [ - self.nixosModules.vm # import vm module - { - time.timeZone = "Europe/Berlin"; - i18n.defaultLocale = "en_US.UTF-8"; - users.users.jdoe = { - password = "example"; - isNormalUser = true; - extraGroups = [ - "wheel" - "video" - "networkmanager" - ]; - }; - home-manager.users.jdoe = { - home = { - username = "jdoe"; - homeDirectory = "/home/jdoe"; - }; - jhome = { - enable = true; - gui.enable = true; - dev.rust.enable = true; - }; - }; - nix.registry.nixpkgs.flake = nixpkgs; - jconfig = { - enable = true; - gui.enable = true; - }; - } - ]; - }; - - nixosModules = - let - nvim-config.imports = [ - nixvim.homeManagerModules.nixvim - ./nvim - ]; - homeManagerModuleSandalone = import ./home { inherit nvim-config stylix; }; - homeManagerModuleNixOS = import ./home { inherit nvim-config; }; - nixosModule = { - imports = [ - (import ./system { inherit stylix; }) - home-manager.nixosModules.home-manager - ] ++ nixpkgs.lib.optional (lix-module != null) lix-module.nixosModules.default; - home-manager = { - useGlobalPkgs = true; - useUserPackages = true; - sharedModules = [ homeManagerModuleNixOS ]; - }; - # Pin nixpkgs - nix.registry.nixpkgs.flake = nixpkgs; - }; - - machines = [ "vm" ]; - mkMachine = hostname: { - imports = [ - nixosModule - (import (./machines + "/${hostname}")) - ]; - home-manager.sharedModules = [ { jhome.hostName = hostname; } ]; - }; - machineModules = lib.genAttrs machines mkMachine; - in - { - default = nixosModule; - inherit nixosModule homeManagerModuleNixOS homeManagerModuleSandalone; - } - // machineModules; - - devShells = forEachSupportedSystem ( - { pkgs, system }: - { - default = pkgs.mkShellNoCC { - buildInputs = [ - pkgs.just - self.packages.${system}.nvim - ]; - QEMU_OPTS_WL = "--enable-kvm -smp 4 -device virtio-gpu-rutabaga,gfxstream-vulkan=on,cross-domain=on,hostmem=2G,wsi=headless"; - }; - } - ); + imports = [ ./flake-modules ]; }; }