From f06b0d9c5b288b7fee828608e6eacf6c5959bba9 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 7 Aug 2024 14:47:32 -0400 Subject: [PATCH] refactor: restructure my custom nix functions --- common/home/neovim/langs/markdown.nix | 2 +- common/home/neovim/langs/web.nix | 2 +- homeManagerModules/firefox/default.nix | 2 +- inputs.nix | 35 ++---------------------- lib/default.nix | 19 +++++++++++++ lib.nix => lib/flake-lib.nix | 37 ++++--------------------- lib/inputs.nix | 38 ++++++++++++++++++++++++++ lib/pkgs.nix | 24 ++++++++++++++++ nixosModules/ags/default.nix | 2 +- outputs.nix | 2 +- 10 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 lib/default.nix rename lib.nix => lib/flake-lib.nix (61%) create mode 100644 lib/inputs.nix create mode 100644 lib/pkgs.nix diff --git a/common/home/neovim/langs/markdown.nix b/common/home/neovim/langs/markdown.nix index f016f083..30ee39a5 100644 --- a/common/home/neovim/langs/markdown.nix +++ b/common/home/neovim/langs/markdown.nix @@ -9,7 +9,7 @@ inherit (config.vars) neovimIde; inherit (pkgs) vimPlugins; - inherit (import "${self}/lib.nix" {inherit pkgs;}) buildPlugin; + inherit (import "${self}/lib" {inherit pkgs;}) buildPlugin; in lib.mkIf neovimIde { programs = { diff --git a/common/home/neovim/langs/web.nix b/common/home/neovim/langs/web.nix index 931327bd..61217d2f 100644 --- a/common/home/neovim/langs/web.nix +++ b/common/home/neovim/langs/web.nix @@ -9,7 +9,7 @@ inherit (config.vars) neovimIde; inherit (pkgs) vimPlugins; - inherit (import "${self}/lib.nix" {inherit pkgs;}) buildPlugin; + inherit (import "${self}/lib" {inherit pkgs;}) buildPlugin; in lib.mkIf neovimIde { programs = { diff --git a/homeManagerModules/firefox/default.nix b/homeManagerModules/firefox/default.nix index 39b25883..bc3479be 100644 --- a/homeManagerModules/firefox/default.nix +++ b/homeManagerModules/firefox/default.nix @@ -9,7 +9,7 @@ self: { firefox-gx = pkgs.callPackage ./gx-theme.nix { inherit (self.inputs) firefox-gx-src; - inherit (import "${self}/lib.nix" {}) mkVersion; + inherit (import "${self}/lib" {}) mkVersion; }; in { config = { diff --git a/inputs.nix b/inputs.nix index c569221f..ed0b0b0f 100644 --- a/inputs.nix +++ b/inputs.nix @@ -1,37 +1,6 @@ let - lock = builtins.fromJSON (builtins.readFile ./flake.lock); - lib = import "${builtins.fetchTarball { - url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; - sha256 = lock.nodes.nixpkgs.locked.narHash; - }}/lib"; - - inherit (lib) attrValues findFirst foldl' hasAttr listToAttrs matchAttrs map optionalAttrs recursiveUpdate removeAttrs; - - recursiveUpdateList = list: foldl' recursiveUpdate {} list; - - # Misc functions - mkInput = {type ? "github", ...} @ info: let - input = - findFirst - (x: matchAttrs (removeAttrs info ["inputs"]) (x.original or {})) {} - (attrValues lock.nodes); - - mkOverride = i: - optionalAttrs - (hasAttr i (input.inputs or {})) - {inputs.${i}.follows = i;}; - in - recursiveUpdateList [ - info - {inherit type;} - (mkOverride "systems") - (mkOverride "flake-utils") - (mkOverride "lib-aggregate") - ]; - - mkDep = info: mkInput (recursiveUpdate info {inputs.nixpkgs.follows = "nixpkgs";}); - mkHyprDep = info: mkInput (recursiveUpdate info {inputs.hyprland.follows = "hyprland";}); - mkSrc = info: mkInput (info // {flake = false;}); + inherit (import ./lib {}) lib mkDep mkInput mkHyprDep mkSrc; + inherit (lib) listToAttrs map removeAttrs; # Inputs nixTools = { diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 00000000..a4cfcee8 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,19 @@ +{ + inputs ? {}, + pkgs ? {}, +}: let + lock = builtins.fromJSON (builtins.readFile ../flake.lock); + + lib = import "${builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/${lock.nodes.nixpkgs.locked.rev}.tar.gz"; + sha256 = lock.nodes.nixpkgs.locked.narHash; + }}/lib"; + + inherit (lib) optionalAttrs; + + mkVersion = src: "0.0.0+" + src.shortRev; +in + {inherit lib mkVersion;} + // (import ./inputs.nix lib lock) + // optionalAttrs (inputs != {}) (import ./flake-lib.nix inputs) + // optionalAttrs (pkgs != {}) (import ./pkgs.nix pkgs mkVersion) diff --git a/lib.nix b/lib/flake-lib.nix similarity index 61% rename from lib.nix rename to lib/flake-lib.nix index a0c19331..1e2d3375 100644 --- a/lib.nix +++ b/lib/flake-lib.nix @@ -1,32 +1,7 @@ -{...} @ inputs: rec { - mkVersion = src: "0.0.0+" + src.shortRev; - - buildPlugin = pname: src: - inputs.pkgs.vimUtils.buildVimPlugin { - inherit pname src; - version = mkVersion src; - }; - - buildNodeModules = dir: npmDepsHash: let - pkg = inputs.pkgs.callPackage ({buildNpmPackage, ...}: let - inherit (builtins) readFile fromJSON; - - packageJSON = fromJSON (readFile (dir + /package.json)); - in - buildNpmPackage { - pname = packageJSON.name; - inherit (packageJSON) version; - - src = dir; - - inherit npmDepsHash; - dontNpmBuild = true; - }) {}; - in "${pkg}/lib/node_modules/${pkg.pname}/node_modules"; - - # Import pkgs from a nixpkgs - mkPkgs = system: input: - import input { +inputs: rec { + # Import pkgs from a nixpkgs instance + mkPkgs = system: nixpkgs: + import nixpkgs { inherit system; config.allowUnfree = true; overlays = @@ -56,7 +31,7 @@ modules = [ {home-manager.extraSpecialArgs = specialArgs;} - ./common + ../common ] ++ mods; }; @@ -77,7 +52,7 @@ }; } {home-manager = {inherit extraSpecialArgs;};} - ./common/nix-on-droid.nix + ../common/nix-on-droid.nix ] ++ mods; }; diff --git a/lib/inputs.nix b/lib/inputs.nix new file mode 100644 index 00000000..4e2fa435 --- /dev/null +++ b/lib/inputs.nix @@ -0,0 +1,38 @@ +lib: lock: let + inherit (lib) attrValues findFirst foldl' hasAttr matchAttrs optionalAttrs recursiveUpdate removeAttrs; + + recursiveUpdateList = list: foldl' recursiveUpdate {} list; +in rec { + /** + From an attrset, returns a flake input that has its type defaulted + to `github` and has some of its inputs following this flake's input + of the same name. + + It gets information from the `flake.lock` file and can be used thanks + to flakegen + */ + mkInput = {type ? "github", ...} @ info: let + input = + findFirst + (x: matchAttrs (removeAttrs info ["inputs"]) (x.original or {})) {} + (attrValues lock.nodes); + + mkOverride = i: + optionalAttrs + (hasAttr i (input.inputs or {})) + {inputs.${i}.follows = i;}; + in + recursiveUpdateList [ + info + {inherit type;} + (mkOverride "systems") + (mkOverride "flake-utils") + (mkOverride "lib-aggregate") + ]; + + mkDep = info: mkInput (recursiveUpdate info {inputs.nixpkgs.follows = "nixpkgs";}); + + mkHyprDep = info: mkInput (recursiveUpdate info {inputs.hyprland.follows = "hyprland";}); + + mkSrc = info: mkInput (info // {flake = false;}); +} diff --git a/lib/pkgs.nix b/lib/pkgs.nix new file mode 100644 index 00000000..831d7f6f --- /dev/null +++ b/lib/pkgs.nix @@ -0,0 +1,24 @@ +pkgs: mkVersion: { + buildPlugin = pname: src: + pkgs.vimUtils.buildVimPlugin { + inherit pname src; + version = mkVersion src; + }; + + buildNodeModules = dir: npmDepsHash: let + pkg = pkgs.callPackage ({buildNpmPackage, ...}: let + inherit (builtins) readFile fromJSON; + + packageJSON = fromJSON (readFile (dir + /package.json)); + in + buildNpmPackage { + pname = packageJSON.name; + inherit (packageJSON) version; + + src = dir; + + inherit npmDepsHash; + dontNpmBuild = true; + }) {}; + in "${pkg}/lib/node_modules/${pkg.pname}/node_modules"; +} diff --git a/nixosModules/ags/default.nix b/nixosModules/ags/default.nix index 3a4859a9..45f74d7a 100644 --- a/nixosModules/ags/default.nix +++ b/nixosModules/ags/default.nix @@ -68,7 +68,7 @@ in { home = { file = let - inherit (import "${self}/lib.nix" {inherit pkgs;}) buildNodeModules; + inherit (import "${self}/lib" {inherit pkgs;}) buildNodeModules; mkType = package: girName: { "${agsConfigDir}/config/types/@girs/${toLower girName}".source = diff --git a/outputs.nix b/outputs.nix index 96de0b64..9efc6202 100644 --- a/outputs.nix +++ b/outputs.nix @@ -53,7 +53,7 @@ systems, ... }: let - inherit (import "${self}/lib.nix" inputs) mkVersion mkNixOS mkNixOnDroid mkPkgs; + inherit (import ./lib {inherit inputs;}) mkVersion mkNixOS mkNixOnDroid mkPkgs; perSystem = attrs: nixpkgs.lib.genAttrs (import systems) (system: